Anything wrong with the print?

Discussion in 'C Programming' started by QQ, Jul 12, 2006.

  1. QQ

    QQ Guest

    Hi Here is part of my code

    typedef struct{
    int len;
    char code[16];
    }Code;

    typedef struct{
    ....
    Code *a;
    ....
    }List;

    Now I'd like to print the content.

    I have

    Code tmpA;
    List list;
    tmpA=5;
    memcpy(code,"12345",5);
    list.a = &tmpA;
    printf("a(len=%d,value=(%s))\n",list.a->len,list.a->code);

    however it seems that something wrong, the print out is not right.
    QQ, Jul 12, 2006
    #1
    1. Advertising

  2. QQ said:

    > Hi Here is part of my code


    The rest of it would be handy. Something that actually compiles would be
    even better.

    >
    > typedef struct{
    > int len;
    > char code[16];
    > }Code;
    >
    > typedef struct{
    > ...
    > Code *a;
    > ...
    > }List;
    >

    <snip>
    >
    > Code tmpA;
    > List list;
    > tmpA=5;
    > memcpy(code,"12345",5);
    > list.a = &tmpA;
    > printf("a(len=%d,value=(%s))\n",list.a->len,list.a->code);
    >
    > however it seems that something wrong, the print out is not right.


    Since the code won't compile, I'm hardly surprised you're not getting the
    printout you desire.

    --
    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, Jul 12, 2006
    #2
    1. Advertising

  3. QQ

    John Bode Guest

    QQ wrote:
    > Hi Here is part of my code
    >
    > typedef struct{
    > int len;
    > char code[16];
    > }Code;
    >
    > typedef struct{
    > ...
    > Code *a;
    > ...
    > }List;
    >
    > Now I'd like to print the content.
    >
    > I have
    >
    > Code tmpA;
    > List list;
    > tmpA=5;


    Whoa, hold up. tmpA is a struct type. 5 is *not* a struct type. The
    compiler should have yakked on this line. Did you mean

    tmpA.len = 5;

    instead?

    > memcpy(code,"12345",5);


    What is code? Where has it been defined? Did you mean

    memcpy(tmpA.code, "12345", 5);

    instead?

    > list.a = &tmpA;
    > printf("a(len=%d,value=(%s))\n",list.a->len,list.a->code);
    >
    > however it seems that something wrong, the print out is not right.


    Cut and paste the *actual* code that shows the problem, and we may be
    able to help.
    John Bode, Jul 12, 2006
    #3
  4. QQ

    Guest

    QQ wrote:
    > Hi Here is part of my code
    >
    > typedef struct{
    > int len;
    > char code[16];
    > }Code;
    >
    > typedef struct{
    > ...
    > Code *a;
    > ...
    > }List;
    >
    > Now I'd like to print the content.
    >
    > I have
    >
    > Code tmpA;
    > List list;
    > tmpA=5;



    You have an incompatible type in assignment;
    perhaps you what were seeking was
    partial initialization of a struct?


    > memcpy(code,"12345",5);



    What about copying the terminating
    null character? Consider using
    strncpy instead. Also, I think
    you wanted the destination to
    be tmpA.code, not code.

    > list.a = &tmpA;
    > printf("a(len=%d,value=(%s))\n",list.a->len,list.a->code);
    >
    > however it seems that something wrong, the print out is not right.



    Here's a small complete program that does
    what (I'm guessing) you wanted:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct
    {
    int len;
    char code[16];
    } Code;

    typedef struct
    {
    Code *a;
    } List;

    int main (void)
    {
    Code tmpA = { 5 };
    List list;

    /*
    * Incompatible type in assignment:
    * tmpA = 5;
    *
    * Forgetting to copy the terminating null character?
    * memcpy(tmpA.code, "12345", 5);
    */

    strncpy(tmpA.code, "12345", sizeof(tmpA.code) - 1);
    list.a = &tmpA;
    printf("a(len=%d, value=(%s))\n", list.a->len, list.a->code);
    return EXIT_SUCCESS;
    }

    --
    Hope this helps,
    Steven
    , Jul 12, 2006
    #4
  5. QQ schrieb:
    > Hi Here is part of my code
    >
    > typedef struct{
    > int len;
    > char code[16];
    > }Code;
    >
    > typedef struct{
    > ...
    > Code *a;
    > ...
    > }List;
    >
    > Now I'd like to print the content.
    >
    > I have
    >
    > Code tmpA;
    > List list;
    > tmpA=5;
    > memcpy(code,"12345",5);
    > list.a = &tmpA;
    > printf("a(len=%d,value=(%s))\n",list.a->len,list.a->code);
    >
    > however it seems that something wrong, the print out is not right.


    What does it print? Does it print "42"? Then it would be right, it's the
    answer. :)

    Well, post compileable code and say _what_ is wrong, what you expected
    and what you got instead.

    However, you print list.a->code as it where a c-style-string
    (null-terminated), but it actually isn't null-terminated, isn't it?

    --
    Thomas
    Thomas J. Gritzan, Jul 12, 2006
    #5
    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. Mano kumar
    Replies:
    2
    Views:
    358
    Kevin Spencer
    Oct 17, 2003
  2. Replies:
    5
    Views:
    506
  3. rf
    Replies:
    1
    Views:
    366
    Leif K-Brooks
    Jul 6, 2003
  4. keto
    Replies:
    0
    Views:
    935
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    348
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page