.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   sizeof of argument array might be suprising... (http://forums.bots-united.com/showthread.php?t=6411)

ghost of evilspy 17-07-2007 17:30

sizeof of argument array might be suprising...
 
Just something I wasn't aware of...

Code:

#include <stdio.h>

int test_array1[] = { 1,2,3,4,5,6, };

int func_test_array3(int test_array3[6])
{
        return(sizeof(test_array3));
}

int func_test_array5(int len)
{
        int test_array5[len];
       
        return(sizeof(test_array5));
}

int main(void)
{
        int test_array2[6];
        int test_array4[] = {1,2,3,4,5,6,};
       
        printf("sizeof(test_array1): %d\n", sizeof(test_array1));
        printf("sizeof(test_array2): %d\n", sizeof(test_array2));
        printf("sizeof(test_array3): %d\n", func_test_array3(test_array2));
        printf("sizeof(test_array4): %d\n", sizeof(test_array4));
        printf("sizeof(test_array5): %d\n", func_test_array5(6));
       
        return(0);
}

Code above outputs:
sizeof(test_array1): 24
sizeof(test_array2): 24
sizeof(test_array3): 4
sizeof(test_array4): 24
sizeof(test_array5): 24

Whistler 18-07-2007 15:22

Re: sizeof of argument array might be suprising...
 
the arrays in parameters are the same as pointers, so that's why you got result like that. :)

i.e.,

int func_test_array3(int test_array3[6])

is 100% the same as:

int func_test_array3(int *test_array3)

@$3.1415rin 18-07-2007 17:10

Re: sizeof of argument array might be suprising...
 
yep. ... I should reload before posting


All times are GMT +2. The time now is 18:04.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.