Reputation: 33
This is my abstract data structure
typedef struct {
int *items;
int size;
} List;
I would like the user to enter integers on a single line such as
a.out
12 14 2 8 9
and read them into the List. I understand how to add to a list, i guess the thing i don't get is getting the integers from a single line input
Edit: Sorry but I meant using something like scanf not with command line arguments
Upvotes: 1
Views: 71
Reputation: 16607
1. Definition of your main
should be int main(int argc,char **argv)
2. The numbers will command line arguments (check value of argc
greater than 1
before using argv
).
3. argv[1]
, argv[2]
will have these numbers , but as string .
4. Convert these to integers using atoi
or sscanf
functions and store in structure members as you desire.
Edit: Sorry but I meant using something like scanf not with command line arguments
You can use fgets
, tokenize string using strtok
and then convert and store into integer variable.
Upvotes: 1
Reputation: 9800
You have to use input arguments like:
Your main function will look like: int main (int argc, char *argv[] )
In this case you can add your argument at command line as you would
./a.out 12 14 2 8 9
And you can access those argument by argv[1], argv[2], argv[3], ...
and you can loop over the number of arguments the user provided which is contained in the argc variable
example to access the first argument:
int i;
i= atoi(argv[1]);
Upvotes: 0