Hazy Pointer output [:(]

This is a discussion on Hazy Pointer output [:(] within the C Programming forums, part of the General Programming Boards category; hello all, i was trying a question which i have mentiond below. please try to explain the last output in ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Lightbulb Hazy Pointer output [:(]

    hello all,

    i was trying a question which i have mentiond below.

    please try to explain the last output in the first printf and please verify your tests using the value 'oneone' in place of 'one' in char a[5][10] and then arrive on any answer



    program:


    Code:
    main()
    {
            char a[5][10]={"One", "Two", "Three", "Four", "Five" };
            char **str = a;
            char *b[5]={"One", "Two", "Three", "Four", "Five" };
            char **str1 = b;
            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]);
    }




    thanks in advance.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What's the question?

    BTW, arrays start at index 0, not 1.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    i mean i was trying the mentioned program. sorry for the mistake
    and i yes array index starts from o and not 1 .
    but how does that matter in the current context ??

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, ok, so you tried the program. So what? What do you want explained?

    I brought up the index one thing, because looking at index 1 means that you're going to get a different address than if you are looking at index 0 (which array names generally degenerate into pointers that point at index 0). If you're going to do comparisons, you probably want to compare apples to apples.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Deathray:

    as far as i could see i have made the question and my area of doubt very much clear int the problem text itself.
    it would be appreciable if you could offer me some help only after you try this and see the output.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    OK, you'll have to rely on someone else to help you, since I can't understand what the issue is.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    ok ok
    i'll explain further for you and all who visit this therad
    the program was tried on a 32 bit machine and the last output of the first printf statement gives a value "1" with the original code, i can understand that very well, however i cant explain the answer if i edit the two dimentional array contents from "one" to "oneone".

    am i able to make the issue clearer now ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    The answer is that you are trying to convert a char[5][10] (named a) into a char** (named str), but they are incompatible types for such a conversion. I think that this is in the realm of undefined behaviour, so it is pointless to try and explain the output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Depends on what you mean by compatible. Since str is a char**, it thinks that the "second dimension" of the array is one (since it's not an actual char[5][10], but just a **). So str[1][1] is the same as str[2] is the same as str+2. They are compatible for assignment, but you can't index with the [] notation.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Compiler warning: warning initialization from incompatible pointer types
    That goes for char **str = a;

    The results are correct for b. Not for a. So the above assignment STRANGELY enough is wrong. Though never doubt GCC warnings

    The size of str is 8 byte. The size of a is 50 byte.
    Which means that a is not a pointer. Indeed. Try
    printf("%s", a)
    It will print "one". So a is actually a[0]. Again if you put:
    printf("%s", *a)
    you ll get the same result.

    Aahh, this is kind of strange.
    Last edited by C_ntua; 06-16-2008 at 06:47 AM.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Hello Guys:

    as far as my anamlysis is conserned i have found the following please spend some time and send me your feed-backs:

    its correct that the a pointer to a pointer and an a 2D array do not replaces each other in all usages of each other.

    lets take a look into the output of the first printf:
    things most probably goes this way:

    we have the same memory address output for 'a' and 'str'
    next

    &a[1] means 10 bytes shift there we get an address 10 spaces to the base address.

    &str[1] means the pointer str is incremented by size of a pointer so an address 4 spaces to its base address which it initially hold.

    &a[1][1] in the same steps gets an increment of 11 bytes so an address 11 bytes to the base address is received.

    now comes the one causing problems:

    &str[1][1] --> now we know that str[1] points to the place just next to the place where there is a '\o' after 'e' of 'ONE'. the value there is NULL whic is an address again and we increment the address by 1 and thus the answer we receive is 1.
    but be cautiousthis 1 is not an integer rather its an address.
    u can confirm by performing any illigal operation it throws error.

    reviews are welcome.
    now

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by C_ntua
    The size of str is 8 byte. The size of a is 50 byte.
    Which means that a is not a pointer.
    Of course, a is an array, not a pointer, but arrays are converted to pointers to their first element. That is why you have your observation that "a is actually a[0]". However, a is not a[0] since they have different types, even though they have the same address.

    Quote Originally Posted by tabstop
    Depends on what you mean by compatible. Since str is a char**, it thinks that the "second dimension" of the array is one (since it's not an actual char[5][10], but just a **). So str[1][1] is the same as str[2] is the same as str+2. They are compatible for assignment, but you can't index with the [] notation.
    I relied on gcc to be compliant with the C standards in making my observation (e.g., C99 states that "for two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types"). How do you explain the gcc warning?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's a good question. They are certainly convertible according to 6.3.2.3. It doesn't say there you get undefined behavior, but that doesn't refer to indexing.

    I don't think they are compatible, reading the standard again; example 3 on the bottom of page 117 shows that you can only take out one [] to get compatible types. So I was mistaken, and you are right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21