rtucan February 2016

difference between strlen(string) and strlen( *string)

Let's say I have an array of strings that are all of same size.

char strings[][MAX_LENGTH];

what would be the difference between strlen(strings) and strlen(*strings)?

I know that strings by itself would be the address of the first string in the array,

but what is *strings?

Answers


David Hoelzer February 2016

First, don't do this. C will allow you to do lots of things that are a bad idea. This doesn't mean you ought to do it. :)

While you may have compiler warnings, these two are effectively identical. The reason is that with this definition:

char strings[][MAX_LENGTH];

The allocation for this will end up being one continuous block. Within that block of memory, there are no "structures" or management devices that can be used to identify where individual strings start and stop. This creates an interesting situation.

Effectively, *string and string are both pointers to precisely the same memory location. This means that calling strlen on either one of them will return the null delimited string length of the first element in the first array.

However, I must reiterate... Don't do this.

Post Status

Asked in February 2016
Viewed 2,513 times
Voted 11
Answered 1 times

Search




Leave an answer