ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.