Page 73 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 73

Fundamentals of Data Structures




                    Notes           printf(“\n arr[0] = [%s] \n”,arr[0]);
                                    printf(“\n arr[1] = [%s] \n”,arr[1]);
                                    printf(“\n arr[2] = [%s] \n”,arr[2]);
                                    return 0;
                                   }
                                   In the above code, we took three pointers pointing to three strings. Then we declared an array
                                   that can contain three pointers. We assigned the pointers ‘p12, ‘p22 and ‘p32 to the 0,1 and 2 index
                                   of array. Let’s see the output :
                                   $ ./arrayofptr
                                    p1 = [Himanshu]
                                    p2 = [Arora]
                                    p3 = [India]
                                    arr[0] = [Himanshu]
                                    arr[1] = [Arora]
                                    arr[2] = [India]

                                   So we see that array now holds the address of strings.
                                   5.2.1 Multidimensional Arrays and Pointers


                                   We should think of multidimensional arrays in a different way in C:
                                   A 2D array is really a 1D array, each of whose elements is itself an array. Hence
                                    a[n][m] notation.
                                   Array elements are stored row by row.
                                   When we pass a 2D array to a function we must specify the number of columns — the number of
                                   rows is irrelevant.
                                   The reason for this is pointers again. C needs to know how many columns in order that it can
                                   jump from row to row in memory.
                                   Consider int a[5][35] to be passed in a function:
                                   We can do:
                                    f(int a[][35]) {.....}
                                   or even:
                                    f(int (*a)[35]) {.....}
                                   We need parenthesis (*a) since [] have a higher precedence than *
                                   So:

                                    int (*a)[35]; declares a pointer to an array of 35 ints.
                                    int *a[35]; declares an array of 35 pointers to ints.
                                   Now lets look at the (subtle) difference between pointers and arrays. Strings are a common
                                   application of this.
                                   Consider:
                                    char *name[10];
                                    char Aname[10][20];



          66                                LOVELY PROFESSIONAL UNIVERSITY
   68   69   70   71   72   73   74   75   76   77   78