Char * question

S

smartbeginner

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?
 
R

Richard Heathfield

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:

<[email protected]>

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

abdur_rab7

char* c;

is a character pointer;

c= "Im a beginner";

what about the allocation of c
 
R

raghu

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
 
E

Emmanuel Delahaye

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

Giannis Papadopoulos

raghu said:
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 )
 
R

Richard Heathfield

(e-mail address removed) 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.
 
R

Richard Heathfield

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

smartbeginner

Thanks people for this speedy reply

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

Giannis Papadopoulos

smartbeginner said:
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()
 
B

buda

smartbeginner said:
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
 
J

Jason

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

Chad

Richard said:
raghu said:


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
 
R

Richard Heathfield

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

Martin Ambuhl

smartbeginner said:
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'
 
R

Randy Howard

raghu wrote
(in article
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.
 
R

Randy Howard

Richard Heathfield wrote
(in article
Chad said:


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

Joe Wright

Randy said:
Richard Heathfield wrote
(in article



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. :)
 
J

John Bode

Chad said:
<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.
 
K

Keith Thompson

Martin Ambuhl said:
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.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top