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

Char * question

P: n/a
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}

The o/p in both cases is 2
Can you answer me why?

Dec 26 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
smartbeginner said:
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
Here is your first problem, which is to do with prototypes. Please read a
recent thread which started with this message ID:

<11**********************@f14g2000cwb.googlegroups .com>

which asked a similar question and had a similar error.

Also note that sizeof yields size_t, not int, so %d is inappropriate.
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}

The o/p in both cases is 2
Can you answer me why?


Assigning a value to an object does not change its size.

--
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)
Dec 26 '05 #2

P: n/a
char* c;

is a character pointer;

c= "Im a beginner";

what about the allocation of c

Dec 26 '05 #3

P: n/a
since c is a pointer it always occupies two bytes to hold the
address,hence the o/p is 2. when you have initialised the pointer c
,it takes the starting address of string,hence the o/p is two even for
that.
hope i'm correct.have a nice time

Dec 26 '05 #4

P: n/a
smartbeginner a écrit :
Hello,
My pgm is
main()
{
char *c;
This statement (? instruction ?) defines an object called 'c'. Its type
is 'pointer to char'. It's not initialized.
printf("\n Size of c is %d",sizeof(c));
sizeof is a C-unary-operator that returns the size of an object or of a
type (parens required) in number of bytes. The type of the returned
value is size_t.

Given that c is a pointer to char, the size of a pointer to char is
probably a few bytes (2,4 etc. depending on your machine). Note that the
value hold by the object has of course non influence on the size of the
object.

Note that due to the type returned by the sizeof operator, the correct
formatter for printf() is "%zu". Note also that the place for the end of
line character is ... at the end of the line, and not at its beginning.

Note also that the sizeof operator doesn't require parens when its
operand is an object.

printf (" Size of c is %zu\n", sizeof c);

If you have a pre-C99 compiler, the trick is tu use the "%lu" formatter
with the (unsigned long) cast.

printf (" Size of c is %lu\n", (unsigned long) sizeof c);
c="Im a beginner";
Now, the value of 'c' holds the address of the first character of string
literal.

printf("\n Size of string c is %d",sizeof(c));
}
The o/p in both cases is 2
Can you answer me why?


As explained before, this doesn't change the size of the c object that
still is a pointer to c.

--
A+

Emmanuel Delahaye
Dec 26 '05 #5

P: n/a
raghu wrote:
since c is a pointer it always occupies two bytes to hold the
address,hence the o/p is 2. when you have initialised the pointer c
,it takes the starting address of string,hence the o/p is two even for
that.
hope i'm correct.have a nice time


Nope. Yes, c is a pointer to char, but it is not necessarily 2 bytes
long. In my system - and nearly all 32bit systems, although I've never
seen a 32bit system with less - it is 4 bytes long. In older x86
processors, it was 2 bytes. And in some microcontrollers it is 1 byte long.

So it is machine-dependent.

Also read C-faq's question 5.17 ( http://c-faq.com/null/machexamp.html )
Dec 26 '05 #6

P: n/a
ab********@yahoo.co.in said:
char* c;

is a character pointer;

c= "Im a beginner";

what about the allocation of c


char *c; allocates storage for the c object. It doesn't allocate any storage
for a string. But it's used for pointing to a string that already exists.
Pointing at dynamically allocated memory is not the /only/ purpose of
pointers, you know.

--
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)
Dec 26 '05 #7

P: n/a
raghu said:
since c is a pointer it always occupies two bytes


Please spend a few more years learning C before you attempt to teach it.
Pointers do not always occupy two bytes.

--
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)
Dec 26 '05 #8

P: n/a
Thanks people for this speedy reply

Then how can I find the size of string with the same earlier program

Dec 26 '05 #9

P: n/a
smartbeginner wrote:
Thanks people for this speedy reply

Then how can I find the size of string with the same earlier program


Please do quote...
#include <string.h> and then use strlen()
Dec 26 '05 #10

P: n/a
"smartbeginner" <rr******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Thanks people for this speedy reply

Then how can I find the size of string with the same earlier program


Please provide some context when you reply to something. Also, please read
the FAQ before posting. The "problem" you have has been explained there
(well, you have several problems in the code you posted and they are all in
the FAQ one way or the other, but here's a link to the one you are reffering
to in this post)
http://c-faq.com/aryptr/index.html
Dec 26 '05 #11

P: n/a
smartbeginner:
sizeof(c) just return the size of pointer.
to get size of string,should use strlen()

Dec 26 '05 #12

P: n/a
Richard Heathfield wrote:
raghu said:
since c is a pointer it always occupies two bytes


Please spend a few more years learning C before you attempt to teach it.
Pointers do not always occupy two bytes.

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


<OT>
Wow! So it takes others more than 2 years to get a full grasp over the
language? Seriously. Here I thought I was just plain stupid because it
has taken me this long to learn the language (and still going).
</OT>

Chad

Dec 26 '05 #13

P: n/a
Chad said:
Wow! So it takes others more than 2 years to get a full grasp over the
language?


Oh yes. And, in some cases, much much much much much much much much much
much much much much much much much much much much much much much longer.

--
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)
Dec 26 '05 #14

P: n/a
smartbeginner wrote:
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}

The o/p in both cases is 2
Can you answer me why?


Because sizeof(char *) is a compile-time constant, apparently with the
value 2 for your implementation.

Your comment in the second printf is wrong: c is a char*, not a string.
In fact, since you did not end the last output line with an end-of-line
character, your program is not portable. '\n' is the end-of-line
character, not a beginning-of-line character as your program suggests.

You have other problems.
You have no declaration for the variadic function printf(), and one is
required. #including <stdio.h> will fix this.

sizeof() gives a result as an unsigned integer of type size_t, but "%d"
is the specifier for a *signed* int of type signed int. You need to fix
this. If your library supports the specifier for size_t (unlikely), use
it. Otherwise cast the result of size_t and use the appropriate
specifier. It is usually safe to case to (unsigned long) and print with
'%lu'.

You need to find out what standard your compiler claims to conform to.
If it is C89 (or C90), then your failure to have a return or exit() at
the end of main is an error.
If it is C99, then the implicit declaration of main to return an int is
gone and you *must* supply the return type,
int main(void)
and you *should* always return values which claim to do so, but are not
required to in the special case of printf().

If you are going to post to technical newsgroups, drop the cute babytalk:
'program' is not spelled 'pgm'
'output' is not spelled 'o/p'
Dec 26 '05 #15

P: n/a
raghu wrote
(in article
<11*********************@g49g2000cwa.googlegroups. com>):
since c is a pointer it always occupies two bytes
did you just climb out from under a rock?

It's exceedingly less than "always" true, in fact, on most
modern machines it is not.
hope i'm correct.


Sadly, no.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 26 '05 #16

P: n/a
Richard Heathfield wrote
(in article
<do**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):
Chad said:
Wow! So it takes others more than 2 years to get a full grasp over the
language?


Oh yes. And, in some cases, much much much much much much much much much
much much much much much much much much much much much much much longer.


We've actually seen examples in this very newsgroup of people
with exposure to C since almost its invention that still do not
understand it well at all.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 26 '05 #17

P: n/a
Randy Howard wrote:
Richard Heathfield wrote
(in article
<do**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):

Chad said:

Wow! So it takes others more than 2 years to get a full grasp over the
language?


Oh yes. And, in some cases, much much much much much much much much much
much much much much much much much much much much much much much longer.

We've actually seen examples in this very newsgroup of people
with exposure to C since almost its invention that still do not
understand it well at all.

That would suggest a list of people. I wonder if I'm on it? A number of
people do show significant growth however, our friend pete among them. I
am concerned about several others, who shall remain nameless here.

Chris Torek is coming along nicely. :-)
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 26 '05 #18

P: n/a

Chad wrote:
Richard Heathfield wrote:
raghu said:
since c is a pointer it always occupies two bytes


Please spend a few more years learning C before you attempt to teach it.
Pointers do not always occupy two bytes.

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


<OT>
Wow! So it takes others more than 2 years to get a full grasp over the
language? Seriously. Here I thought I was just plain stupid because it
has taken me this long to learn the language (and still going).
</OT>

Chad


I'm on year 15. I'm still learning. Welcome to the club.

Dec 26 '05 #19

P: n/a
Martin Ambuhl <ma*****@earthlink.net> writes:
[...]
You need to find out what standard your compiler claims to conform
to. If it is C89 (or C90), then your failure to have a return or
exit() at the end of main is an error.
If it is C99, then the implicit declaration of main to return an int
is gone and you *must* supply the return type,
int main(void)
and you *should* always return values which claim to do so, but are
not required to in the special case of printf().


I think you mean "in the special case of main()".

In my opinion, C99's new rule that falling off the end of main() is
equivalent to "return 0;" is best ignored. Just always return a
value, and your code will be valid under any standard.

--
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.
Dec 26 '05 #20

P: n/a
Keith Thompson a écrit :
In my opinion, C99's new rule that falling off the end of main() is
equivalent to "return 0;" is best ignored. Just always return a
value, and your code will be valid under any standard.


Agreed.

--
A+

Emmanuel Delahaye
Dec 26 '05 #21

P: n/a
Joe Wright wrote
(in article <R9********************@comcast.com>):
Randy Howard wrote:
Richard Heathfield wrote
(in article
<do**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):

Chad said:
Wow! So it takes others more than 2 years to get a full grasp over the
language?

Oh yes. And, in some cases, much much much much much much much much much
much much much much much much much much much much much much much longer.

We've actually seen examples in this very newsgroup of people
with exposure to C since almost its invention that still do not
understand it well at all.

That would suggest a list of people. I wonder if I'm on it?


Actually, I thought I was down the hall in comp.programming when
I wrote that. There are not that many examples of "old timers"
here with that problem, and I certainly didn't aim it at you.
A number of
people do show significant growth however, our friend pete among them. I
am concerned about several others, who shall remain nameless here.

Chris Torek is coming along nicely. :-)


Exceptionally well, yes. :-)
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 27 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.