473,396 Members | 1,924 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,396 software developers and data experts.

sizeof and pointer

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
17 1878
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
4
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
79
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
1
by: somenath | last post by:
Hi All, I have couple of questions regarding the behavior of the bellow code. #include<stdio.h> int main(void) { int ar={ {1,2}, {3,4}, {4,5}
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.