Newbie Question: sizeof() problem in function

This is a discussion on Newbie Question: sizeof() problem in function within the C Programming forums, part of the General Programming Boards category; Hi. I'm a first year C student. I'm doing a homework assignment and I'm getting a strange result.
We have ...

Newbie Question: sizeof() problem in function

Hi. I'm a first year C student. I'm doing a homework assignment and I'm getting a strange result.
We have to use an array to display our initials in rows and columns made out of the letters in the initials. My initials are SR, so I need to make a S out of S's and an R out of R's. Then we have to search the array to see how many letters are in the array.

I made a function that would search the array using a for loop and a simple if statement. It will count each time it finds a specific character in the array. Then the function returns the total value.

I want to use sizeof( array ); in my for loop initialization so it knows how far to go, but when I sizeof() inside the function, it only reports a size of "4". When I use sizeof() in my main function, it reports the proper size of "105". Since I'm only using one array, and I know how many elements are in it, I've cheated and just set my for loop to run 105 times.

Anyone know what I'm missing? I have a feeling it's the way I've set up the array, but it's based on the examples we were shown in class on how to initialize arrays. Thanks for the help in advance!

sizeof() doesnt work on an array inside a function as they are passed as a pointer. So, you will always get 4 bytes which is the size of a pointer.
To get the sizeof an array where it is declared, you have to get the size of the entire array and divide it by the sizeof the first element.

Unfortunately, you can't use sizeof() to determine the number of elements in an array which has been passed to a function.

It works as you'd expect in the same function you declare an array. Applying sizeof() to this array

Code:

int array[10];

would yield 10*4 = 40, and if you wanted the number of elements in the array, you could just divide by sizeof(array[0]). (This last step is, of course, unnecessary for arrays of chars.)

But when you pass an array to a function, you're not *really* passing an array. You're really passing a pointer. You can see this by noting that

Code:

void function(char data[]);
void function(char *data);

are the same as far as the compiler is concerned. Passing entire arrays on the stack as parameters would be horribly inefficient, so you really just pass a pointer to the first element of the array . . . which is fine in most cases, but it can have unexpected results.

You'll probably already have noticed that if you change an array in a called function, the changes are reflected in the caller. And it also means that when you apply sizeof() to an array which is passed into a function, you're really getting the size of a pointer (usually 4 on a 32-bit system).

The solution is that you must pass in the size of the array to the function. If you made your function, for example,

Code:

int searchArray( char search[], int elements, char key ) {

your loop could loop up to the elements variable and it would work as expected. In main(), of course, you could use sizeof() to determine the number of elements in the array, since the array is declared in main()'s stack space and so applying sizeof() to it will have the result you expect.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell