-->

Pointer Definition and Declaration

Pointer Definition and Declaration

Pointer Definition and Declaration

Pointer is a variable, which holds the address of another variable.
Example: if x contains the address of y, then x is said to “pointing to” y.
Declaration:
Like a variable or a constant, we must declare a pointer before we use it to store a variable address.
Pointer is declared with a preceding asterisk (*).
The general form of a pointer variable declaration is: <data-type> * <var-name>;
Where ‘var-name’ is the name of the pointer variable and ‘data-type’ is a regular ‘C’ data type like int, char etc.

Example:
int *ptr; //Here ptr is the integer pointer variable
int ptr; //Here ptr is the normal integer variable

The declaration tells the compiler three things about the variable ‘ptr’
-Te asterisk (*) tells that the variable ‘ptr’ is a pointer variable.
-‘ptr’ needs a memory location.
-‘ptr’ points to a variable of type ‘data-type’.

Example:
int i=5;
int *ptr; //declare a pointer variable
ptr=&i; //store address of ‘i’ in ‘ptr’
printf(“*ptr=%d\n”,*ptr); //refer to reference of ‘ptr’
Here, ptr is a variable storing an address but not storing the actual value of ‘i’
Pointer Definition and Declaration,Features of Pointers,Pointer Variables Declaration,Initialization of the pointer variable,difference between Pointers and Functions,Pointers and Functions,Dangling Memory in pointers,Pointer Arithmetic,Incrementing a Pointer,Decrementing a Pointer,Pointer comparisons,Pointer Arithmetic and Arrays,Pointer to Pointer,Arrays and Pointers,Pointer to Void,Memory Allocation Functions,Conceptual view of memory,Accessing Dynamic Memory,Memory Management functions,Block Memory Allocation – Malloc,Contiguous Memory Allocation – Calloc,Reallocation of Memory – Realloc,Releasing Memory,Introduction to Pointers,define pointer in c language,c programming pointers,what is pointer in c language,use of pointers,difference between arrays and pointers,role of pointer,pointers concept in c language,how to declare pointer variable in c,pointer declaration in c language,Overview of Pointers,cse study zone,real time examples for pointers,pointers examples


Pointer Operators
There are two operators that are used with pointers namely, address-of-operator(&) and indirection/de-reference operator(*).

Address-of operator(&): it returns the address of the variable instead of stored value.
“&” is used on a variable (int, char, double and array element) to get the address:

Example: ptr = &j;
Address
Content
Address
Content
Address
1000
i: 40
1001
j: 33
1002
1004
ptr: 1001
1005

1006
“&” cannot be used on a constant, array name, register variable and expressions:

Example:
ip1 =&”abcdef”; //incorrect
ip1 = “abcdef”;  //correct
indirection/De-reference operator (*): It indicates that ptr is a pointer to type int and not a normal variable of type int.
‘*’ is used on a pointer to access the variable it points to.

Example:
int x=1, y ={2,3};
int *ip1, *ip2, *ip3;
ip1 = &x;
*ip1 =0; //x=0
printf(“%d\t%d\n”, x,y[0]);
ip2 =&y[0];
*ip2 = 1; //y[0] = 1;
“* pointer” has a data type which is the same as that of the variable the pointer points to.
int *a;
every pointer points to a specific data type except void *;
void pointer (void *) is used to hold the address of any data type.

Example:
ptr = &j;
*ptr = 99;
Refer to the content of the referee
Address
Content
Address
Content
1000
i: 40
1001
j: 99
1004
ptr: 1001
1005



Related Posts

Subscribe Our Newsletter