473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to take the size of a pointer to array.

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

Aug 30 '06 #1
22 1640
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 30 '06 #2

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"

Aug 30 '06 #3
Chad wrote:
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"
strlen("apple"); ??????????????????????????
Aug 30 '06 #4

Chad wrote:
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;
}

Aug 30 '06 #5

bw*****@yahoo.com wrote:
Chad wrote:
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.

Aug 30 '06 #6
Chad wrote:
>
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*.

--
Ian Collins.
Aug 30 '06 #7
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".
Chad wrote:
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"
Aug 30 '06 #8
th***********@gmail.com wrote:
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".
Please don't top post.
Chad wrote:
>>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"


--
Ian Collins.
Aug 30 '06 #9
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.
...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? :=)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 30 '06 #10
On 29 Aug 2006 19:06:09 -0700, "bw*****@yahoo.com" <bw*****@yahoo.com>
wrote in comp.lang.c:
>
Chad wrote:
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.
return 0;
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 30 '06 #11
sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :
th***********@gmail.com wrote:
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".

Please don't top post.
Chad wrote:
>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"

--
Ian Collins.
Aug 30 '06 #12
Joe Wright said:
Richard Heathfield wrote:
>Chad said:
<snip>
>>>
I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6).
<snip>
>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...

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 30 '06 #13

Jack Klein wrote:
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!

Aug 30 '06 #14
Sjouke Burry wrote:
Chad wrote:
>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
<snip>
>>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"); ??????????????????????????
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".
--
Flash Gordon
Aug 30 '06 #15
th***********@gmail.com wrote:
sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :
th***********@gmail.com wrote:
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".
Please don't top post.

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
Aug 30 '06 #16
th***********@gmail.com said:
sorry sir; i'm used to writing e-mails.
Ian Collins a écrit :
>th***********@gmail.com wrote:
hi cdalten ! i'm not used to you using nice words like "apple" and
"orange".

Please don't top post.
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 30 '06 #17
On Tue, 29 Aug 2006 21:41:38 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
>On 29 Aug 2006 19:06:09 -0700, "bw*****@yahoo.com" <bw*****@yahoo.com>
wrote in comp.lang.c:
>>
Chad wrote:
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
--
jay
Aug 30 '06 #18
Chad wrote:
>
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
--
pete
Aug 30 '06 #19

jaysome wrote:
On Tue, 29 Aug 2006 21:41:38 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
On 29 Aug 2006 19:06:09 -0700, "bw*****@yahoo.com" <bw*****@yahoo.com>
wrote in comp.lang.c:
>
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.

Aug 30 '06 #20
th***********@gmail.com writes:
sorry sir; i'm used to writing e-mails.
Read <http://www.caliburn.nl/topposting.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 30 '06 #21
"bw*****@yahoo.com" <bw*****@yahoo.comwrites:
jaysome wrote:
>On Tue, 29 Aug 2006 21:41:38 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
>On 29 Aug 2006 19:06:09 -0700, "bw*****@yahoo.com" <bw*****@yahoo.com>
wrote in comp.lang.c:
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".
size_t is guaranteed to be an unsigned type. The most portable way to
print a size_t value is to cast it to unsigned long and use "%lu". By
"most portable", I mean that it works in C90, which doesn't have
"%zu".
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.
But if size_t is *smaller* than unsigned long, it still invokes
undefined behavior.
The two correct ways to cast this thing would have been:

pre-c99:

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

c99:

printf("%zu", string_len);
Right. But it's not uncommon to have a compiler that supports most of
C99, but a runtime library whose printf() implementation doesn't
recognize "%zu". (The compiler and the C library are sometimes
provided by different vendors.)
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.
This:

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

doesn't actually assume anything about the underlying type for size_t.
It's guaranteed to work if size_t is no bigger than unsigned long.
For example, if unsigned int and unsigned long are 32 and 64 bits,
respectively, and size_t is unsigned int, the above will still work.

In C90, unsigned long is the largest unsigned integer type, so the
printf above cannot fail. In C99, it's possible that size_t could be
unsigned long long (which *might* be bigger than unsigned long), or it
could be some extended integer type (though implementers are *advised*
to make size_t no bigger than unsigned long). Even in that unusual
case, the above will cause problems only if the value of string_len
happens to exceed ULONG_MAX (which is at least 4294967295), and even
then the conversion from size_t to unsigned long yields a well-defined
(but numerically incorrect) result.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 30 '06 #22
"Chad" <cd*****@gmail.comwrites:
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?
You're missing the fact that a pointer doesn't store the size of what
it points to.

If a pointer of type foo* points to a single object of type foo, then
you can obviously get the size of the single pointed-to object with
"sizeof *ptr". But if the pointer points to the first element of an
array of foo, there's no direct way to determine the size of the array
given the pointer.

If the array somehow encodes its own size, you can use that. For
example, if it's a string you can use strlen() (and add 1 to account
for the trailing '\0'). But that's not *necessarily* the allocated
size. For example:

char s[100] = "apple";
char *p = s;

"strlen(p) + 1" gives you 6, but the actually allocated size of the
array is 100 bytes.

In general, you just have to keep track of it yourself. If you
allocated the array with malloc(), for example, you knew the size when
you allocated it; just don't forget that information.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 30 '06 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
29
by: Vasileios Zografos | last post by:
Hi everyone, I need to build a function to plug it in a program (that I didnt make or can change) that should be called something like this: float someFunction(float x) { ...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.