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

Char * question

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
21 1442
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
char* c;

is a character pointer;

c= "Im a beginner";

what about the allocation of c

Dec 26 '05 #3
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
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
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
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
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
Thanks people for this speedy reply

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

Dec 26 '05 #9
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
"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
smartbeginner:
sizeof(c) just return the size of pointer.
to get size of string,should use strlen()

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

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

Similar topics

2
by: Nicholas Parnell | last post by:
Hi! I have the problem where I cast a bit string of "10000000" to a byte, which i get -128; this is fine so far. However, when I take this byte and cast it to a char, I get a question mark('?')....
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
1
by: b83503104 | last post by:
When are they not consistent?
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
7
by: owolablo | last post by:
Can anybody please tell me how to change the individual elements of a char variable. I need to parse through the string, check for a particular character and change it to something else if it is...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
12
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
16
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.