> Each array element stores a string.
Correction, each array element points to a string.
Main Topics
Browse All TopicsCommand line arguments..
Hi,
I was tryng to understand pointers and arrays together and came accross to command line arguments..
int main(int argc,char *argv[])
{
...
}
And when we want to use teh value, we removed the asterisk .
for example,
sum+=atoi(argv[0]);
and it is not atoi (*argv[0])
I tested the program and it works but I dont understand how cud there be a pointer and array together..
char *argv[]
Lets say "100" is argument 1 , how to access individual character of it . argv[0][0]..
also, if i declare a double like below , what does it mean..
double *a[12]..
any clarification would help ....thank you
I guess, i dont understand what a pointer and [] together means...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
->Each array element stores a string.
->Correction, each array element points to a string.
Does it mean that the memory has already been allocated for the string ?
Also,
argv[0][0] points to first argument first character ..but how is it in pointer .i mean in array it is like this : argv[0][0], but in pointer ? is it something like (argv+i) ....
Also, in double , what does it mean?double *a[12]..
thanks
Hi
The argument int argc is an argument counter , it holds the information about the number of arguments passed to the main() function
The second argument char *argv[] is an argument vector , ( literally its a two dimensional character array , have the capability to hold 'n', strings
one of the reason for using strings is , string composed of character , numbers , special symbols , and so it will be easy to convert strings into other existing data types , thats why here in this program the function atoi() is employed to convert the string into integer
The arguments to a program are passed via two values :
1) argc contains the number of arguments that were passed (including the name of the program)
2) argv contains an array of the arguments (one string each). There will be argc arguments in the array, and the first will be the name of the program.
So, as said before, argv is an array of char* (and a char* is basically a string in C). In memory, that looks like this :
------------- --------------------------
| argv[0] | --> | 'p' | 'r' | 'o' | 'g' | 'r' | 'a' | 'm' | '_' | 'n' | 'a' | 'm' | 'e' | '\0' |
------------- --------------------------
| | --------------------------
| argv[1] | --> | 'f' | 'i' | 'r' | 's' | 't' | '_' | 'a' | 'r' | 'g' | 'u' | 'm' | 'e' | 'n' | 't' | '\0' |
------------- --------------------------
| | --------------------------
| argv[2] | --> | 's' | 'e' | 'c' | 'o' | 'n' | 'd' | '_' | 'a' | 'r' | 'g' | 'u' | 'm' | 'e' | 'n' | 't' | '\0' |
------------- --------------------------
So, to access the first argument that was passed to the program, you use argv[1].
To access the third character of the first argument, you use argv[1][2].
>> Does it mean that the memory has already been allocated for the string ?
Yes, the memory has already been allocated for you.
>> also, if i declare a double like below , what does it mean..
>> double *a[12]..
It means you create an array of double pointers. You get something like this in memory :
-----------
| a[0] | -->
| |
-----------
| a[1] | -->
| |
-----------
...
-----------
| a[11] | -->
| |
-----------
So, an array of pointers to double that don't point to anything at the moment.
To use them, you need to let the pointers point to something, either by letting them point to already existing memory, or by allocating new memory for them.
Suppose you allocate new memory for them like this :
for (i = 0; i < 12; ++i) {
a[i] = (double*) calloc(1, sizeof(double)); /* we allocate memory for 1 double, but we could have allocated more */
}
then we get this :
----------- ------------
| a[0] | --> | |
| | ------------
----------- ------------
| a[1] | --> | |
| | ------------
-----------
...
----------- ------------
| a[11] | --> | |
| | ------------
-----------
We can now put values in there, by doing for example :
*(a[1]) = 0.5;
which gives us :
----------- ------------
| a[0] | --> | |
| | ------------
----------- ------------
| a[1] | --> | 0.5 |
| | ------------
-----------
...
----------- ------------
| a[11] | --> | |
| | ------------
-----------
And so on.
Remember that I only allocated memory for 1 double ? We could have allocated memory for more than one double like this :
for (i = 0; i < 12; ++i) {
a[i] = (double*) calloc(3, sizeof(double)); /* we allocate memory for 3 doubles */
}
then we get this :
----------- --------------------------
| a[0] | --> | | | |
| | --------------------------
----------- --------------------------
| a[1] | --> | | | |
| | --------------------------
-----------
...
----------- --------------------------
| a[11] | --> | | | |
| | --------------------------
-----------
We can now put values in there, by doing for example :
a[1][2] = 0.5;
which gives us :
----------- --------------------------
| a[0] | --> | | | |
| | --------------------------
----------- --------------------------
| a[1] | --> | | | 0.5 |
| | --------------------------
-----------
...
----------- --------------------------
| a[11] | --> | | | |
| | --------------------------
-----------
And so on.
Note that since we allocated dynamic memory ourselves, that we also need to clean it up ourselves once we don't need it any more :
for (i = 0; i < 12; ++i) {
free(a[i]);
}
>> I guess, i dont understand what a pointer and [] together means...
You have to read them as they are defined. In this case, it's an array ([]) of pointers (*) to char, and the array is named argv :
char* argv[]
Does that make more sense ?
Business Accounts
Answer for Membership
by: nixfreakPosted on 2007-05-26 at 21:53:38ID: 19163569
char *argv[]
argv is an array of char pointers.
Each array element stores a string.
The first element is the name of the running program.
Second element onwards contain the arguments passed to the program.
The last array entry is NULL, so that we know where it ends :)