I have to parse this string and fill these parameters in the structure.Example I have to parse -dh 500 and fill height element in the structure.I can parse this string but logic is becoming very complex and again

Let me know if you have any simple approach.Reply if you are too busy with your work.

Start with carefully studying the problem, in this case, the strings. KNOW those strings - know what are the separators between the fields, know how many fields they might have AT THE MOST.

STUDY & KNOW the problem, until you know how to solve it. Then set up the logic you used, to solve it. Put that logic into a flowchart or pseudo code, so each step of an algorithm to solve the problem, can be identified.

THEN write your code (which should spring right out from the pseudo code or flowchart).

Put off all the details, and get the flow of the program, and the functions you want for it, set up. Check your work as you go for any compiler warnings or errors, and for the accuracy of the program, so far.

Then add your details that you put off, and make any final little adjustments (typically, this will be to the look and feel of the interface).

Welcome to Top Down design! ;)

When you say:

A structure that assumes no name or value will be larger than 16 bytes

Who says you want char's? Why 16 bytes? That looks so much like a "magical number" you just pulled out of the hat.

Start with the strings, Luke! Turn off your target acquisition system in your X-wing fighter! ;)

Yes, you did, and my warnings were not intended for your example, but for Pinkannu. I don't want to see code from the OP, until after the problem has been thoroughly studied, and the logic to solve it, has been found.

I don't even know what the blazes you're doing with that code. *Argv[] is an array of pointers to char, so it should work fine with a string.

Forget code for a second. Show several strings as examples of your input. Then show the output that you want. Then show the pseudo code logic that you plan on using in your program to make that happen.

I know as a beginner, the C syntax can be a major hurdle, but ignore it for right now. Assume that you can get the syntax to work, with a little help, and it's not a problem. Concentrate on the logic, and program flow of that logic.

Hi Adak,
following are the details.
1.I am launching another process from my application.
2.While launching I will pass shared memory name where input parameter string is placed.i.e parameter sharing is done using shared memory.
3.So in launched process I will get shared memory name in argv.I will open that memory and copy string to local character array.
4.Then I have to parse this string Ex.

"-dAwin Main_Window -dh 200 -dw 500 ...."

where -dAwin -> Application window name
-dh -> Window height
-dv -> Window width.
-dAwin , -dh , -dw are the identifiers and they are followed by corresponding values.
5.I have to parse this string and fill following structure.

struct Input_Param
{
char ApWinName[255];
int Height ;
int width;
};

This is just a sample.I have around 30 such parameters to parse.So string is quite big.
Note :- I am getting only shared memory name in char **argv.Also sequence and length may change.So I can not assume string will always come as shown in above example. It can come as shown below.

Looks like -dH is the field identifier for your struct, where "H" represents a unique handle {w, Win, h, etc.}.

So make a list of these field id's: -dh, -dw, -dWin, etc., and use strstr() to return a pointer to them, wherever they are, in the string. Use that pointer to then assign the value they point to, to your struct fields.

You'll need to include <string.h> for strstr(), and use sscanf() to store your value (since it is formatted data) from a string). Make your buffer plenty large enough to handle one string - maybe BUFSIZ (which varies from system to system, but 512 chars size is common these days). (BUFSIZ is a macro).

Heaven help us, shared memory. You couldn't just pass the string to a function?

Anyway, one thing at a time, let's get the parse going locally, and then rest, later.

If you simply need to parse the string and still have key/value pairs, I suggest using strtok(). Get odd-numbered tokens as the key and even-numbered tokens as the value. Once you get the left-side, you can remove or ignore the -d and evaluate the key.

If you simply need to parse the string and still have key/value pairs, I suggest using strtok(). Get odd-numbered tokens as the key and even-numbered tokens as the value. Once you get the left-side, you can remove or ignore the -d and evaluate the key.