3

I'm with this doubt: how to get the size of a char array in this case:

#include<stdio.h>

void f(char * x)
{
printf("Size %d\n", sizeof(x)/sizeof(char));
}

main()
{
char x[5] = {'a', 'e', 'i', 'o', 'u'};
f(&x[0]);
}

Contrary to my expectations, I'm receiving 8 rather than 5 or even 6. What is wrong here?

Thanks!

Leandro Lima
  • 1,140
  • 3
  • 24
  • 42
  • 2
    There is no other option than to pass the size of the array as parameter to the function. – Mahesh Apr 01 '13 at 13:41
  • 1
    [See this](http://stackoverflow.com/questions/12749657/length-of-char-pointer-c) – Suvarna Pattayil Apr 01 '13 at 13:41
  • 1
    Since x is a char pointer, sizeof(x) will return the size of the pointer. – Edward Clements Apr 01 '13 at 13:42
  • 1
    1. `%d` corresponds to an `int` argument. The `sizeof` operator produces a `size_t`, not an `int`. The argument mismatch produces undefined behaviour. Use `%zu`, instead. 2. `sizeof (char)` is *always* one. Dividing `sizeof (x)` by one produces `sizeof (x)`. Clean up your code by removing this unnecessary division-by-one operation. – autistic Apr 01 '13 at 14:23
  • possible duplicate of [C sizeof a passed array](http://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array) – Jens Gustedt Apr 01 '13 at 16:21
  • possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – Jonathan Leffler Apr 13 '13 at 22:40

5 Answers5

5

sizeof(x) in your code will return the size of pointer char *x and not the size of the char array that x is pointing on

and the size of pointer in your 64-bits system is 8. and for 32-bits system the size of pointer is 4

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
3

Here, sizeof() is returning the size of the pointer, not the size of the original array.

The only way for f() to know the size of the array pointed to by the char* is for it to be told by the caller:

void f(char * x, size_t size)
{
   ...
}

main()
{
   char x[5] = {'a', 'e', 'i', 'o', 'u'};
   f(x, sizeof(x) / sizeof(x[0]));
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You can not. sizeof returns the size of a pointer.

Store the size of your array in a variable and pass it too.

I prefer this:

void f(char *x, int size)
{
// ...
}

main()
{
  char x[5] = {'a', 'e', 'i', 'o', 'u'};
  f(x, 5);
}
masoud
  • 55,379
  • 16
  • 141
  • 208
0

You get it like this

void f(char * x, int size_of_array)
{
printf("Size %d\n", size_of_array);
}

main()
{
char x[5] = {'a', 'e', 'i', 'o', 'u'};
f(&x[0], 5);
}

Once you pass an array it decays into a pointer of that type, and you loose the ability to get the size of the array via the sizeof macro. You need to pass the number of elements. If your array is of numeric type you can always pass the size of an array as the first element:

void f(char * x)
{
printf("Size %d\n", x[0]);
}

main()
{
char x[6] = {6, 'a', 'e', 'i', 'o', 'u'};
f(&x[0]);
}    

But of course in this case there's extra overhead to updating that element to make sure it matches what you expect.

Mike
  • 47,263
  • 29
  • 113
  • 177
0

sizeof(x) gives you size of the char pointer not the size of the array. char * is a pointer to a char. If you dochar a = 'A'; f(&a); it is still valid. char * is not designed to point to only char arrays, so sizeof(x) returns size of the pointer and not what it is pointing at.

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59