LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C: string to struct (https://www.linuxquestions.org/questions/programming-9/c-string-to-struct-702232/)

quantt 02-10-2009 09:48 AM

Hm, I think, what it's wrong, I patched that, but I've got there...
Code:

  for (i = 0; i < 4; i++)
  {
    const unsigned int id = (address >> (24 - i*8)) & 0xFF;

    sprintf(p->adr, "%u%c", id, (i == 3 ? '\n' : '.'));
  printf("%s\n", p->adr); //good 10.10.10.3
  }
  printf("%s\n", p->adr); // printed just latest value 0.0.0.3, why? me need print it not into "for" area.


johnsfine 02-10-2009 10:10 AM

Quote:

Originally Posted by dwhitney67 (Post 3437212)
sprintf(p->adr + strlen(p->adr),

Notice the addition of strlen(p->adr)

That achieves the concatenation without needing either strcat or separate buffer (such as the buffer inside itoa in my earlier example)

Quote:

Originally Posted by quantt (Post 3438466)
sprintf(p->adr,

You didn't copy that detail.

So inside the loop you see only each chunk one at a time and outside the loop you see only the last chunk.

If you have the concatenation working then inside the loop you would see each everything up to and including the current chunk and outside the loop, you would see the whole thing.

quantt 02-10-2009 10:28 AM

sprintf(p->adr + strlen(p->adr),
it's doesn't work.
because it doing sumup value, when it doing via sprintf. Furthermore, on result I've got wrong value in p->adr
But, I not understand why printf into cycle is working, but outside "for()" it does not.
for(){
...
printf("%s\n", p->adr); //good 10.10.10.3
}
printf("%s\n", p->adr); // is not good 0.0.0.3

johnsfine 02-10-2009 10:56 AM

Quote:

Originally Posted by quantt (Post 3438517)
it's doesn't work.
because it doing sumup value, when it doing via sprintf.

Show your whole test program. We shouldn't need to guess why something didn't work from descriptions like "because it doing sumup value".

But this time I'll guess:

dwhitney67 allocated the structure using

struct data *p = calloc(1, sizeof(struct data));

That zeroes the entire structure when allocated, so you don't need to null terminate the buffer.

You may have allocated the structure some other way. Some methods of allocating the structure would require that you null terminate the string before using strlen(p->adr). This should be done once, after allocating the struct and before starting the loop.

either
p->adr[0] = 0;
or
p->adr[0] = '\0';
are equally valid.

quantt 02-10-2009 03:55 PM

Quote:

Show your whole test program.
Patched meant what it should be replace similar of core previous code programm.

which's way I can take successful useful printf outside this loop? Hm, I think what if I'll tried usage construction strca and itoa it's not change result of printf outside of the loop.

dwhitney67 02-10-2009 04:04 PM

quannt -

What part of the code that I posted earlier do you not understand?


All times are GMT -5. The time now is 05:37 AM.