My current problem is to read in an unknown number of integers from stdin
. My approach is to use gets() to store the entire line as a char array (char str[50]
). I am trying to parse the char array and convert each "string int" to an integer and store in an int array. I tried using strtol (nums[i]=strtol(A, &endptr, 10)
where A
is the char array. However, endptr doesnt seem to store anything when the rest of the A are also numbers. For example, if A is "8 hello" endptr=hello, but when A is "8 6 4" endptr is nothing.
Is there a better approach? Is this possible with atoi
? Any help is greatly appreciated! Thanks!
char A[1000];
long nums[1000];
printf("Enter integers: ");
gets(A);
char *endptr;
int i=0;
while(endptr!=A){
nums[i]=strtol(A, &endptr, 10);
i++;
}