LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-04-2009, 08:27 AM   #1
quantt
Member
 
Registered: Aug 2008
Posts: 62

Rep: Reputation: 15
C: string to struct


hello,
Suppose me the best way to putting str to char with separator.
Code:
struct data *p;
for (num=0;num<4; num++) {
str += sprtinf(arr[num], sizeof(arr[num]));
str += sprtinf(".");
p->str;
}
If I doing which's I sampled, than I've got point on end of line.
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.
 
Old 02-04-2009, 08:35 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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;
for (num=0;num<4; num++) {
str += sprtinf(arr[num], sizeof(arr[num]));
if(num < 3) str += sprtinf(".");
p->str;
}
 
Old 02-05-2009, 06:33 AM   #3
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
...

Last edited by quantt; 02-05-2009 at 07:33 AM. Reason: solved
 
Old 02-06-2009, 07:46 AM   #4
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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++) {
      id[i] = (address >> (24-i*8)) & 0xFF;
      //printf("%u%c", id[i], (i==3) ? '\n' : '.' );
	p->srt += ntohs(id[i]);
	if(i < 3) {
p->srt += ntohs(".");
}
printf("%u",p->srt);
}
 
Old 02-06-2009, 08:12 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 02-06-2009 at 08:19 AM.
 
Old 02-06-2009, 10:50 AM   #6
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
That quoted code is from my answer in the post
http://www.linuxquestions.org/questi...16#post3421516
Absolutely, It working. But I need use struct so as to doing it. The only C like style.
If I doing there, I've got not what expect.
Code:
for (i=0;i<4; i++) {
      id[i] = (address >> (24-i*8)) & 0xFF;
	p->srt += ntohs(id[i]);
	if(i < 3) {
p->srt += ntohs(".");
}
printf("%u",p->srt);
}
Quote:
implies p->srt is an unsigned int.
Hm, what's function cat return char? I need printed it stuff into 8bit.
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' : '.' );
but with struct.
 
Old 02-06-2009, 12:07 PM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by quantt View Post
I've another question about that bicycle.
Forgive me, but I can't resist.

What bicycle?
 
Old 02-07-2009, 05:06 AM   #8
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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);
 
Old 02-07-2009, 09:23 AM   #9
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 johnsfine View Post
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).
You never answered that.

Quote:
Originally Posted by quantt View Post
printf("%u",p->srt);
But you're still asking for that.

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;
To add text to the end of the contents of a char array, you can't use +=. You can use strcat, such as
Code:
strcat( p->str, "." );
If you have itoa available (some C development environments don't) you can convert each of your id bytes to text with itoa
Code:
strcat( p->str, itoa( id[i] ) );
The corresponding part of the code you showed using "ntohs" makes no sense. I can't tell what you intended.

Once you manage to build the whole text you want in the char array, you can print it with
Code:
printf("%s", p->str);
 
Old 02-07-2009, 09:55 PM   #10
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
The "%u" passed to printf tells printf to treat the corresponding argument (p->srt) as an unsigned decimal integer.
No, because I've another type of data, therefore I need use long type such as unsigned...
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?

Last edited by quantt; 02-08-2009 at 12:20 AM.
 
Old 02-08-2009, 07:14 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 02-08-2009 at 07:15 AM.
 
Old 02-08-2009, 05:20 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think
[quote]
p->str[0] = 0;
[/code]
should be
Code:
p->str[0] = '\0';
ie null string terminator, not the value zero.
 
Old 02-08-2009, 08:10 PM   #13
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by chrism01 View Post
I think
Code:
p->str[0] = 0;
should be
Code:
p->str[0] = '\0';
ie null string terminator, not the value zero.
[I took the liberty of changing [quote] to [code] near the beginning of that 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;
is legal where and only where the statement
Code:
p->str[0] = '\0';
is legal, provided that what is being assigned to is of type char or unsigned char. Furthermore, they both compile to the same code. The string terminating character has, in fact, the value zero. The only difference is in how it looks to humans reading the code, so the difference is arguably useful for documentation purposes.

Here's a demonstration. It consists of two bash scripts.

Script 1.sh:
Code:
#!/bin/bash

cat > 1.c <<EOD
int main(void)
{
  typedef struct { char str[8]; } mytype_t, *mytype_p;

  mytype_t t;

  mytype_p p;

  p=&t;

  p->str[0]=0;
  p->str[0]='\0';

  return 0;

} /* main() */
EOD
gcc 1.c -Wall -S
cat -n $0
echo ===
cat -n 1.s
Script 2.sh:
Code:
#!/bin/bash

cat > 2.c <<EOD
int main(void)
{
  typedef struct { char str[8]; } mytype_t, *mytype_p;

  mytype_t t;

  mytype_p p;

  p=&t;

  p->str[3]=5;
  p->str[3]='\005';

  return 0;

} /* main() */
EOD
gcc 2.c -Wall -S
cat -n $0
echo ===
cat -n 2.s
When you diff the two scripts, you get this output:
Code:
3c3
< cat > 1.c <<EOD
---
> cat > 2.c <<EOD
14,15c14,15
<   p->str[0]=0;
<   p->str[0]='\0';
---
>   p->str[3]=5;
>   p->str[3]='\005';
21c21
< gcc 1.c -Wall -S
---
> gcc 2.c -Wall -S
24c24
< cat -n 1.s
---
> cat -n 2.s
The output from the first script is this. I've colored the appropriate assembler statements in red.
Code:
     1  #!/bin/bash
     2
     3  cat > 1.c <<EOD
     4  int main(void)
     5  {
     6    typedef struct { char str[8]; } mytype_t, *mytype_p;
     7
     8    mytype_t t;
     9
    10    mytype_p p;
    11
    12    p=&t;
    13
    14    p->str[0]=0;
    15    p->str[0]='\0';
    16
    17    return 0;
    18
    19  } /* main() */
    20  EOD
    21  gcc 1.c -Wall -S
    22  cat -n $0
    23  echo ===
    24  cat -n 1.s
===
     1          .file   "1.c"
     2          .text
     3  .globl main
     4          .type   main, @function
     5  main:
     6          leal    4(%esp), %ecx
     7          andl    $-16, %esp
     8          pushl   -4(%ecx)
     9          pushl   %ebp
    10          movl    %esp, %ebp
    11          pushl   %ecx
    12          subl    $16, %esp
    13          leal    -16(%ebp), %eax
    14          movl    %eax, -8(%ebp)
    15          movl    -8(%ebp), %eax
    16          movb    $0, (%eax)
    17          movl    -8(%ebp), %eax
    18          movb    $0, (%eax)
    19          movl    $0, %eax
    20          addl    $16, %esp
    21          popl    %ecx
    22          popl    %ebp
    23          leal    -4(%ecx), %esp
    24          ret
    25          .size   main, .-main
    26          .ident  "GCC: (GNU) 4.2.3"
    27          .section        .note.GNU-stack,"",@progbits
The output from the second script is this. I've colored the appropriate assembler statements in red.
Code:
     1  #!/bin/bash
     2
     3  cat > 2.c <<EOD
     4  int main(void)
     5  {
     6    typedef struct { char str[8]; } mytype_t, *mytype_p;
     7
     8    mytype_t t;
     9
    10    mytype_p p;
    11
    12    p=&t;
    13
    14    p->str[3]=5;
    15    p->str[3]='\005';
    16
    17    return 0;
    18
    19  } /* main() */
    20  EOD
    21  gcc 2.c -Wall -S
    22  cat -n $0
    23  echo ===
    24  cat -n 2.s
===
     1          .file   "2.c"
     2          .text
     3  .globl main
     4          .type   main, @function
     5  main:
     6          leal    4(%esp), %ecx
     7          andl    $-16, %esp
     8          pushl   -4(%ecx)
     9          pushl   %ebp
    10          movl    %esp, %ebp
    11          pushl   %ecx
    12          subl    $16, %esp
    13          leal    -16(%ebp), %eax
    14          movl    %eax, -8(%ebp)
    15          movl    -8(%ebp), %eax
    16          movb    $5, 3(%eax)
    17          movl    -8(%ebp), %eax
    18          movb    $5, 3(%eax)
    19          movl    $0, %eax
    20          addl    $16, %esp
    21          popl    %ecx
    22          popl    %ebp
    23          leal    -4(%ecx), %esp
    24          ret
    25          .size   main, .-main
    26          .ident  "GCC: (GNU) 4.2.3"
    27          .section        .note.GNU-stack,"",@progbits
Just for kicks and giggles, here's the diff of the assembler output:
Code:
1c1
<       .file   "1.c"
---
>       .file   "2.c"
16c16
<       movb    $0, (%eax)
---
>       movb    $5, 3(%eax)
18c18
<       movb    $0, (%eax)
---
>       movb    $5, 3(%eax)
 
Old 02-09-2009, 09:19 AM   #14
quantt
Member
 
Registered: Aug 2008
Posts: 62

Original Poster
Rep: Reputation: 15
[QUOTE=chrism01;3436526]I think
Quote:
p->str[0] = 0;
[/code]
should be
Code:
p->str[0] = '\0';
ie null string terminator, not the value zero.
which's way I can do it?
Code:
struct data{
unsigned char adr [16];
};
int main(){
struct data *p;
...
unsigned char buf[16];
unsigned char *str;
   for (i=0;i<4; i++) {
      id[i] = (address >> (24-i*8)) & 0xFF;
sprintf (str, "%u%c", id[i], (i==3) ? '\n' : '.');
str=buf;
buf=p->adr;//incompatible types in assignment
printf("%s", buf);// it's worked
printf("%s", p->adr);// need this
}//for
return 0;
}//main

Last edited by quantt; 02-09-2009 at 09:26 AM.
 
Old 02-09-2009, 09:48 AM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
quantt -

Consider the following code. Does this meet your needs?

PHP Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct data
{
  
unsigned char adr[16];
};


int main()
{
  
unsigned int address 0xC0A8010A;

  
struct data *calloc(1sizeof(struct data));

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

    
sprintf(p->adr strlen(p->adr), "%u%c"id, (== '\0' '.'));
  }

  
printf("%s\n"p->adr);

  
// ...

  
free(p);

 
  


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 08:07 PM.

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