Thread: Please help!!! array size

  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,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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-24-2010, 12:09 AM
  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, 05:20 PM
  5. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM