How to read complex pointers in C Programming?
Where
(): This operator behaves as bracket operator or function operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator not as multiplication operator.
Identifier:
It is not an operator but it is name of pointer variable. You will
always find the first priority will be assigned to the name of pointer.
Data type: It is also not an operator. Data types also includes modifier (like signed int, long double etc.)
You will understand it better by examples:
(1) How to read following pointer?
char (* ptr)[3]
Answer:
Step 1:
() and [] enjoys equal precedence. So rule of associative will decide
the priority. Its associative is left to right So first priority goes to
().
Step 2:
Inside the bracket * and ptr enjoy equal precedence. From rule of
associative (right to left) first priority goes to ptr and second
priority goes to *.
Step3: Assign third priority to [].
Step4: Since data type enjoys least priority so assign fourth priority to char.
Now read it following manner:
Rule 2: Assign the priority of each function parameter separately and read it also separately.
Understand it through following example.
(3) How to read following pointer?
void (*ptr)(int (*)[2],int (*) void))
Answer:
Assign the priority considering rule of precedence and associative.
Now read it following manner:
ptr is pointer to such function which first parameter is pointer to one dimensional array of size two which content int type data and second parameter is pointer to such function which parameter is void and return type is int data type and return type is void.
Pointer : Accessing Value from Address in C
#include<stdio.h> main( ) { int i = 3 ; int *j ; j = &i ; printf ( "nAddress of i = %u", &i ) ; printf ( "nAddress of i = %u", j ) ; printf ( "nAddress of j = %u", &j ) ; printf ( "nValue of j = %u", j ) ; printf ( "nValue of i = %d", i ) ; printf ( "nValue of i = %d", *( &i ) ) ; printf ( "nValue of i = %d", *j ) ; }
Output:
Address of i = 65524 Address of i = 65524 Address of j = 65522 Value of j = 65524 Value of i = 3 Value of i = 3 Value of i = 3
How to Access Address of Variable using Pointer
Program :
#include<stdio.h>void main( )
{
int i = 3 ;
printf("nAddress of i = %u", &i ) ;
printf("nValue of i = %d", i ) ;
printf("nValue of i = %d", *( &i ) ) ;
}
Output:
Address of i = 65524 Value of i = 3 Value of i = 3Explanation :
Variable ‘i’ is declared of type integer in the program , now suppose the integer variable can have value 3 stored inside it and it has address 65524.
No comments:
Post a Comment