473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1577
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++ ) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.