472,954 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Anything wrong with the print?

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
4 1559
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hal Vaughan | last post by:
I am working on an install program, for another program that will need a list of printers on a system. Originally I found the printers by doing this: public PrintNames() { String tName = "";...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
5
by: Nathan Pinno | last post by:
Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? # Plays the guessing game higher or lower. # Originally written by Josh...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
4
by: Chris | last post by:
Could anyone please help me. IS there something wrong with it? char * function getString() { char myString; sprintf(myString, "hello world");
1
by: Adam Monsen | last post by:
Is there anything wrong with using something like super(type(self), self).f() to avoid having to hardcode a type? For example: class A(object): def f(self): print "in A.f()" class B(A): def...
17
by: so many sites so little time | last post by:
all right so the script is pretty simple it goes it retrives what the id of the post is and it lets you edit it well no it doesnt. now if you go to www.kirewire.com/pp2/index/php you will see a...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.