C: string to struct
hello,
Suppose me the best way to putting str to char with separator. Code:
struct data *p; 1.2.3.4. but me need that 1.2.3.4 Were numbers from 1 to 4 it's just char values, from 0 to 3 char numbers. A point it's the separator. |
With no corrections for the large number of obvious typos and syntax errors in your example, one way to implement the logic that I think you want is this:
Code:
struct data *p; |
...
|
hello in again,
I've another question about that bicycle. If I doing like remarked printf I can take similar of this line... 1.2.3.4 But if I try use a struct. I getting something else. And suggest me something what can work like ntohs, but more faster. Code:
for (i=0;i<4; i++) { |
That quoted code is from my answer in the post
http://www.linuxquestions.org/questi...16#post3421516 That might give enough extra context to quantt's question that someone might answer it. Even with that extra context I don't understand the question. Understanding broken C code is usually one of my skills, but when combined with broken English it probably isn't high on my skills list. Quantt, it might help if you tell us the data type of the object represented by p->srt in your above post (p->str in earlier posts). A line like p->srt += ntohs(id[i]); implies p->srt is something like a C++ std::string but I thought you were programming in C not C++. A line like printf("%u",p->srt); implies p->srt is an unsigned int. |
Quote:
If I doing there, I've got not what expect. Code:
for (i=0;i<4; i++) { Quote:
I tried use inet_ntoa, but unsuccessful. It's change nothing with stdout. Suggest me easy way to doing that Code:
printf("%u%c", id[i], (i==3) ? '\n' : '.' ); |
Quote:
What bicycle? |
I just need with it...
collect id[i] with i from 0 to 3 and separated "."(point) and print it stuff str = id[0].id[1].id[2].id[3]; printf("%u",p->srt); |
Quote:
Quote:
The "%u" passed to printf tells printf to treat the corresponding argument (p->srt) as an unsigned decimal integer. The thing you want to print is not an integer. You haven't told us what it is, but it isn't an integer. Maybe you're used to a language like Python, where a beginner can ignore issues of data type and the compiler and/or run time library seem to figure out an appropriate data type for each operation. C doesn't work that way. An important part of programming in C is choosing a data type for each item of data that you work with. Your choice of data type determines which operations are possible and how you must do those operations. For example, one reasonable choice of the data type for p->str is a char array preallocated, uninitialized with enough space for at least one character longer than the longest string you might store there. Before using strcat (see below) the first time on an uninitialized char array, you need to terminate it, so somewhere before the loop Code:
p->str[0] = 0; Code:
strcat( p->str, "." ); Code:
strcat( p->str, itoa( id[i] ) ); Once you manage to build the whole text you want in the char array, you can print it with Code:
printf("%s", p->str); |
Quote:
unsigned char id[4]; unsigned char str; What's function can take unsigned char and return unsigned char? Or what's function I can use without strcat, because if I tried use itoa I've got that"undefined reference to `itoa'", but it defined on <stdlib.h> Or which is another way to work with C-strings? |
I hate to be rude (and I admit you speak English better than I speak any second language), but this is too hard. Do you know someone who speaks your first language that knows some C and can explain basic concepts to you? Or maybe you know someone who speaks both English and your first language that is willing to read the replies written to you at LQ and tell you what they say.
|
I think
[quote] p->str[0] = 0; [/code] should be Code:
p->str[0] = '\0'; |
Quote:
This won't help quantt, and I'm not exactly sure what will. But I need to comment on chrism01's statement. I would point out that in C, the statement Code:
p->str[0] = 0; Code:
p->str[0] = '\0'; Here's a demonstration. It consists of two bash scripts. Script 1.sh: Code:
#!/bin/bash Code:
#!/bin/bash Code:
3c3 Code:
1 #!/bin/bash Code:
1 #!/bin/bash Code:
1c1 |
[QUOTE=chrism01;3436526]I think
Quote:
Code:
struct data{ |
quantt -
Consider the following code. Does this meet your needs? PHP Code:
|
All times are GMT -5. The time now is 07:04 AM. |