How to take the size of a pointer to array.

C

Chad

Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

Chad
 
R

Richard Heathfield

Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.
 
C

Chad

Richard said:
Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.

Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"
 
S

Sjouke Burry

Chad said:
Richard said:
Chad said:

Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.


Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"
strlen("apple"); ??????????????????????????
 
B

bwaichu

Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

Chad

Fortunately, there is the ansi string library. And since the C strings
nul terminate, you can try this:

#include <stdio.h>
#include <string.h>

int
main(void) {

size_t string_len;
char *msg_list[] = {" apple", " orange", " grape" };

string_len = strlen(msg_list[0]);
(void)printf("1st element is %ld char long", string_len);

return 0;
}
 
C

Chad

Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

Chad

Fortunately, there is the ansi string library. And since the C strings
nul terminate, you can try this:

#include <stdio.h>
#include <string.h>

int
main(void) {

size_t string_len;
char *msg_list[] = {" apple", " orange", " grape" };

string_len = strlen(msg_list[0]);
(void)printf("1st element is %ld char long", string_len);

return 0;
}

Hmm.... interesting. The questions stems from some code I wrote for a
local bbs sytem
that runs OpenBSD. I had asked the site administrator the same
question and he told
me if I had used the constant qualifer in front of

char *msg_list[] = {" apple", " orange", " grape" };

it would give me what I was looking for. It didn't. I then used
strlen() to get the
correct results. For whatever reasons, it seemed like a hack. Hence I
decided to post
the question here. I see some of the others also told me to use
strlen().

I forgot where this was going.
 
I

Ian Collins

Chad said:
Hmm.... interesting. The questions stems from some code I wrote for a
local bbs sytem
that runs OpenBSD. I had asked the site administrator the same
question and he told
me if I had used the constant qualifer in front of

char *msg_list[] = {" apple", " orange", " grape" };

it would give me what I was looking for. It didn't. I then used
strlen() to get the
correct results. For whatever reasons, it seemed like a hack. Hence I
decided to post
the question here. I see some of the others also told me to use
strlen().
The advice to use const is correct, but for the wrong reason. You have
an array of string literals, which should be const char*.
 
T

thisdayislong

hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".
Chad said:
Richard said:
Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.

Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"
 
I

Ian Collins

hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".

Please don't top post.
Chad said:
Richard said:
Chad said:


Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.

Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"
 
J

Joe Wright

Richard said:
Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.

...and without regard to msg_list. Chad would want strlen(msg_list[0]) to
get 6 as length of " apple", a seven byte array of char.

Welcome back Richard. Out of work again? :=)
 
J

Jack Klein

Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

Chad

Fortunately, there is the ansi string library. And since the C strings
nul terminate, you can try this:

#include <stdio.h>
#include <string.h>

int
main(void) {

size_t string_len;
char *msg_list[] = {" apple", " orange", " grape" };

string_len = strlen(msg_list[0]);
(void)printf("1st element is %ld char long", string_len);

This of course produces undefined behavior, unless of course size_t is
typedef'ed to unsigned long on your platform. There is, of course, no
guarantee that it is. It could just as easily be unsigned int or
unsigned long long.

Of course, you could use a cast: (long)string_len, in the printf()
call.

And casting the return value of a function call to void is just plain
silly. If you do this because of a static checking tool that cannot
be configured to ignore this case, you need a better tool.
 
T

thisdayislong

sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".

Please don't top post.
Chad said:
Richard Heathfield wrote:

Chad said:


Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.


Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"
 
F

Flash Gordon

Sjouke said:
Chad said:
Richard said:
Chad said:


Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

msg_list is an array of three pointers-to-char. On your platform, it
Is there anyway to get the size of apple from this construction without
having to type sizeof "apple"
strlen("apple"); ??????????????????????????

You forgot the null termination, strlen will only return 5. So to get
the size of the unnamed array it is "strlen(msg_list[0])+1".
 
R

Richard Heathfield

Joe Wright said:
Richard said:
Chad said:
sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get,
regardless of the platform.

..and without regard to msg_list. Chad would want strlen(msg_list[0]) to
get 6 as length of " apple", a seven byte array of char.

But the string literal in question was "apple", not " apple", and he wanted
its size, not its length.
Welcome back Richard. Out of work again? :=)

I didn't actually leave, as such, at least not by choice. Do you remember I
mentioned my Linux box suffered a near-miss, which prompted me to back up
for the first time this millennium? Well, the machine did in fact die the
next day, and I've been using a W... I've been using a Wi... a Wi... a W...
a not-Linux machine for the last couple of weeks. Curiously, no matter how
hard I kicked it, I couldn't get it to talk to my router. Grrr. Anyway, the
Linux box has been resurrected. My hardware chap tells me the hard disk
wasn't plugged in properly, and the CPU had the wrong heat sink, etc etc.
He's sorted it all out, and removed a couple of hundredweight of dust. He
also gave me a stern lecture on ventilation. But all is now well with the
world, for I am once more using God's own operating system. Linus shoulda
called it that. Goosy for short.

