By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

sizeof and pointer

P: n/a
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
------------------------
anyone can tell me why?

Jun 14 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
kevin wrote:
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

------------------------
anyone can tell me why?
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.

--
Ian Collins.
Jun 14 '07 #2

P: n/a
kevin wrote:
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
------------------------
anyone can tell me why?
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)".

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 14 '07 #3

P: n/a
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
------------------------
anyone can tell me why?
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.

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

P: n/a
Ian Collins said:
kevin wrote:
<snip>
>>
the size of p is:1
the size of a is:6

------------------------
anyone can tell me why?
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.

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

P: n/a
Richard Heathfield wrote:
Ian Collins said:
>kevin wrote:

<snip>
>>the size of p is:1
the size of a is:6

------------------------
anyone can tell me why?
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.
True, but as you said in your other post, unlikely.

--
Ian Collins.
Jun 14 '07 #6

P: n/a
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:
>>kevin wrote:

<snip>
>>>the size of p is:1
the size of a is:6

------------------------
anyone can tell me why?

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.
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.

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

P: n/a
Richard Heathfield wrote:
Ian Collins said:
>Richard Heathfield wrote:
>>Ian Collins said:

kevin wrote:
<snip>
the size of p is:1
the size of a is:6
>
------------------------
anyone can tell me why?
>
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.
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.
Fair point. I should have said "There aren't many systems around (at
least not hosted ones) where sizeof(char*) == sizeof(char)".

--
Ian Collins.
Jun 14 '07 #8

P: n/a
On 6 14 , 3 36 , Richard Heathfield <r...@see.sig.invalidwrote:
Ian Collins said:


Richard Heathfield wrote:
Ian Collins said:
>kevin wrote:
<snip>
the size of p is:1
the size of a is:6
>>------------------------
anyone can tell me why?
>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.
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.

--
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.

Jun 14 '07 #9

P: n/a
On 6 14 , 3 41 , kevin <kevin0...@gmail.comwrote:
On 6 14 , 3 36 , Richard Heathfield <r...@see.sig.invalidwrote:


Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:
>>kevin wrote:
><snip>
>>>the size of p is:1
>>>the size of a is:6
>>>------------------------
>>>anyone can tell me why?
>>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.
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.
--
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.- -

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

Jun 14 '07 #10

P: n/a
kevin wrote:

(from his OP)

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

(end-from)

.....
On 6 14 , 3 41 , kevin <kevin0...@gmail.comwrote:
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.)

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 14 '07 #11

P: n/a
kevin <ke*******@gmail.comwrites:
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
------------------------
anyone can tell me why?
Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 14 '07 #12

P: n/a
On Jun 14, 2:42 pm, Keith Thompson <k...@mib.orgwrote:
kevin <kevin0...@gmail.comwrites:
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
------------------------
anyone can tell me why?

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

--
Keith Thompson (The_Other_Keith) k...@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."
-- 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.

Jun 14 '07 #13

P: n/a
On Thu, 14 Jun 2007 10:46:59 -0000, in comp.lang.c , anil
<an**********@gmail.comwrote:
>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
Jun 14 '07 #14

P: n/a
anil said:

<snip>
Array name is treated as 'pointer to first element' in function
arguments and in expressions
Function arguments *are* expressions.

<snip>

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

P: n/a
kevin wrote:
>
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

------------------------
anyone can tell me why?
p is a pointer. a is an array.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 14 '07 #16

P: n/a
On Jun 14, 6:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:
anil said:

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

Function arguments *are* expressions.

<snip>

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

Jun 15 '07 #17

P: n/a
anil <an**********@gmail.comwrites:
On Jun 14, 6:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>anil said:
<snip>
Array name is treated as 'pointer to first element' in function
arguments and in expressions

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.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 15 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.