Anything wrong with the print?

Q

QQ

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.
 
R

Richard Heathfield

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;
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.
 
J

John Bode

QQ said:
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.
 
A

attn.steven.kuo

QQ said:
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;
}
 
T

Thomas J. Gritzan

QQ said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top