This blog is under construction

Wednesday, 25 December 2013

Array of character pointers

Consider an array of n elements.  If each element in the array is a pointer to character, then the array is called as array of character pointer.

How to declare array of character pointer?
Below is the declaration for character pointer.
char *arr[10];

How to initialize array of character pointers?
Below is the initialization of array of character pointers.
char *days[7] = { "SUN", "MON", "TUE", "WED",
                           "THU", "FRI", "SAT"};
Here, days is an array of 7 character pointers.  Each character pointer in the array days refers to the string constant at the corresponding index in the array.  For example,
days[0] points to the string constant "SUN"
days[1] points to the string constant "MON"
days[2] points to the string constant "TUE"
days[3] points to the string constant "WED"
days[4] points to the string constant "THU"
days[5] points to the string constant "FRI"
days[6] points to the string constant "SAT"

Dynamic memory allocation for array of character pointers:
Below is the procedure to perform dynamic memory allocation for array of character pointers.
char *days[7];
for (i = 0; i < 7; i++) {
    // allocate memory block of size 32 bytes
days[i] = (char *)malloc(sizeof(char) * 32);
}

How to access data pointed by character pointers in an array?
Below program explains how to access data pointed by character pointer in an array.


  #include <stdio.h>
  int main() {
        int i, ch;
        char *holiday[2] = {"SATURDAY", "SUNDAY"};
        for (i = 0; i < 2; i++) {
                /* access char by char using char pointer */
                while ((ch = *(holiday[0]++)) != '\0') {
                        printf("%c", ch);
                }
                printf("\n");
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  SATURDAY
  SUNDAY



Let us now see how the data are stored in memory in the case of static memory allocation.

  #include <stdio.h>
  int main() {
        int i, j, ch;
        char *holiday[2] = {"SAT", "SUN"}; // char pointer
        for (i = 0; i < 2; i++) {
                j = 0;
                /* loop until null character */
                while(*(holiday[i] + j) != '\0') {
                        /* print the address and value */
                        printf("0x%x => %c\n",
                                (holiday[i] + j), *(holiday[i] + j));
                        j++;
                }
                /* print the address & value of null character */
                printf("0x%x => \\%d\n", (holiday[i] + j), *(holiday[i] + j));
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  0x8048560 => S
  0x8048561 => A
  0x8048562 => T
  0x8048563 => \0
  0x8048564 => S
  0x8048565 => U
  0x8048566 => N
  0x8048567 => \0


From the output, we could infer that the characters in string constants "SUN" and "SAT" are located at consecutive memory locations.

No comments:

Post a Comment