Pointer variables store memory addresses. They must be declared with a data type and the asterisk (*) symbol. Pointer variables can point to variables of basic data types like int, float, and char. Arithmetic operations on pointers change the memory address being pointed to. Pointers are useful for handling arrays, returning multiple values from functions, and dynamic memory allocation.
2. What is a variable?
Each variable must be defined before you can
â
use it inside your program.
Did you ask yourself why we have to declare
â
variables?
âFirst reason is To allocate memory
âSecond reason is Instruct compiler how
to treat this memory
location ML? As int or
float...
3. What is a variable?
int main() Three memory locations have
{ been allocated.
int x = 16;
float y = 2.3; âThe first memory location
char z = 70;
. holds int value.
.
. â The second memory
} location holds floating-point
number.
âThe last memory location
holds one byte integer.
4. What is a variable?
âA variable is a named memory location.
âVariables provide direct access to its memory
location.
âCan you understand what will happen when you
write:
int x;
X variable
x = 44;
44 value
1638 address
5. Memory Addresses
Each Memory Location has an address â each
â
variable has an address.
Memory address in 32-bit Machines is 32-bit
â
unsigned integer number.
How to get the address of a variable?
â
&variable_name
int i=15;
â
printf(â%d
â %uâ,i,&i);
&i returns the address of i.
â
6. Pointer Variables
â Ask yourself:
âWhat is an integer variable?
âWhat is a floating-point variable?
âWhat is a character variable?
â Now, what is a pointer variable?
â It is a variable that stores the memory address.
OR
â It points to a storage locations of memory.
OR
â Pointers contain memory addresses as their values.
7. Declaring Pointer Variables
Like ordinary variables, pointer variable must be
â
declared before they are used.
Declaration form
â
â datatype *pointer_variable;
âThe * tells the compiler that this is pointer variable
âPointer_variable will point to the given datatype
â Declaration Style
âint* iptr;
âint *iptr;
âint * iptr;
8. Example
â int *iptr;
iptr is pointer variable of integer data type, it will store the
â
memory address of int data type.
â float *fptr;
fptr is pointer variable of float data type, it will store memory
â
address of float data type.
â char *cptr;
cptr is pointer variable of character data type, it will store
â
memory address of character data type.
9. Suspicious Pointer Conversion
When you assign a particular type of pointer
â
variable with address of different type, such type
of automatic type conversion is known as
suspicious type conversion.
In turbo c compiler it is not cause of any
â
compilation error but compiler will send one
warning message: suspicious pointer
conversion. So we should avoid suspicious
pointer conversion.
10. Generic Pointer
âvoid pointer in c is known as generic pointer. Literal
meaning of generic pointer is a pointer which can point
type of data.
âvoid *ptr;
âHere ptr is generic pointer.
NULL pointer is a pointer which is pointing to nothing.
â
NULL pointer points the base address of segment.
âint *ptr=(char *)0;
âfloat *ptr=(float *)0;
âchar *ptr=(char *)0;
âdouble *ptr=(double *)0;
âchar *ptr=â0â;
âint *ptr=NULL;
NULL is macro constant which has been defined in the
â
heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h
as
11. wild pointer
A pointer in c which has not been initialized is
â
known as wild pointer.
â Example:
void main()
{
int *ptr; Output: Any address
printf("%un",ptr); Garbage value
printf("%d",*ptr);
}
12. Dangling pointer
âIf any pointer is pointing the memory address of
any variable but after some time that variable
has been deleted from that memory location
while pointer is still pointing such memory
location. Such pointer is known as dangling
pointer and this problem is known as dangling
pointer problem.
13. Arithmetic Operations with
Pointer
Like ordinary variables, pointer variables can also be used as a
â
part of expression.
The following rules are applied when we perform arithmetic
â
operations on pointer variables.
âAddress + Number= Address
âAddress - Number= Address
âAddress++ = Address
âAddress-- = Address
â++Address = Address
â--Address = Address
âIf we will add or subtract a number from an address result will also be an
address
14. Benefits of using pointer
âEfficient in handling arrays and data tables.
âReturns multiple values from a function.
âUse of pointer arrays to character strings results
in saving of data storage space in memory.
âSupports dynamic memory management.
âCan manipulate dynamic data structure.
âIncreases execution speed and reduces
execution time