Solved

extract data from char array

Posted on 2003-11-17
7
578 Views
Last Modified: 2010-04-15
Hi All,

can some one give me some sample code for parsing character to return for example middle 5 characters from a buffer or the last 2.

What i tried initially was to cast a character array to a structure with all character arrays as the fields in the struct. (because i know the format of the char array). Why doesn't this work?

char array[10] = "1234567890"

typedef struct {
  char field1[2];
  char field2[5];
  char field3[3];
}s_test;

char *ptr = array;

s_test *data = (s_test *)ptr;

data->field1 should equal "12"?
data->field2 should equal "34567"?
data->field3 should equal "890"?

Thanks




0
Comment
Question by:pesst
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9768947
>can some one give me some sample code for parsing character to return for example middle 5 characters
>from a buffer or the last 2.

char buffer[200];
char result[10];

/*buffer holds your string and result is where you want chars 15-20 */

strncpy ( result, buffer+15, 5 );
buffer[6] = '\0';

>s_test *data = (s_test *)ptr;
compiler will not allow the cast between char * and a struct ... use memcpy

char array[10] = "1234567890"

typedef struct {
 char field1[2];
 char field2[5];
 char field3[3];
}s_test;

struct s_test test;
memcpy ( test, array, 10 );

make sure that you know the format and behaviour of the array throughout the program or you may end up pulling you hair our
also portability of such tricks is suspect due to issues like structure padding
0
 
LVL 5

Expert Comment

by:migoEX
ID: 9770981
Note, that even in this case, where field[2] is "34567", you can't use it in str* functions, as next character is not '\0'. actually, this definition is equivalent to:

typedef struct {
   char buf[11];
   char* field1;
   char* field2;
   char* field3;
} s_test;

struct s_test test;
strcpy( test.buf, "1234567890" );
test.field1 = test.buf;
test.field2 = test.buf + 2;
test.field3 = test.field2 + 5;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9771292
> Why doesn't this work?

I tried your code and the only thing to change was

    char array[11] = "1234567890";

because a terminating zero character must be added to a quoted string constant;

The contents of data was correct, i. e.

       data->field1[0] = '1';
       data->field1[1] = '2';
       ...
       data->field3[2] = '0';

However, only data->field3 may be correctly used as a string by string functions because of the terminating zero.

Regards, Alex

       
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:rstaveley
ID: 9777700
data->field2 does indeed get you the address of the expected portion of string, but in the absence of '\0' termination, you need to supply printf additional information to let it know how many characters to print.
--------8<--------
#include <stdio.h>

char array[10] = "1234567890";
typedef struct {
  char field1[2];
  char field2[5];
  char field3[3];
} s_test;
s_test *data = (s_test*)array;

int main()
{
        printf("data->field1 should equal \"%.2s\"\n",data->field1);
        printf("data->field2 should equal \"%.5s\"\n",data->field2);
        printf("data->field3 should equal \"%.3s\"\n",data->field3);
}
--------8<--------
0
 
LVL 2

Author Comment

by:pesst
ID: 9777772
Thanks for all the information guys. I went ahead and just created a separate array for each field and used:

strncpy(field1,data, 2)
field1[2] = '\0'

strncpy(field1,data+2, 5)
field2[5] = '\0'

strncpy(field1,data+7, 3)
field3[3] = '\0'

So I'll give the points to the first answer.


0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9778127
Also... Notice that you have set the sizeof(array) to 10. This means that array doesn't get a '\0' terminator. If you'd not explicitly set the size to 10, sizeof(array) would be 11 and a '\0' terminator would be added.

You'd be lucky to get away with doing a printf on the array without specifying its length in the format specifier, because of the lack of '\0' terminator.

The following illustration uses a byte-aligned struct to illustrate the point.
--------8<--------
#include <stdio.h>

/* Byte alignment - otherwise we'd get padding spoiling the illustration :-) */
#pragma pack(1)
struct X {
char array[10];
char secret[10];
};
#pragma pack()

struct X s = {
        "1234567890"      /* 10 characters - no space for '\0'! */
        ,"abcdefgh"      /* 8 characters - fits in with '\0' termination */
        };

int main()
{
        printf("The s.array pointer gets us \"%s\" (s.array's size is %u)\n",s.array,sizeof(s.array));
}
--------8<--------

By byte aligning the structure, I ensured that the compiler doesn't word align secret and put it on a word boundary leaving zero padding between array and secret. This would effectively give you '\0' termination. You should see the same effect without byte-alignment on a 32-bit system, if you made the array size 8 chars (or 4, 12, 16 etc.).

e.g. This should show the effect on most 32 or 16 bit systems:
--------8<--------
#include <stdio.h>

char array[8] = "12345678"; /* 8 characters - no space for '\0'! */
char secret[] = "abcdefgh"; /* A 32-bit compiler will probably put this in memory following stright on from array */

int main()
{
        printf("The array pointer gets us \"%s\" (size of array is %u)\n",array,sizeof(array));
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9778142
[Sorry I posted that rather too late. Had it minimised in a window, while I was doing other stuff.]
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now