Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding length of char array

Tags:

arrays

c

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?

like image 514
Nikhil Avatar asked May 27 '11 09:05

Nikhil


People also ask

How do you find the length of an unsigned char array?

size_t size = sizeof(unsigned char*); If you're wanting to know how many elements does the pointer point to, that's a bit more complex. If this is a C style string then strlen or some variant is your best option.

What is length of char in C?

printf ( "Length of String is %lu\n" , strlen (str)); printf ( "Size of String is %lu\n" , sizeof (str)); } Output: Length of String is 8 Size of String is 9. Since size of char in C is 1 byte but then also we find that strlen() gave one less value than sizeof().

What is the length of char?

length is an unsigned integer literal designating the length in bytes. The default length for a CHAR is 1, and the maximum size of length is 254.


2 Answers

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.

like image 170
Joe Avatar answered Oct 12 '22 00:10

Joe


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.

like image 23
lx. Avatar answered Oct 11 '22 22:10

lx.