29

I have some code as follows:

int i=0;
char a[7]={0x00,0xdc,0x01,0x04};
int len=0;
len = sizeof(a);
printf("The Length is : %d", len);

Here I want to find the length of the array a in c? How can this be done?

5
  • Is, ur code returning any errors? Commented May 27, 2011 at 9:24
  • NO it is just showing 7 as answer.. Commented May 27, 2011 at 9:25
  • 2
    I think you want strlen(), but because your first character is 0, you will get 0. Commented May 27, 2011 at 9:26
  • 3
    You have declared your array as a[7] so obvioulsy sizeof would return 7. Commented May 27, 2011 at 9:28
  • yes, don't use sizeof, rather debug it starting from 1 val, u'll c empty values after 4 Commented May 27, 2011 at 9:32

6 Answers 6

36

By convention C strings are 'null-terminated'. That means that there's an extra byte at the end with the value of zero (0x00). Any function that does something with a string (like printf) will consider a string to end when it finds null. This also means that if your string is not null terminated, it will keep going until it finds a null character, which can produce some interesting results!

As the first item in your array is 0x00, it will be considered to be length zero (no characters).

If you defined your string to be:

char a[7]={0xdc,0x01,0x04,0x00};

e.g. null-terminated

then you can use strlen to measure the length of the string stored in the array.

sizeof measures the size of a type. It is not what you want. Also remember that the string in an array may be shorter than the size of the array.

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

2 Comments

The length of the array is 7. Zero is the length of the string which is stored in the array.
I think it's clear that he wants to know the length of the string not the array, as he knows that the length of the array is 7 (see his question + comments) and considers that 'wrong'. I was careful to say that strlen gives the length of the string, but I've added clarification. I think the question arises out of the fact that he wants to know the length of the string but is confused (hence the question title) because it is not equal to the length of the array.
4

If you are expecting 4 as output then try this:

char a[]={0x00,0xdc,0x01,0x04};

Comments

4

sizeof returns the size of the type of the variable in bytes. So in your case it's returning the size of your char[7] which is 7 * sizeof(char). Since sizeof(char) = 1, the result is 7.

Expanding this to another example:

int intArr[5];
printf("sizeof(intArr)=%u", sizeof(intArr));

would yield 5 * sizeof(int), so you'd get the result "20" (At least on a regular 32bit platform. On others sizeof(int) might differ)

To return to your problem:

It seems like, that what you want to know is the length of the string which is contained inside your array and not the total array size.

By definition C-Strings have to be terminated with a trailing '\0' (0-Byte). So to get the appropriate length of the string contained within your array, you have to first terminate the string, so that you can tell when it's finished. Otherwise there would be now way to know.

All standard functions build upon this definition, so if you call strlen to retrieve the str ing len gth, it will iterate through the given array until it finds the first 0-byte, which in your case would be the very first element.

Another thing you might need to know that only because you don't fill the remaining elements of your char[7] with a value, they actually do contain random undefined values.

Hope that helped.

Comments

1

You can try this:

   char tab[7]={'a','b','t','u','a','y','t'};
   printf("%d\n",sizeof(tab)/sizeof(tab[0]));

Comments

0

You can do len = sizeof(a)/sizeof(*a) for any kind of array. But, you have initialized it as a[7] = {...} meaning its length is 7...

3 Comments

sizeof(a) gives you the size of the pointer, not the array.
I think it gives you size of array in this case. If the array was created using new, it gives you size of pointer
Yes, don't know why I wrote my previous comment. :)
0

If anyone is looking for a quick fix for this, here's how you do it.

while (array[i] != '\0') i++;

The variable i will hold the used length of the array, not the entire initialized array. I know it's a late post, but it may help someone.

1 Comment

this will return 0 (zero), because the first byte in a is zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.