Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-12-2007, 04:39 PM   #1
LQ Newbie
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
need help, going mad about array pointers and strings...


I have a problem and I am very close to going crazy. If someone could help me, I would be appreciate...

struct command_line
char *command_name;
char **args;

main ()

struct command_line *command;
command=(struct command_line *)malloc(sizeof (struct command_line);

char real_command_line[500];
i=0,j=0, k=0;

command->args=malloc((n+1) * sizeof(char *));

//assume real_command_line has "please be done" in it. n=3, and the allocation for args will be 4.
//the below parts takes "please", copies it in a string named buffer, equals buffer to command->args[k], then takes "be" and again the same and "done" and the same operations are done. but command->args[k] has a null value everytime and I can't solve it!

if(real_command_line[i]==' ' || real_command_line[i]=='\0' )
buffer[j]='\0'; j=0;
i++; printf("buffer %s\n", command->args[k] );

i++, j++;

buffer[j]='\0'; j=0;
printf("buffer %s\n", command->args[k]);

printf results are ok.

but after this step, when I try to printf command->args[0],
it gives the last string which is "done". Actually all command->args[0], command->args[1], command->args[2] is getting equal to "done". I don't know why and how can I solve this...

Last edited by haydari; 04-12-2007 at 05:06 PM.
Old 04-12-2007, 07:05 PM   #2
LQ Newbie
Registered: Apr 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Well, I have done it!

If someone is curious how ( ), I used an extra pointer array like char **array;
and allocated it's columns and rows and used it instead of the array buffer. I think somehow using a single array like buffer[250] overwrites all the rows of the **args pointer array. I don't know why, but that's the case.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Pointers to C Strings anamericanjoe Programming 7 12-13-2006 07:44 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 01:22 PM
pointers and strings skibud2 Programming 4 09-22-2003 08:38 AM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 11:58 AM
C: pointers, strings, splitting headache notsoevil Programming 5 06-19-2003 05:04 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration