?
Solved

Copying characters from ptr to char string into an multidemensional array

Posted on 2003-03-03
4
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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