Like Tree2Likes
  • 2 Post By grumpy

Please help!!! array size

This is a discussion on Please help!!! array size within the C Programming forums, part of the General Programming Boards category; Hello, Can someone tell me why do I get 16 instead of the full size of row * col * ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Exclamation Please help!!! array size

    Hello,

    Can someone tell me why do I get 16 instead of the full size of row * col * int?
    (If I did int *iptr[4][7]; however, I would get (4*7*4) = 112 and not 16)

    Code:
    int *iptr[4]; // array of 4 elements
    
        for(i=0; i<4; i++)
        {
            iptr[i] = malloc(sizeof(int) * 7); // expand each array to by 7 wide
        }
    
        printf("size of iptr = %d\n",  sizeof(iptr));
    Thanks in advance for your help

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    What is the type of iptr[0]?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    sizeof(iptr) is equal to the size of an array of four pointers (to int). That is equivalent to four times the size of a pointer. The size of a pointer on a typical 32-bit system is 4 (corresponding to 4 8-bit types). 4x4 is 16.


    sizeof() is a compile time expression. malloc() is function that gets called at run time. So there is no relationship between the argument you supply to any malloc() call and any subsequent usage in code of sizeof(), since the value yielded by the sizeof() operator is computed by the compiler before the program is even executed.
    stahta01 and iMalc like this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-23-2010, 11:09 PM
  2. How do I get the size of an array from a pointer to the array?
    By Programmer_P in forum C++ Programming
    Replies: 31
    Last Post: 06-03-2010, 04:13 PM
  3. array size
    By Freshjunior in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2010, 07:17 AM
  4. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 04:20 PM
  5. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 02:51 PM

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