sizeof pointer-to-array -- &array

This is a discussion on sizeof pointer-to-array -- &array within the C Programming forums, part of the General Programming Boards category; Hello everyone. I'm trying to find out the size of some expressions. Code: #include <stdio.h> int main(void){ char a [100] ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    5

    sizeof pointer-to-array -- &array

    Hello everyone. I'm trying to find out the size of some expressions.

    Code:
    #include <stdio.h>
    
    int main(void){
        char   a [100] = {0};
        char (*p)[100] = &a;
    
        printf("%d\n", sizeof  a[0]);
        printf("%d\n", sizeof &a[0]);
        printf("%d\n", sizeof  a);
        printf("%d\n", sizeof  p);
        printf("%d\n", sizeof &a);
    
        return 0;
    }
    The 1st expression is type char and I get 1

    The 2nd is type char* (pointer-to-char), and I get 4--because of beeing on a 32 bits plataform, right?

    The 3rd one is type char[100] (array-of-100-char), and I get 100

    The 4th is type char(*)[100] (pointer-to-aray-of-100-char), and I get 4.

    The last one is also type char(*)[100] (pointer-to-array-of-100-chars), but I get 100, shouldn't I get 4?

    I've tested this progrman on an MS and on a Borland compiler. Both gave me the same result.

    I've looked in various books and the standard and I think I should be getting 4, not 100.

    What do you think?

    Thanks.

    Joey.
    Last edited by josemariasola; 07-15-2006 at 03:30 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    $ ./a.out
    1
    4
    100
    4
    4

    Your answers seem to be in the wrong order.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Yeah, here are my results, which are similar to Salem's

    Code:
    kermit@minty ~/cprogs/board $ ./sizes
    1
    8
    100
    8
    8
    If the code you compiled and ran is truly like that which you posted, then I would suggest getting a different compiler...

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Um. VC++2005 output.

    Code:
    1
    4
    100
    4
    100
    Funky.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    I made a beginners mistake. What I really got in the third one is 100, not 4.
    Code:
    printf("%d\n", sizeof  a);
    But the one I don't understand is the last one.
    Code:
    printf("%d\n", sizeof &a);
    I get the same results Tonto does.
    I've ran it on MSVC++2003, Borland 5, Borland 4 and even Borland 3 (where I got 1\n2\n100\n2\n100\n).
    May it be a bug in those compilers?
    Is it an implementation defined behavior?
    Or is it just a different interpretation of the standard?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I believe that the Borland and MS output you see is incorrect. gcc produces the correct result.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    And for anyone who is interested, icc produces the same result (this proves little, but is interesting nonetheless) as gcc on my machine.
    Last edited by kermit; 07-15-2006 at 04:35 PM.

  8. #8
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    While were on the topic, can anyone explain how/why sizeof knows if an array is pointer declared or subscript declared:
    Code:
    char a[] = "test";
    char *b = "test";
    sizeof(a);
    sizeof(b);
    Both a and b are arrays, both a and b are pointers to the first index, both arrays are identical in size and contents, and both can be accessed via pointer or subscript; Yet sizeof somehow still is able to know which was pointer-declared and which was not because using sizeof(a) returns the size of the enitre array (1 byte * 5 chars) whereas using sizeof(b) only returns the size of a char*.
    How is this possible? Is there some sort of flag that goes up within the variable so that sizeof will know whether it was pointer declared or not? And if so, I thought arrays were designed to be completely interchangeable between use of either pointers or subscripts while having the exact same results...(use of char x[] vs char *x was always said to be more for the programmer's convenience rather than the compiler's, since they would be represented in memory exactly the same way) however, in light of the sizeof operator, it would appear that there are differences in representation between a pointer declared array and a subscript declared array. Are there any other differences between them that would effect behaviour?
    Last edited by @nthony; 07-15-2006 at 04:44 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by @nthony
    Both a and b are arrays
    b is not an array, it is a pointer.

    Quote Originally Posted by @nthony
    since they would be represented in memory exactly the same way
    Wrong.

    Quote Originally Posted by @nthony
    Are there any other differences between them that would effect behaviour?
    http://c-faq.com/aryptr/index.html
    http://c-faq.com/aryptr/aryptrequiv.html
    http://web.torek.net/torek/c/expr.html#therule
    Last edited by Dave_Sinkula; 07-15-2006 at 04:49 PM. Reason: Sorry about all the edits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Both a and b are arrays
    Not really. a is an array and b is a pointer to the first element of an array. Most certainly not the same thing even though the difference may not always be obvious.

    >both a and b are pointers to the first index
    How can a and b be both arrays and pointers? If your statements contradict each other then it's obvious that you're confused about the topic. May I recommend C for Smarties?

    >How is this possible?
    Because a is an array and b is a pointer. sizeof is one of the cases where an array name is not converted into a pointer to the first element.

    >I believe that the Borland and MS output you see is incorrect.
    As do I. There's no logical interpretation of the standard that could produce those results.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    >>I believe that the Borland and MS output you see is incorrect.
    >As do I. There's no logical interpretation of the standard that could produce those results.

    But is it possible that both companies be wrong on this same subject for so long?

    Could there be another explanation than "it's just a bug both companies have"?

    Joey.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Doubtful. See the standard is quite clear on how it likes things done. That's why we call it the Standard. Microsoft likes to implement their own things all the time, in an effort to get them the way they like them, throwing the standards out the window. (See the Microsoft Java vs Sun Java wars as an example.)

    For example, Microsoft allows you to fflush( stdin ) which is clearly wong even at a basic conceptual level.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I don't see why it's printing out 100. Interesting.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Bubba
    I don't see why it's printing out 100. Interesting.
    Perhaps it descards the &.
    http://groups.google.com/group/comp....e1f45751aa74e9
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    Quote Originally Posted by quzah
    Doubtful. See the standard is quite clear on how it likes things done. That's why we call it the Standard. Microsoft likes to implement their own things all the time, in an effort to get them the way they like them, throwing the standards out the window
    OK, that's right for MS, but does that also goes for Borland?

    Maybe I'm beeing a little naive, but I carefully configured both, Borland and MS, compilers to conform to ANSI C 90 and to emit all possible warnings.

    Should I just accept it's a bug?

    Joey.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

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