#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Unable to understand two dimensional char arrays vs pointer to array of char pointers


    Hi

    I have a problem understanding how a two dimensional array is different from a pointer to an array of char pointers (argv argument to main)

    just to understand the difference I have executed the below code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main(int argc, char *argv[]){
        int i=0,j=0;char *p; 
        char A[4][10]={"Good","Nice","Better","Best"};
        
        for(i=0;i<argc;i++){
            printf("%p\n",*(argv+i));
        }
        
        printf("-----------------------\n");
        for(i=0;i<argc;i++){
            p=*(argv+i);
            /*printf("%p\n",p);*/
            while(*p)
            {printf("%c ",*p);
            p++;}
            printf("\n");
            } 
        printf("-----------------------\n");
        
        for(i=0;i<argc;i++){
            for(j=0;argv[i][j];j++)
            printf("%c",argv[i][j]);
            printf("\n");
        }
        printf("-----------------------\n");
        p=(char *)A;
        for(i=0;i<4;i++){
            for(;*p;p++){
                printf("%c",*p);
                }
                while(!*p){
                printf("-");p++;
                }
                printf("\n");
        }
            
        
    }
    From the result of above I have understood that a two dimensional array is stored in contiguous memory locations and the order of elements is specified by the index. A[i][j] points to a jth character of element i

    Argv is a pointer to an array which holds the address of each command line string. The command line strings are also observed to be stored in contiguous memory locations and each element of argv array has the address of command line arguments. That said so, I wonder how argv[i][j] works because what is stored in argv is not the data (command line argument) but the address of the data.

    argv is completely different in terms of storage and reference to A[][] but still both are accessible as if they were alike.

    Thanks
    Zulfi
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    When you say something like
    A[row][col]
    What the compiler generates is something like
    char c = *(A+(row*rowSize+col))

    Now when you do something like
    argv[row][col]
    something different happens.
    The first step is something akin to
    char *temp = argv[row];
    The second step is then
    char c = temp[col];

    You can see that the true array is a single memory reference and a bit of maths.
    For an array of pointers like argv, there is first a read to get the pointer to the array of chars, and then a second read to get the character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo