c - Store argv to an int array -
i have code store argv
dynamically allocated int
array:
int *data; // pointer array of integer numbers int size; // size of data array int main(int argc, char* argv[]) { // initialize array data size=argc; printf("%dsize=\n",size); printf("%d\n",argc); data=(int*)calloc(size,sizeof(int)); int i=0; (i=1;i<size;i++) { data[i]=argv[i]; printf("%d\n",data[i]); } (i=1;i<argc;i++) { printf("%d\n",argv[i]); } return 0; }
when run in command line: ./sumprime 5 1 2 3 4
prints:
6size= 6 15311660 15311662 15311664 15311666 15311668 15311660 15311662 15311664 15311666 15311668
not array of 5 1 2 3 4
expected. how can store int
array argv
, print out?
should be:
data[i]=atoi(argv[i]);
remember command line argument strings. though pass integer in command line string needs converted integer.
Comments
Post a Comment