Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]

Complete example:

#include <stdio.h>  void test(int arr[]) {     int arrSize = (int)(sizeof(arr) / sizeof(arr[0]));     printf("%d\n", arrSize); // 2 (wrong?!) }  int main (int argc, const char * argv[]) {     int point[3] = {50, 30, 12};      int arrSize = (int)(sizeof(point) / sizeof(point[0]));     printf("%d\n", arrSize); // 3 (correct :-) )      test(point);      return 0; } 

Before passing it to a function, sizeof gives me the correct value. Doing the exact same thing on the exact same array in the function gives weird results. There's one element missing. Why?

like image 348
dontWatchMyProfile Avatar asked Jun 01 '10 13:06

dontWatchMyProfile


People also ask

How does sizeof work on arrays C?

sizeof() Operator to Determine the Size of an Array in CIt returns the size of a variable. The sizeof() operator gives the size in the unit of byte. The sizeof() operator is used for any data type such as primitives like int , float , char , and also non-primitives data type as an array , struct .

Can size of array be changed in C?

Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.

Why do we pass size of array in C?

Passing the array size tells the function where the bounds are so you can choose not to go beyond them. This is inherited from C language, if you use std::array or std::vector you don't need to use array size as a parameter function.

Can you use sizeof on an array?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.

Why sizeof array is 4 and 8 in C?

Because array is decayed to a pointer when passed as function argument, so sizeof gives you 4 and 8 for 32- and 64-bit platforms respectively. Show activity on this post.

How to find the size of an array Using sizeof operator?

Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers. Consider the below program. Explanation: This code generates an error as the function fun () receives an array parameter ‘arr []’ and tries to find out the number of elements in arr [] using sizeof operator.

What happens when you pass an array to a function?

When you pass an array into a function in C, the array decays into a pointer to its first element. When you use sizeof on the parameter, you are taking the size of the pointer, not the array itself.

Why can't a function have an array parameter?

Because in C, C++, and Objective-C, functions cannot actually have array parameters. They only can have parameters that look like array parameters, but they aren't.


1 Answers

When you pass an array into a function in C, the array decays into a pointer to its first element. When you use sizeof on the parameter, you are taking the size of the pointer, not the array itself.

If you need the function to know the size of the array, you should pass it as a separate parameter:

void test(int arr[], size_t elems) {    /* ... */ }  int main(int argc, const char * argv[]) {    int point[3] = {50, 30, 12};    /* ... */    test(point, sizeof(point)/sizeof(point[0]));    /* ... */ } 

Also note that, for a similar reason (taking the sizeof a pointer), the sizeof(point)/sizeof(point[0]) trick doesn't work for a dynamically allocated array, only an array allocated on the stack.

like image 191
Nick Meyer Avatar answered Oct 18 '22 00:10

Nick Meyer