Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Copying characters from ptr to char string into an multidemensional array

Posted on 2003-03-03
4
Medium Priority
?
268 Views
Last Modified: 2010-04-15
My problem is I get junk data when trying to copy over a char from a ptr char string and put it into an array which is two-dimensional.  My piece of code is as follows:

char argv_reduced[argcount][MAXBUF], nextchar;
int l, m = 0;
for(l = 0; l < argcount; l++                    {
nextchar = *((args[m]) + m);
argv_reduced[l][m] = nextchar;                    m++;
while(nextchar == ' ')
{
l++;
m++;
nextchar = *((args[m]) + m);
}
                             
}

AFTER extensive debugging, I notice nextchar gets the character value, but the argv_reduced[l][m] assignment gets junk data.

Thank you
0
Comment
Question by:themaxxx
4 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8057615
Are you sure that the line:

nextchar = *((args[m]) + m);

is giving you what you want?

That would be the 0th character of the 1st string, the 1st character of the 2nd string, etc...  Seems odd that that is what you would be trying to do...

Can you describe, perhaps with examples, exactly what you are trying to accomplish and what is "junk" data?  Is it truly not characters that are part of the expected solution, or are those characters simply not in the expected order?
0
 
LVL 22

Expert Comment

by:grg99
ID: 8057622

Some questions and suggestions:


char argv_reduced[argcount][MAXBUF], nextchar;

>> What is argcount, a variable or a constant?

int l, m = 0;

> Not a great idea to use the letter "l" as a variable-- too easy to confuse it with the digit "1".


for(l = 0; l < argcount; l++                    {
nextchar = *((args[m]) + m);

>> What the heck is "args"?  Not declared above.
And it doesnt make much sense to use m twice in this stmt,
unless you intend to get the m'th character of the m'th string.


argv_reduced[l][m] = nextchar;                    m++;

while(nextchar == ' ')

>>> Is this loop in the right place? First you store a space, then you (not quite right) skip over spaces, but you also are bumping "l", which will skip you down the argument list once for each character.

{
l++;
m++;
nextchar = *((args[m]) + m);
}
                             
}

>>>  it looks like it would help to give your l and m variable names more descriptive names, then it might be more obvious where you're using and/or incrementing the right thing at the right place.

Regards,

grg99

0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 400 total points
ID: 8058218

Hi Themaxxx,

Grg99 is correct in his suggestion that i, j, k, l, m etc are not very descriptive variable names.  Even if your routine is made to work, 4 months from now you'll go through the same headache when you want to "enhance" it.

That being said, I think I see where you're heading.  You're scanning the programs arguments and trying to modify them into a new array.  Try this:

/*  Make Sure that MAXARGS and MAXBUF are large enough
    The algorithm requires that each item is a zero-terminated string
*/

char ArgvReduced[MAXARGS][MAXBUF];
char *SourcePtr, *DestPtr;

int  ArgIndex;

/*  Assuming you don't want to parse the program name in argv[0], ArgIndex starts at 1 */

for (ArgIndex = 1; ArgIndex < ArgCount; ArgIndex++)
{
  SourcePtr = argv[ArgIndex];
  DestPtr = ArgvReduced[ArgIndex];
  while (*SourcePtr)  /* Scan until end-of-string */
  {
    if (*SourcePtr != ' ')  /*  squeeze out blanks  */
      *(DestPtr++) = *SourcePtr;
    SourcePtr++;
  }
  *DestPtr = 0;  /*  Zero terminate the new string  */
}

Of course, if you're using argv and argc from the command line, the only blanks will be those that were enclosed in quotes.

This should give you a good head start.

Kdo

0
 

Author Comment

by:themaxxx
ID: 8060393
Sorry for not being so descriptive and for labelling my loop variables poorly.  With that out of the way, the program I have to write is a simple shell running under unix/linux.  So it doesn't seem that bad until I hit pointer arrays to character strings, and it got confusing.  The algorithm Kdo supplied is actually similar to what I found out today in C book for Unix that talks about a simple shell program.  Thanks for all the tips and that algorithm.

-Tony
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

572 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