Pointers and their length

I'm trying to write a simple C program, though for some reason, I'm having a problem getting the array's size

I know I need to use sizeof, but it doesnt work inside the function :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printArr(int arr [])
{
	for (int i = 0 ; i < n ; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int c[6] = {1, 3, 2, 5, 0, 7};
	int n = sizeof(c)/sizeof(int);
	printArr(c);

	getchar();
	getchar();
}


in the main, n will be 6, though inside the function printArr, n will be 1. why is that?
Line 1 is equivalent to
 
void printArr(int* arr)
so the problem is that sizeof(arr) will give you the size of the pointer (usually 4 or 8 bytes).

There is no way you can know the array size from just the pointer. You will have to pass the size as an additional argument to the function.
in the main, n will be 6, though inside the function printArr, n will be 1. why is that?
It should be undefined and not compile since it is not passed to the function.
Thank you guys! I thought adding a size paramter is a bad programming, because you could check it urself at the function!
Topic archived. No new replies allowed.