Different array sizes [duplicate] - c

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 9 years ago.
When I run the following program, I get different array sizes. I tired different ways but the result is the same, what could io be doing wrong ?
#include<stdio.h>
void array_size(char *a[])
{
printf("Func Array Size: %d\n", sizeof(a));
}
int main()
{
char *str_array[]={"one", "two", "three"};
printf("Array Size: %d\n", (int)sizeof(str_array));
array_size(str_array);
return 0;
}

In function main str_array is an array with three char *.
The parameter a of function array_size is just a pointer. The compiler does not dynamically pass array length information when calling array_size.
The size of one pointer is not equal the size of three char * pointers.

This is because sizeof is a compiler built-in and not a runtime function. It is hard-coded into the binary.
`sizeof((char *)[]) = sizeof(a) = sizeof(void *)`
`sizeof(str_array) = sizeof({"one", "two", "three"}) = 3 * sizeof(char *)`

Related

count array length in c by pointer, the answer is wrong [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 6 years ago.
#include <stdio.h>
int a[3] = {1,2,3};
void cal(int* item){
printf("Length(inside) is %lu\n", sizeof(item)/sizeof(item[0]));
}
int main(){
printf("Length(outside) is %lu\n", sizeof(a)/sizeof(int));
cal(a);
}
The code above comes out with the result that
Length(outside) is 3
Length(inside) is 2
However, it is wrong, because both of them should be 3, what's wrong with my code?
First of all, you should be using %zu fomat specifier to print the result.
Secondly, when passed to a function as argument, array name decays to the pointer to the first element, so the sizeof(item) will not work as expected.
Basically what you're doing is sizeof (int *)/ sizeof (int) and in your environment, it is likely that a pointer occupies 8 bytes and an int takes 4, so you get this result.
This is because the array decays into a pointer when it is passed to a function.
So essentially what you are doing is
sizeof(int*)/sizeof(int);
What you can think is the difference between
int a[5];
sizeof(a)/sizeof(a[0]); //this works
and
int a[5];
int* p = a;
sizeof(p)/sizeof(p[0]);

Array size in functions [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 8 years ago.
I'm working with some arrays in plain C99 and I need to know their size. Does anyone knows why this is happening:
int function(char* m){
return sizeof(m) / sizeof(m[0]);
}
int main(){
char p[100];
int s = sizeof(p) / sizeof(p[0]);
printf("Size main: %d\nSize function: %d\n",s,function(p));
return 0;
}
Output:
Size main: 100
Size function: 8
Thanks!
Arrays decay to pointers at the first chance they get, thus in function all you have is a pointer, which is 8 bytes in your case.
This is happening because sizeof(m) == sizeof(char*), which is 8 on your platform.
When you call function(p), p decays into a pointer and loses its size information.

Why is the sizeof function returning to different values for the same array? [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 9 years ago.
Consider this piece of code (or just copy/paste and run it):
#include <stdio.h>
int array[] = {1, 2, 3, 4, 5, 6, 7};
int my_put(int *array)
{
printf("Size of array %lu\n", sizeof(array));
return 0;
}
int main(int argc, char **argv)
{
printf("Size of array %lu\n", sizeof(array));
my_put(array);
return 0;
}
My question is: How come that the sizeof function returns two different values? Why is the size 28 in the main function and 8 in the my_put function?
Because in my_put(), array is an int * pointer, not an array. Your parameter shadows the global array variable. It might be clearer if you rewrite my_put to look like this:
int my_put(int *a)
{
printf("Size of array %zu\n", sizeof array);
printf("Size of a %zu\n", sizeof a);
return 0;
}
This new function doesn't confuse two variables named array. I also fixed it to use the z format specifier for size_t types.
Plenty more at the comp.lang.c FAQ, section 6, Arrays and Pointers, particularly Question 6.21 for your case.
In my_put name array is shadowed by your func's arg and its size is 8 (since it is a pointer). In main you get size of your 'real' array. That's all
In function my_put , array is pointer and its size is of the size of int *. Read C-FAQ 6.21.
Because its not the same "array". You have two arrays here -- the global variable "array" and the argument "array" to my_put. Now the second one is actually a pointer that points at the first one, but its a distinct variable with a different type (int pointer vs int array), so it has a different size.

how to sizeof a parameter of a function in C? [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 9 years ago.
I have the following code in c:
void getNumber(int numberArray[])
{
printf("The size of the array is %u", sizeof(numberArray));
}
void main()
{
int array[6] = {1,2,3,4,5,6};
getNumber(array);
}
but why the The size of the array is 4? And how to get the size of whole array?
void getNumber(int numberArray[])
is equivalent to
void getNumber(int* numberArray)
so sizeof(numberArray) is just measuring the size of int*.
If you want to know the length of the array, you either need to pass its length as a separate argument
void getNumber(int* numberArray, int len)
getNumber(array, sizeof(array)/sizeof(array[0]));
or arrange for the array to end with a known sentinel value
int array[] = {1,2,3,4,5,6,-1}; // assumes -1 is not a valid value
int arrayLength(int* numberArray)
{
int len=0;
while (*numberArray++ != -1) {
len++;
}
return len;
}
This is not possible, except if you're using C99 and the array is a variable-length array.
In general an array is very much like a pointer in C, and even more so when being passed to a function.
In case of getnumber(array) your array name array decays to pointer and is giving its size 4. In case of sizeof, an array name doesn't decays to pointer and hence giving the size of entire array.

differnt size is displayed when passed from main to a function [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
How to reliably get size of C-style array?
(10 answers)
Closed 9 years ago.
#include <stdio.h>
#include <string.h>
int fashion (int[]);
main()
{
int a[]={3,2,5,1,3};
int size;
size= sizeof a/sizeof (int);
printf("size of array %d\n",sizeof(a)); //size of the array
printf("size of int %d\n",sizeof(int)); //size of the int
printf("lenght of array %d\n",size); //actual length of the array
fashion(a);
return 0;
}
int fashion(int input1[]) //tried with int fashion(int *input1)
{
int size;
size= sizeof input1/sizeof (int);
printf("\nin function\n");
printf("size of array %d\n",sizeof(input1)); //size of the array
printf("size of int %d\n",sizeof(int)); //size of the int
printf("lenght of array %d\n",size); //actual length of the array
}
Below is the output of the code:
output is
size of array 20
size of int 4
lenght of array 5
In function
size of array 8
size of int 4
lenght of array 2
Both the code in main function and function called are same but resulting different results.
Why the size of the array is changed in main function it is 20 and in the function it is 8?
who can i make both the results same?
I even tried with Fashion(int input1[]) but resulting in same.
This has to do with different typing. sizeof is a compiler operator, not a runtime function.
a is of type int[5], which correctly leads to a size of 5*4 = 20.
input1 is of type int * which has the same size as a void *. sizeof(int *) = sizeof(void *) is normally 4 on on 32-bit systems and 8 on 64-bit systems (which yours seems to be).
Generally when passing arrays to functions you pass the pointer to the first element (as in your function) and additionally the length of the array as a separate argument.

Resources