in C/C++

Pointer Basic

Long ago, before I started to write my first C program, I was told that pointer will be the one of the difficulties in C to struggle with.  Now, after getting enrolled into computer science major in college for one year, I still feel scared when I use pointers, especially double pointers, during programming and hope some day I will learn pointer systematically. I think today is that day!


Just find an used book about pointer while I am writing this post --- Understanding and Using C Pointers, Richard Reese

Pointer basic:

Variables stored in memory have two attributes --- namely value and address.


In micro level, computer system will access each variable in memory according to its address.

In high level language, compared with assembly language, we can access a variable through its identifier (name of the variable) or address. Pointer can help us achieve the second approach. 

Pointer declaration:

char *pr, *pr1; //pr and pr1 are both pointers to char
char *pr, pr1; //pr is the pointer to char, while pr1 is only a char type

Pointer assignment:


char array[4] = {'a','b','c',''}; //a char array
char *pr; //pointer that point to a memory cell with char type
pr = array; //assign the address of the array to pointer pr


Notice: when we use array, the name of the array will point to the first element in the array. Then we assign the array to a pointer, it actually pass the address of the first element of that array to the pointer.

 
If we want to access the specific element in the array, we can use the dereference operator '*' and  add the index to the pointer
 
printf( "%s", pr);  //it will print the whole array: abc
printf( "%c", *(pr + 0) ); //it will print the second element: a
printf( "%c", *(pr + 1) ); //it will print the second element: b
printf( "%c", *(pr + 2) ); //it will print the third element : c

 

Pointer operators:

In C and C++, there are two types operators for address access --- reference and dereference operator

The reference operator: '&'
Reference operator will :
            1. retrieve the address of the variable
            2. claim a reference to a type

name = 'c';
char *pr;
pr = &name; //retrieve the address of variable 'name' and assign it to pointer


The dereference operators: '*'
Dereference operator will access the value stored in memory according to the address.

char name = 'b';
char *pr = &name;
printf( "%c", *pr ); //it will print: b

 

Double pointer (pointers to pointers):

C and C++ also support multi-level pointer. Double pointer allows user to initialize a pointer that points to another pointer.


char array[4] = {'a','b','c',''}; //a char array
char *pr = array; //pointer that point to a memory cell with char type
char **ppr = pr; //pointer that points to the pointer 'pr'


In my view, the one of the practice for double pointer is that we can build a dynamic multi-dimentsion array by using malloc().

Example: 


char array0[4] = {'a','b','c',''}; //a char array
char array1[4] = {'d','e','f',''};
char array2[4] = {'g','h','i',''};
char **ppr = (char **)malloc( 3 * sizeof( char *)); //dynamically allocate a double pointer
*pr = array0;
*(pr + 1) = array1;
*(pr + 2) = array2;
printf( "%c", **ppr ); //it will output: a
printf( "%s", *ppr );  //it will output: abc
printf( "%s", *(ppr+1); //it will output: def
printf( "%s", *(ppr+2); //it will output: ghi

Write a Comment

Comment