Here is a short Pointer Refresher in C Language:
-pointers: variables that hold the 'memory address' of another variable
-They are declared using '*' operator and initialized using '&' operator.
-eg:
$ cat -n ptr0.c
1 #include <stdio.h>
2
3 int main()
4 {
5 int num;
6 int *pnum; /* pointer declaration */
7 pnum=# /* pointer initialization */
8 num=57;
9 printf("\n Value of variable num = %d.", num);
10 *pnum=23; /* indirect invocation of value of variable num via pointer */
11 printf("\n New value of variable num = %d.", num);
12 printf("\n The first value was direct invocation of variable.");
13 printf("\n The second value was an indirect invocation of variable, using a pointer.");
14 printf("\n Good bye.\n");
15 return 0;
16 }
-advantages of Pointers:
.allow dynamic allocation of mem /* see '*pum=23;' in above code */
.allow functions to modify calling argument
-void pointer:
.is defined as 'void *pointervar'
.its type is unknown and can thus receive a parameter that receive any type of pointer argument
.in other words, during value assignment, no explicit cast is required.
-see below code for void pointer illustration:
$ cat -n voidptr.c
1 #include <stdio.h>
2
3 int main()
4 {
5 int i=5;
6 char ch='M';
7 void *ptr; /* declaring a void pointer */
8
9 ptr=&i; /* initialize ptr with an integer var addr */
10 printf("\n Pointer ptr now points to an integer %d.", *(int *)ptr);
11 ptr=&ch; /* initialize ptr with an character var addr */
12 printf("\n Pointer ptr now points to a char %c.", *(char *)ptr);
13 printf("\n The same pointer was used to point to an integer and then to a char.");
14 printf("\n This was possible only due to a void pointer.");
15 printf("\n Good bye.");
16
17 return 0;
18 }
19
-pointers: variables that hold the 'memory address' of another variable
-They are declared using '*' operator and initialized using '&' operator.
-eg:
$ cat -n ptr0.c
1 #include <stdio.h>
2
3 int main()
4 {
5 int num;
6 int *pnum; /* pointer declaration */
7 pnum=# /* pointer initialization */
8 num=57;
9 printf("\n Value of variable num = %d.", num);
10 *pnum=23; /* indirect invocation of value of variable num via pointer */
11 printf("\n New value of variable num = %d.", num);
12 printf("\n The first value was direct invocation of variable.");
13 printf("\n The second value was an indirect invocation of variable, using a pointer.");
14 printf("\n Good bye.\n");
15 return 0;
16 }
-advantages of Pointers:
.allow dynamic allocation of mem /* see '*pum=23;' in above code */
.allow functions to modify calling argument
-void pointer:
.is defined as 'void *pointervar'
.its type is unknown and can thus receive a parameter that receive any type of pointer argument
.in other words, during value assignment, no explicit cast is required.
-see below code for void pointer illustration:
$ cat -n voidptr.c
1 #include <stdio.h>
2
3 int main()
4 {
5 int i=5;
6 char ch='M';
7 void *ptr; /* declaring a void pointer */
8
9 ptr=&i; /* initialize ptr with an integer var addr */
10 printf("\n Pointer ptr now points to an integer %d.", *(int *)ptr);
11 ptr=&ch; /* initialize ptr with an character var addr */
12 printf("\n Pointer ptr now points to a char %c.", *(char *)ptr);
13 printf("\n The same pointer was used to point to an integer and then to a char.");
14 printf("\n This was possible only due to a void pointer.");
15 printf("\n Good bye.");
16
17 return 0;
18 }
19
No comments:
Post a Comment