LinuxQuestions.org
Review your favorite Linux distribution.
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-09-2007, 06:23 PM   #1
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
How can I assign to a pointer array like char *args[]; ?


Hi,

I need to assign to an array like char *args[];

for example I have some data as parameters in the *argv[] array.
I need to assign these values starting from argv[1] to argv[argc-1] into the *args[] array.

for (i=1; i<argc; i++)
strcpy(args[i-1], argv[i]);

That doesn't work. Any idea how can I handle this?

Thanks.

Last edited by haydari; 04-09-2007 at 06:26 PM.
 
Old 04-09-2007, 06:55 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I'm suspecting you need this to call execv. Try
Code:
int i = 0;
char **args = malloc((argc + 1) * sizeof(char *));
while(i < argc) {
   args[i] = argv[i + 1];
   i++;
}
args[argc] = 0;
execv...
If you're adventurous, you can also try
Code:
int i = 0;
while(i < argc) {
   argv[i] = argv[i + 1];
}
execv...
But what you really need to know is that strcpy isn't always appropriate, and direct assignment is okay sometimes.
 
Old 04-09-2007, 07:17 PM   #3
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you very much, it worked.

Yes, I will use it with execve :=). I have an assignment about creating a shell terminal executing shell commands with parameters, using execve command.

In your code, could you tell me why do I need to use double pointer, **args? And I hope any error won't appear when I use it like execve(pathname, args, NULL); because of it is a double pointer... (I didn't have enough time to check this in the lab, will try it tomorrow..)

Last edited by haydari; 04-09-2007 at 08:01 PM.
 
Old 04-09-2007, 11:48 PM   #4
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
Code:
 int execve(const char *filename, char *const argv[],
                  char *const envp[]);
2nd argument is nothing but **args

*argv[] is = to **argv
 
  


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
Cannot assign NULL to array? exvor Programming 7 10-31-2006 01:00 PM
C : Function to modify pointer to char introuble Programming 2 06-21-2006 12:24 PM
c char* pointer function question true_atlantis Programming 9 04-14-2006 01:29 PM
Assign a command to 'Alt Gr' itself and '&#711;' char in UT/Q3A kilgor Linux - Games 0 03-08-2005 03:50 PM
is *- -p = '\n' valid? can i set \n char to a pointer? feetyouwell Programming 1 10-01-2004 01:09 AM

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

All times are GMT -5. The time now is 12:58 AM.

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