Sameer Reza Khan - 1 year ago 58
C Question

# how to convert dynamically created character array into integer array when the input is integers delimited with space

I want to create two arrays, one is a character array and the second is an integer array. Both are created dynamically when user provides input about the number of integers element. All the integers are delimited by spaces.

Input:

First line consists of

`N`
, Here
`N`
is number of integers. Say user inputs 5, the next line will contain
`N`
integers delimited by space. i.e 5 integers
`5 10 23 456 2`

Output:

Output will show the elements from the integer array i.e
`int_array[i], int_array[i+1]`
....
`5,10`
...

But when I tried with my code, I can see only 0's from integer array.

Here is the code:

``````#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

int *cstois(char *char_array, int *int_array, int n) {
int i, j;
for (i = 0, j = 0; i < n; i++) {
if (char_array[i] >= '0' && char_array[i] <= '9') {
int_array[j] == int_array[j] * 10 + (char_array[i] - '0');
} else
if (char_array[i] == ' ') {
j++;
} else
continue;
}
return int_array;
}

int main() {
int i, n;
printf("enter no. of elements");
scanf("%d\n", &n);
char *char_array;
char_array = (char*)malloc(n * sizeof(int));
fgets(char_array, sizeof(char_array), stdin);
int *int_array = (int*)calloc(n, sizeof(int));
cstois(&char_array[0], &int_array[0], n);
for (i = 0; i < n; i++)
printf("%d\n", int_array[i]);
free(char_array);
free(int_array);
return 0;
}
``````

There are multiple problems in your code:

• There is no standard header `<malloc.h>`. `malloc()` is defined in `<stdlib.h>`.

• in function `cstois()`, you do not correctly update the number because of a typo: `int_array[j] == int_array[j] * 10 + (char_array[i] - '0');` should be `int_array[j] = int_array[j] * 10 + (char_array[i] - '0');`

• You ignore all characters that are not digits nor the space character. This is not necessarily correct: `1,2,3` would be parsed as `123` instead of being reported as an error.

• Much worse even, you do not check for `'\0'` when scanning the input buffer. If there are not enough numbers in the line read from the user, you scan beynd the end of the string and potentially beyond the end of the array, invoking undefined behavior.

• You skip to the next element upon the space character. Separating numbers with multiple spaces would cause numbers to be skipped incorrectly.

• You cannot handle negative numbers.

• in function `main()`, the input array is allocated as `char_array = (char*)malloc(n * sizeof(int));`. This is incorrect as it limits the average number of digits to 3 for 32 bit ints... You should allocate at least 20 digits per number to allow for large integers.

• The line read from the user with `fgets()` is tragically short: `sizeof(char_array)` is the size of the pointer, not the size allocated for the array. Save that size to a variable and pass it to both `malloc()` and `fgets()`.

• You do not check the return value of `scanf()`, nor that of `fgets()` or `malloc()`. An empty file will not be handled correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download