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-04-2009 08:27 AM

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.

jiml8 02-04-2009 08:35 AM

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;
}


quantt 02-05-2009 06:33 AM

...

quantt 02-06-2009 07:46 AM

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);
}


johnsfine 02-06-2009 08:12 AM

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.

quantt 02-06-2009 10:50 AM

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.

wje_lq 02-06-2009 12:07 PM

Quote:

Originally Posted by quantt (Post 3434122)
I've another question about that bicycle.

Forgive me, but I can't resist.

What bicycle?

quantt 02-07-2009 05:06 AM

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);

johnsfine 02-07-2009 09:23 AM

Quote:

Originally Posted by johnsfine (Post 3434143)
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 (Post 3434993)
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);

quantt 02-07-2009 09:55 PM

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?

johnsfine 02-08-2009 07:14 AM

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.

chrism01 02-08-2009 05:20 PM

I think
[quote]
p->str[0] = 0;
[/code]
should be
Code:

p->str[0] = '\0';
ie null string terminator, not the value zero.

wje_lq 02-08-2009 08:10 PM

Quote:

Originally Posted by chrism01 (Post 3436526)
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)


quantt 02-09-2009 09:19 AM

[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


dwhitney67 02-09-2009 09:48 AM

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);




All times are GMT -5. The time now is 07:04 AM.