Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Char * question

Reply
Thread Tools

Char * question

 
 
smartbeginner
Guest
Posts: n/a
 
      12-26-2005
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?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-26-2005
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:

<(E-Mail Removed) .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)
 
Reply With Quote
 
 
 
 
abdur_rab7@yahoo.co.in
Guest
Posts: n/a
 
      12-26-2005
char* c;

is a character pointer;

c= "Im a beginner";

what about the allocation of c

 
Reply With Quote
 
raghu
Guest
Posts: n/a
 
      12-26-2005
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

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      12-26-2005
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
 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      12-26-2005
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 )
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-26-2005
http://www.velocityreviews.com/forums/(E-Mail 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.

--
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)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-26-2005
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)
 
Reply With Quote
 
smartbeginner
Guest
Posts: n/a
 
      12-26-2005
Thanks people for this speedy reply

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

 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      12-26-2005
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()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments