char * and int *

Discussion in 'C Programming' started by gyan, Jul 6, 2005.

  1. gyan

    gyan Guest

    why following's output is noting in C program

    int a=5;
    printf("\n%s\n",(char *)(&a));

    and
    following lines give correct output

    printf("\n%d\n",*(int *)(&a));
     
    gyan, Jul 6, 2005
    #1
    1. Advertising

  2. gyan

    Suman Guest

    gyan wrote:
    > why following's output is noting in C program
    >
    > int a=5;
    > printf("\n%s\n",(char *)(&a));

    Read up on the conversion specifiers a bit.
    Specifically, from the man page of printf(), I'll quote (partly) a
    couple that
    might be of interest to you:
    s The char * argument is expected to be a pointer to an array
    of
    character type (pointer to a string). Characters from the
    array
    are written up to (but not including) a terminating NUL
    charac-
    ter;...

    p The void * pointer argument is printed in hexadecimal (as
    if by
    `%#x' or `%#lx').
    So `s' is not the correct thing to use when you are trying to print out
    the
    address (provided, I understand you correctly), rather p is.
    >
    > and
    > following lines give correct output
    >
    > printf("\n%d\n",*(int *)(&a));

    is the same as
    printf("\n%d\n",a);
    and prints 5 with a newline before and after the number.The cast is
    superfluous.
     
    Suman, Jul 6, 2005
    #2
    1. Advertising

  3. gyan

    Chris Dollin Guest

    gyan wrote:

    > why following's output is noting in C program
    >
    > int a=5;
    > printf("\n%s\n",(char *)(&a));


    `%s` wants a string, ie a char* pointer to a null-terminated
    sequence of characters. `a`, whose address you have taken and
    then forcibly (but reversibly) converted to `char*`, isn't
    any sequence of characters at all, so whatever you get out
    isn't C's responsibility any more - it will be an accident
    of the implementation. (Or even a deliberate feature, but
    usually not.)

    *As it happens*, the underlying implementation may not care.
    There are some bytes in the object `a`; it may treat them
    as characters and print until it finds a zero byte. If you
    machine is big-endian, or the character with value 5 is
    invisible, you'll get what you got - no visible characters
    displayed.

    > and
    > following lines give correct output
    >
    > printf("\n%d\n",*(int *)(&a));


    Because you ask to print an int, and what you give it is

    *(int *)(&a)

    which is

    *(&a)

    because the cast is redundant, &a already being int*, and

    *&a === a

    by the co-definitions of * and &.

    --
    Chris "electric hedgehog" Dollin
    It's called *extreme* programming, not *stupid* programming.
     
    Chris Dollin, Jul 6, 2005
    #3
  4. gyan

    gyan Guest

    I agree with you all, but i am playing little bit.
    see i am working on
    SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000

    here int takes 4 bytes.
    Now i have prgram:
    int a=6;
    char *tmp;
    tmp=(char *)(&a);
    *(tmp+4)='\0';
    printf("\n%s\n",tmp+3);

    Now i run it through debugger dbx.

    according to output:
    p *(tmp) = '\0'
    p *(tmp+1) = '\0'
    p *(tmp+2) = '\0'
    p -f%d *(tmp+3) = 6
    p *(tmp+3) = '\0'

    So when i write
    printf("\n%s\n",tmp+3);
    i hope that program should see 6 at that adress
    (tmp+3) and '\0' at next address.
    Now %s should print 6, since i am passing a char * who stores a char array
    type having 6 at oth byte and '\0' at 1st byte.
     
    gyan, Jul 6, 2005
    #4
  5. gyan

    Me Guest

    > why following's output is noting in C program
    >
    > int a=5;
    > printf("\n%s\n",(char *)(&a));


    Odds are you're on a big endian endian machine that read the upper 8
    value bits (which are 0 in this case) of |a| and printed nothing
    because it treated it as a 0 length NUL terminated string.

    > and
    > following lines give correct output
    >
    > printf("\n%d\n",*(int *)(&a));


    *(int*)&a is the same thing as a
     
    Me, Jul 7, 2005
    #5
  6. gyan

    Jack Klein Guest

    On Wed, 06 Jul 2005 18:54:36 -0400, "gyan" <> wrote in
    comp.lang.c:

    > I agree with you all, but i am playing little bit.
    > see i am working on
    > SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000
    >
    > here int takes 4 bytes.
    > Now i have prgram:
    > int a=6;
    > char *tmp;
    > tmp=(char *)(&a);
    > *(tmp+4)='\0';


    If sizeof(int) is four or less on your platform, the line above
    invokes undefined behavior. And if sizeof(int) is not 4 on your
    platform, it makes no sense at all.

    > printf("\n%s\n",tmp+3);


    The problem is that once you have produced undefined behavior, and you
    have, the rest of the program is completely meaningless as far as the
    C standard, and therefore this newsgroup, are concerned.

    >
    > Now i run it through debugger dbx.


    Debuggers are completely off topic here.

    >
    > So when i write
    > printf("\n%s\n",tmp+3);
    > i hope that program should see 6 at that adress
    > (tmp+3) and '\0' at next address.
    > Now %s should print 6, since i am passing a char * who stores a char array
    > type having 6 at oth byte and '\0' at 1st byte.


    No it shouldn't, you are mistaken. The character '6' in your
    implementation's character set will print "6". The binary value 0x6
    will not.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 7, 2005
    #6
    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:
    574
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    410
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,679
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,193
Loading...

Share This Page