0

Possible Duplicate:
How to get the size of dynamically allocated 2d array

I can't find what I did wrong here. I want to create an array which its size is based on user input, get the data (integers) for the array from user input, and print the array integers.

The problem is that it only prints the first array element, i.e intArr[0].

int main()
{
    int i, n, *intArr;

    printf("Type the array size:\t");
    scanf("%d", &n);

    intArr = (int *)malloc(n * sizeof(int));

    for (i = 0; i < n; ++i)
    {
        printf("Type a number\t");
        scanf("%d", intArr + i);
    }

    printArr(intArr);
}

void printArr(int *arr)
{
    int i; 
    for (i = 0; i < (sizeof(arr) / sizeof(*arr)); ++i)
        printf("%d ", *(arr + i));
}
Community
  • 1
  • 1
tempy
  • 897
  • 3
  • 14
  • 22
  • This question is relevant as well: [**How to find the sizeof(a pointer pointing to an array)**](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) - I didn't know it's impossible to find the size of an array based on a pointer pointing to it. – tempy Oct 04 '12 at 08:06

3 Answers3

3

The type of arr is an int* so sizeof(arr) will be the sizeof(int*) and not the number of elements in arr. On your system sizeof(int*) and the sizeof(int) is the same, giving 1 as the result so the loop prints one element only.

Pass the number of elements as an argument to the printArr() function.

Note:

  • free() what you have malloc()d.
  • casting return value of malloc() is unnecessary.
  • check return value of scanf() to ensure successful.
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 1
    Can you explain to me why `sizeof(arr)` in the `main()` returns the size of the array, but when I pass it to `printArr()` I can't get the size of the array? I thought it should because it points to the first element in the array. – tempy Oct 04 '12 at 07:59
  • 1
    The posted code does not use `sizeof()` anywhere in `main()`. When an array is passed to a function it decays to a pointer to its first element so the type of the argument is `T*` where `T` is the type of array element. There is no additional information stored in a pointer variable to indicate whether is pointing to a single `T` or an array of `T`. – hmjd Oct 04 '12 at 08:02
1

You cannot pass an array through to a function as a pointer, it will lose the information about the size when the array decays into a pointer to its first element.

Pass the size as an extra, explicit, argument.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Why does it lose this information? this is what I don't get. I thought that it would recognize it being a pointer to a first element of an array. – tempy Oct 04 '12 at 08:01
0

You are not passing the number of arguments to your printArr() function, and there is no way it can know this. I would pass the number of elements as another parameter.

The type of "arr" is a pointer to an integer. Most likely 4 bytes, depending on the computer architecture. Same size for "*arr".

You could try:

printf("%d \n", (int) sizeof(arr));
printf("%d \n", (int) sizeof(*arr));
printf("%d \n", (int) sizeof(arr) / sizeof(*arr));

and would see output like this:

4
4
1

Therefore you are doomed to execute the print loop only once.

elomage
  • 4,334
  • 2
  • 27
  • 23
  • 1
    `%d` is for `int`, but you're passing `size_t`. Either use a cast to `int` or use `%zu`. – Alexey Frunze Oct 04 '12 at 08:17
  • @AlexeyFrunze - good point. For the values involved this did not change the point, however for a quality code one should use the types as Alexey suggested. I'll edit my answer accordingly. – elomage Oct 04 '12 at 08:24
  • @tempy `z` is a modifier for `size_t`, `u`, as usual, is unsigned decimal integer. – Alexey Frunze Oct 04 '12 at 08:42