Share your knowledge at the LQ Wiki.
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-09-2007, 07:23 PM   #1
LQ Newbie
Registered: Apr 2007
Posts: 13

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


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?


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

Rep: Reputation: 115Reputation: 115
I'm suspecting you need this to call execv. Try
int i = 0;
char **args = malloc((argc + 1) * sizeof(char *));
while(i < argc) {
   args[i] = argv[i + 1];
args[argc] = 0;
If you're adventurous, you can also try
int i = 0;
while(i < argc) {
   argv[i] = argv[i + 1];
But what you really need to know is that strcpy isn't always appropriate, and direct assignment is okay sometimes.
Old 04-09-2007, 08:17 PM   #3
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 09:01 PM.
Old 04-10-2007, 12:48 AM   #4
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

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

*argv[] is = to **argv


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

All times are GMT -5. The time now is 05:47 AM.

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