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!

6
  • 2
    There is no other option than to pass the size of the array as parameter to the function. Commented Apr 1, 2013 at 13:41
  • 1
    See this Commented Apr 1, 2013 at 13:41
  • 1
    Since x is a char pointer, sizeof(x) will return the size of the pointer. Commented Apr 1, 2013 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. Commented Apr 1, 2013 at 14:23
  • possible duplicate of C sizeof a passed array Commented Apr 1, 2013 at 16:21

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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]));
}

Comments

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);
}

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.