As for my workload, I'm afraid my in-tray is piled as high as ever it was,
and shows little sign of diminishing. It seems that any time I get anything
done, two more tasks appear from nowhere...
 
B

bwaichu

Jack said:
Of course, you could use a cast: (long)string_len, in the printf()
call.

True. I should have typecast the string_len in printf().
And casting the return value of a function call to void is just plain
silly. If you do this because of a static checking tool that cannot
be configured to ignore this case, you need a better tool.

This is more from habit. I am not advocating anyone do this.

I love how folks here keep me on my toes. One day, I hope, to code
good enough to make it through the clc lint.

And it should be the string length plus one to count the nul terminator
to give the the actual number of bytes -- assuming that char is all
ways
one byte.

Thanks!
 
D

Default User

sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :


Top-posting means posting your message over the quotes. Don't do that.
Trim the quotes and put your text following or interspersed with the
quotes. See almost every other message here.




Brian
 
R

Richard Heathfield

(e-mail address removed) said:
sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :

And please don't send me emails, either. Top-posting is just as stupid
stupid stupid in email as it is on Usenet, and if you're too stupid to work
out how to move the insertion point in your poorly-configured email
client's reply window, you're too stupid to be using a computer, let alone
be sending messages to people.
 
J

jaysome

Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I'm missing here?

Chad

Fortunately, there is the ansi string library. And since the C strings
nul terminate, you can try this:

#include <stdio.h>
#include <string.h>

int
main(void) {

size_t string_len;
char *msg_list[] = {" apple", " orange", " grape" };

string_len = strlen(msg_list[0]);
(void)printf("1st element is %ld char long", string_len);

This of course produces undefined behavior, unless of course size_t is
typedef'ed to unsigned long on your platform.
^^^^^^^^^^^^^
Don't you mean "signed long" or just plain old "long"? The conversion
specifier for "unsigned long" is "%lu", not "%ld".
Of course, you could use a cast: (long)string_len, in the printf()
call.

But what happens if string_len > LONG_MAX? Is that undefined behavior?
And casting the return value of a function call to void is just plain
silly. If you do this because of a static checking tool that cannot
be configured to ignore this case, you need a better tool.

Indeed. Such a tool might implement something like this:

-esym(534,memset, strcat, strcpy, fprintf, sprintf, memcpy, printf,
fgets, sscanf, fflush, vsprintf)

Best regards
 
P

pete

Chad said:
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6).

That's strange, because the subject line of your thread is
"How to take the size of a pointer to array.",
which is what your program already does.
Instead, I get the following:

name: apple
size: 12

What I'm missing here?

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
char msg_list[][sizeof " strawberry"] =
{" apple", " orange", " grape",
" banana", " strawberry", " lemon"};
unsigned n = sizeof msg_list / sizeof *msg_list;

while (n-- != 0) {
printf("name: %s \n", msg_list[n]);
printf("size: %d \n", sizeof msg_list[n]);
printf("length: %u\n\n", (unsigned)strlen(msg_list[n]));
}
return 0;
}

/* END new.c */

name: lemon
size: 12
length: 6

name: strawberry
size: 12
length: 11

name: banana
size: 12
length: 7

name: grape
size: 12
length: 6

name: orange
size: 12
length: 7

name: apple
size: 12
length: 6
 
B

bwaichu

jaysome said:
Chad wrote:
Given the following code

string_len = strlen(msg_list[0]);
(void)printf("1st element is %ld char long", string_len);

This of course produces undefined behavior, unless of course size_t is
typedef'ed to unsigned long on your platform.
^^^^^^^^^^^^^
Don't you mean "signed long" or just plain old "long"? The conversion
specifier for "unsigned long" is "%lu", not "%ld".

I did not cast it knowing that size_t is defined on my box as unsigned
long.
And since I could just see the size of the object, I didn't bother. I
knew it wasn't going to exceed the size of signed long. I was lazy.

The two correct ways to cast this thing would have been:

pre-c99:

printf("%lu", (unsigned long)string_len);

c99:

printf("%zu", string_len);

The odd thing about the pre-c99 cast is that we have to assume a type
for size_t. This is where this argument gets odd. The compiler I use
isn't completely c99 compliant, so I cannot use the c99 example above.
However, it's difficult to argue that size_t will not all ways be
unsigned long because you would have to change your printf typecasts to
make your code portable between architectures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top