sizeof and pointer

K

kevin

this is my programm
....
char *p="abcde";
char a[]="abcde";
.....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
.....
 
I

Ian Collins

kevin said:
this is my programm
....
char *p="abcde";
char a[]="abcde";
.....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
.....
---------------------------------------------------------
and the output is:

the size of p is:1
the size of a is:6
Are you sure? There aren't many systems around (at least not hosted
ones!) with an 8 bit char*.

p is a char* and a is any array of 6 char, sizeof(char) is 1, so
sizeof(char[6]) is 6.
 
C

Chris Dollin

kevin said:
this is my programm
...
char *p="abcde";
char a[]="abcde";
....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
---------------------------------------------------------
and the output is:

the size of p is:1
the size of a is:6

I don't think the code you posted is the code you ran, unless your
platform is one where sizeof(char)==sizeof(char*), which seems unlikely
but possible.

The literal answer -- assuming that your (incomplete) code is really
what you ran -- is "the size of a pointer-to-char is 1, the size of the
array `a` is 6 (six chars, abcde plus the nul at the end)".
 
R

Richard Heathfield

kevin said:
this is my programm
...
char *p="abcde";
char a[]="abcde";
....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
---------------------------------------------------------
and the output is:

the size of p is:1
the size of a is:6

First, let's write a correct program:

#include <stdio.h> /* a prototype for printf is *REQUIRED* */

int main(void) /* executable code needs a function in which to live */
{
char *p = "abcde";
char a[] = "abcde";

printf("the size of p is %d\n", (int)sizeof p);
printf("the size of a is %d\n", (int)sizeof a);

return 0;
}

If you are getting 1 for p, you have a rather unusual system, in which
bytes are so wide - probably 16 bits or more - that you can fit a
pointer into one. What is more likely is that you typed your question
instead of copying it from your real program.

If your question is simply "why aren't sizeof p and sizeof a the same?",
that's easy. Arrays are not pointers. Pointers are not arrays. Arrays
can be very large indeed, but pointers are generally quite small
(although rarely as small as 1, I must admit). Arrays are cities.
Pointers are signposts. It is possible to imagine a city as small as a
signpost, of course, but one doesn't normally bother.
 
R

Richard Heathfield

Ian Collins said:
kevin wrote:

Are you sure? There aren't many systems around (at least not hosted
ones!) with an 8 bit char*.

There are, however, a few systems around with 16- or even 32-bit char,
so that a pointer value might easily fit in a byte.
 
I

Ian Collins

Richard said:
Ian Collins said:


There are, however, a few systems around with 16- or even 32-bit char,
so that a pointer value might easily fit in a byte.
True, but as you said in your other post, unlikely.
 
R

Richard Heathfield

Ian Collins said:
True, but as you said in your other post, unlikely.

Yes, but my point in /this/ reply was to stress the fact that char
needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
char *, which you did appear to have assumed.
 
I

Ian Collins

Richard said:
Ian Collins said:


Yes, but my point in /this/ reply was to stress the fact that char
needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
char *, which you did appear to have assumed.
Fair point. I should have said "There aren't many systems around (at
least not hosted ones) where sizeof(char*) == sizeof(char)".
 
K

kevin

Ian Collins said:







Yes, but my point in /this/ reply was to stress the fact that char
needn't be 8 bits, so a sizeof(char *) of 1 needn't mean an 8-bit
char *, which you did appear to have assumed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.- -

- -


i'm sorry, all is my fault,i have run the code on my coputer certainly
not 8 bit one even exit
,but i wrote a wrong result while post the topic.
 
K

kevin

i'm sorry, all is my fault,i have run the code on my coputer certainly
not 8 bit one even exit
,but i wrote a wrong result while post the topic.- -

- -

but i also want to know the p standfor an address,while the a standfor
the whole array.
 
C

Chris Dollin

kevin wrote:

(from his OP)

this is my programm
....
char *p="abcde";
char a[]="abcde";

(end-from)

.....
On 6 14 , 3 41 , kevin <[email protected]> wrote:
but i also want to know the p standfor an address,while the a standfor
the whole array.

Because the declaration `char *p;` declares `p` as pointer-to-char, while
the declaration (with initialisation) `char a[] = "abcde";` declares
`a` as `array[6]char`.

(And `sizeof` is one of the places where an array doesn't decay into a
pointer to its first element.)
 
K

Keith Thompson

kevin said:
this is my programm
...
char *p="abcde";
char a[]="abcde";
....

printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
---------------------------------------------------------
and the output is:

the size of p is:1
the size of a is:6

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
A

anil

kevin said:
this is my programm
...
char *p="abcde";
char a[]="abcde";
....
printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
the size of p is:1
the size of a is:6

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

pointers and arrays are interchangeables in some scenarios but not in
all. Array name is treated as 'pointer to first element' in function
arguments and in expressions, execptions are when array name is passed
argument to sizeof() and when array name is used with & operator. In
all other situations arrays are arrays and pointers are pointers.
 
M

Mark McIntyre

pointers and arrays are interchangeables in some scenarios but not in
all. Array name is treated as 'pointer to first element' in function
arguments and in expressions, execptions are when array name is passed
argument to sizeof()

This isn't actually an exception. sizeof() is not a function, its an
operator, and in this case the array is the operand.
In all other situations arrays are arrays and pointers are pointers.

Indeed.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Heathfield

anil said:

Array name is treated as 'pointer to first element' in function
arguments and in expressions

Function arguments *are* expressions.

<snip>
 
A

anil

anil said:



Function arguments *are* expressions.

<snip>

Yes function arguments are actually expressions, i used the term
'expression' and 'function arguments' to distinguish between functions
and some expression like a=b+c; etc.
 
K

Keith Thompson

anil said:
anil said:


Function arguments *are* expressions.

<snip>
[signature snipped]

Yes function arguments are actually expressions, i used the term
'expression' and 'function arguments' to distinguish between functions
and some expression like a=b+c; etc.

But in this case, there is no meaningful distinction. There's nothing
special about function arguments as far as array conversion is
concerned.
 

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

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top