Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Anything wrong with the print?

Reply
Thread Tools

Anything wrong with the print?

 
 
QQ
Guest
Posts: n/a
 
      07-12-2006
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-12-2006
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)
 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      07-12-2006

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.

 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      07-12-2006
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

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      07-12-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there anything fundamentally wrong with this code? simon.stockton@baesystems.com VHDL 5 04-20-2006 01:09 PM
brut force eigrp over gre working, is there anything wrong? jcharth@hotmail.com Cisco 0 10-05-2005 09:57 PM
not receiving email,is anything wrong? =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 3 07-28-2004 10:32 PM
Is there anything wrong with my insert code? Mano kumar ASP .Net 2 10-17-2003 01:51 PM
Re: Is there anything wrong... rf HTML 1 07-06-2003 11:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57