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).
Related
This question already has answers here:
Sizeof arrays and pointers
(5 answers)
Closed 6 years ago.
#include<stdio.h>
#include<stdlib.h>
int main (int argc, char *argv[]) {
int* arr1 = (int*)malloc(sizeof(int)*4);
int arr2[4];
printf("%d \n", sizeof(arr1));
printf("%d \n", sizeof(arr2));
free(arr1);
return 0;
}
Output
8
16
Why?
Arrays are not pointers.
In your code, arr1 is a pointer, arr2 is an array.
Type of arr1 is int *, whereas, arr2 is of type int [4]. So sizeof produces different results. Your code is equivalent to
sizeof (int *);
sizeof (int [4]);
That said, sizeof yields the result of type size_t, so you should be using %zu to print the result.
arr1 is a pointer when you call sizeof(arr1) this will you size of pointer.
but arr2 represent block of memory for 4 integers.
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]);
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.
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 *)`
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.