LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2009, 09:48 AM   #16
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15

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.
 
Old 02-10-2009, 10:10 AM   #17
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by dwhitney67 View Post
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 View Post
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.

Last edited by johnsfine; 02-10-2009 at 10:12 AM.
 
Old 02-10-2009, 10:28 AM   #18
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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

Last edited by quantt; 02-10-2009 at 10:29 AM.
 
Old 02-10-2009, 10:56 AM   #19
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by quantt View Post
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.
 
Old 02-10-2009, 03:55 PM   #20
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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.
 
Old 02-10-2009, 04:04 PM   #21
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
quannt -

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
adding to vector<string> in a struct niteshadw Programming 3 02-03-2005 07:08 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration