I have the following bit of code:
As a global variable:
char *orderFiles[10];
And then my main method:
int main(int argc, char *argv[])
{
orderFiles = argv;
}
However it keeps giving me an error. What am I doing wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s giving you an error because
char *x[10]gives you an array of ten char pointers which is non-modifiable. In other words, you cannot assign tox, nor change it in any way. The equivalent changeable version would bechar **orderFiles– you can assignargvto that just fine.As an aside, you could transfer individual arguments to your array thus:
but that seems rather convoluted. It will either fill up
orderFileswith the firstNarguments or partially fill it, making the next one NULL.If your intent is simply to stash away the arguments into a global so that you can reference them anywhere, you should do something like:
That code saves the arguments into globals so they can be accessed in a different function.
You should save both arguments to
mainif you want to useargcas well although, technically, it’s not necessary sinceargv[argc]is guaranteed to be NULL for hosted environments – you could use that to detect the end of the argument array.