Char * question

Discussion in 'C Programming' started by smartbeginner, Dec 26, 2005.

  1. 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?
    smartbeginner, Dec 26, 2005
    #1
    1. Advertising

  2. 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:

    <>

    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)
    Richard Heathfield, Dec 26, 2005
    #2
    1. Advertising

  3. smartbeginner

    Guest

    char* c;

    is a character pointer;

    c= "Im a beginner";

    what about the allocation of c
    , Dec 26, 2005
    #3
  4. smartbeginner

    raghu Guest

    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
    raghu, Dec 26, 2005
    #4
  5. 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
    Emmanuel Delahaye, Dec 26, 2005
    #5
  6. 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 )
    Giannis Papadopoulos, Dec 26, 2005
    #6
  7. 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)
    Richard Heathfield, Dec 26, 2005
    #7
  8. 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)
    Richard Heathfield, Dec 26, 2005
    #8
  9. Thanks people for this speedy reply

    Then how can I find the size of string with the same earlier program
    smartbeginner, Dec 26, 2005
    #9
  10. 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()
    Giannis Papadopoulos, Dec 26, 2005
    #10
  11. smartbeginner

    buda Guest

    "smartbeginner" <> wrote in message
    news:...
    > 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
    buda, Dec 26, 2005
    #11
  12. smartbeginner

    Jason Guest

    smartbeginner:
    sizeof(c) just return the size of pointer.
    to get size of string,should use strlen()
    Jason, Dec 26, 2005
    #12
  13. smartbeginner

    Chad Guest

    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
    Chad, Dec 26, 2005
    #13
  14. 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)
    Richard Heathfield, Dec 26, 2005
    #14
  15. 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'
    Martin Ambuhl, Dec 26, 2005
    #15
  16. smartbeginner

    Randy Howard Guest

    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.


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Dec 26, 2005
    #16
  17. smartbeginner

    Randy Howard Guest

    Richard Heathfield wrote
    (in article
    <dop4ul$dll$-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
    Randy Howard, Dec 26, 2005
    #17
  18. smartbeginner

    Joe Wright Guest

    Randy Howard wrote:
    > Richard Heathfield wrote
    > (in article
    > <dop4ul$dll$-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 ---
    Joe Wright, Dec 26, 2005
    #18
  19. smartbeginner

    John Bode Guest

    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.
    John Bode, Dec 26, 2005
    #19
  20. Martin Ambuhl <> 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) <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.
    Keith Thompson, Dec 26, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    549
  2. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    473
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    580
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
Loading...

Share This Page