the sizeof of the array decrease-why [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why sizeof(param_array) is the size of pointer?
void print(char arr[]){
int i;
printf("%d" , sizeof(arr)); /*print 4**/
}
int main()
{
char arr[]={0,1,2,3,4};
printf("%d" , sizeof(arr)); /*print 5**/
print(arr);
}
When I send the array to the function it seems that the size decrease in 1. What happen?

Because you don't really have an array in the function; it has degraded into a pointer and the size is unknown. You are asking for the sizeof a char* which, on your platform, is 4 bytes.

Related

Why on running below program , output of sizeof(a) in void print_size(int a[ ]) function is 8 whereas in int main( ) function sizeof(a) is 12? [duplicate]

This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
Behaviour of Sizeof in C
(7 answers)
Closed 2 years ago.
This program is about finding size of integer array and its elements size ,printing the size of
array a[] in function void print_size(int a[]) ,it is showing sizeof(a) as 8 whereas in int main()function sizeof(a) is 12. How is the output
for sizeof(a) function given in void print_size(int a[])is 8 but in int main() function sizeof(a) is 12 ,on running the program .
Here is code given below:
#include <stdio.h>
void print_size(int a[])
{
printf("%d %d ",sizeof(a),sizeof(a[3]));
}
int main()
{
int a[]={1,2,3};
printf("%d %d \n",sizeof(a),sizeof(a[-1]));
print_size(a);
return 0;
}

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.

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.

C string array sizeof() changes [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
void func(char *s[]){
printf("s: %d\n", sizeof(s));
}
void caller(){
char *a[2];
for(int i = 0; i < 2; i++){
a[i] = (char *)malloc(50 * sizeof(char));
}
strcpy(a[0], "something");
strcpy(a[1], "somethingelse");
printf("a: %d\n", sizeof(a));
func(a);
}
This outputs
a: 16
s: 8
Why is the output of sizeof() different in caller() and func()? Also, is it possible for func to get the number of char * in the array via sizeof?
One is an array of two character pointers. The other is a pointer to an array of pointers. They're not the same size.
In C/C++,Array decays into a pointer.char *s[] is a pointer, the size of pointer is always fixed 4 or 8 (depends upon machine).

Resources