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