help:question about sizeof?

G

guoliang

help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)

"
how can i use pstr to get sizeof string which it point?

thx!
 
P

Peter Nilsson

guoliang said:
help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};

char str[] = "Hello\n";
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));

%d will tell printf to expect an int. You supply a size_t
which is unlikely to be a type that promotes to int.

printf("sizeof = %u\n", (int) sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get
sizeof(str) not sizeof(void *)

I suspect you meant sizeof(char *), although void and character
pointers will have the same size.
"
how can i use pstr to get sizeof string which it point?

You can't.

http://c-faq.com/malloc/sizeof.html
 
M

Martin Ambuhl

guoliang said:
help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)

"
how can i use pstr to get sizeof string which it point?

Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant. Since the sizes of
arrays pointed can change during execution, they can hardly be
compile-time constants.
 
S

shaanxxx

guoliang said:
i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)
"
how can i use pstr to get sizeof string which it point?

Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant.
above statement hold true for c89. c99 has variable length array and
it needs run sizeof operator for variable length array.

6.5.3.4 The sizeof operator:
EXAMPLE 3 In this example, the size of a variable length array is
computed and returned from a
function:
#include <stddef.h>
size_t fsize3(int n)
{
char b[n+3]; // variable length array
return sizeof b; // execution time sizeof
}
 
S

shaanxxx

guoliang said:
i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)
"
how can i use pstr to get sizeof string which it point?

Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant.

above statement hold true for c89. c99 has variable length array and
it needs runtime sizeof operator for variable length array.

6.5.3.4 The sizeof operator:
EXAMPLE 3 In this example, the size of a variable length array is
computed and returned from a
function:
#include <stddef.h>
size_t fsize3(int n)
{
char b[n+3]; // variable length array
return sizeof b; // execution time sizeof
 
J

JimS

%d will tell printf to expect an int. You supply a size_t
which is unlikely to be a type that promotes to int.
Agree.

printf("sizeof = %u\n", (int) sizeof(str));

Are you sure about that? %u is for unsigned int.

Jim
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top