By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Anything wrong with the print?

P: n/a
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.

Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Jul 12 '06 #2

P: n/a

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.

Jul 12 '06 #3

P: n/a
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 '06 #4

P: n/a
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
Jul 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.