LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi,

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!

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

buffer[j]=real_command_line[i];
i++, j++;

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

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 04:06 PM.
 
Old 04-12-2007, 06:05 PM   #2
haydari
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.
 
  


Reply



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 06:44 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM
pointers and strings skibud2 Programming 4 09-22-2003 07:38 AM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 10:58 AM
C: pointers, strings, splitting headache notsoevil Programming 5 06-19-2003 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 PM.

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