Thread: Pointers and 2DArray

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Thumbs up Pointers and 2DArray

    In below code, I am getting following error.. why it happens? .. i

    How to assign it to pointer str? [ without changing a[5][10] to other notation]


    cannot convert `char (*)[10]' to `char**' in initialization

    Code:
    int main()
    {
            char a[5][10]={"One", "Two", "Three", "Four", "Five" };
            char **str=a;                                                             // This is not Ok  ???
            char *b[5]={"One", "Two", "Three", "Four", "Five" };
            char **str1 = b;  // this is Ok
    
            printf("%d %d %d %d\n", sizeof(a), sizeof(str), sizeof(b), sizeof(str1));
            printf("%p %p %p %p %p %p\n", a, str, &a[1], &str[1], &a[1][1], &str[1][1]);
            printf("%p %p %p %p %p %p\n", b, str1, &b[1], &str1[1],
    				 &b[1][1], &str1[1][1]);
    				 getchar();
    				 return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since char [5][10] is not in itself an array of pointers, there is no simple way to make it into a pointer to pointers. You simply can not do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char **str=a;
    But this should be
    char (*str)[10] = a;

    That is str is a pointer to an array of 10 characters.

    Without the (), it's just an array of 10 pointers to char, which is something completely different.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    you can put an intermediate step to achieve that as in.
    Code:
    char a[5][10]={"One", "Two", "Three", "Four", "Five" };
    char *p = &a[0][0];
    char **str = &p;

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    you can put an intermediate step to achieve that as in.
    Code:
    char a[5][10]={"One", "Two", "Three", "Four", "Five" };
    char *p = &a[0][0];
    char **str = &p;
    And that makes a single pointer to pointer, so if you later try to use str[1], it will point to some random place in the memory - not at "Two".

    And
    Code:
    char *p = a[0];
    is a bit simpler to write what you did.

    Now, if you really want to be able to access the 5 elements in the original array with a pointer to pointer, you need to have a char *p[5], and set each element to a[0], a[1], a[2], etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Now, if you really want to be able to access the 5 elements in the original array with a pointer to pointer, you need to have a char *p[5], and set each element to a[0], a[1], a[2], etc.
    This is not quite true, although I wouldn't recommend anyone to do that:

    Code:
            char a[5][10]={"One", "Two", "Three", "Four", "Five"};
            char *p = a[0];
            char **str = &p;
    
            printf("%s\n", (*str + n*10));
    Now you can use the printf() to display the n-th element of a. But this is of course much the same as simply writing *&p instead of *str in the printf(). On the other hand, its seems senseless to use a pointer to a pointer in the first place anyway.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess that is in return for the "you should not leave stuff uninitialized": Yes, of course, using the right arithmetic will make any pointer point to any other place in memory, so if it is possible to calculate the right place, we can do that. But it's hardly an OBVIOUS way to do it. In my defence, my comment was referring to so if you later try to use str[1]... (although I should, in hindsight [doh!], have made the reference more explicit).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed