In my programming, the user enters a string containing numbers, and the system needs to dynamically allocate an array of integers to receive these numbers, converting the character numbers in the string to the integer numbers in the int array one at a time. And then you add up these numbers. That is my code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char integral[10]; // create an char array to store at most 9 letters
printf("Enter a string that include number: ");
scanf("%9s", integral); // record the string
int length = strlen(integral); // get the whole length of array after users finished
int *array; // create an integral pointer
array = malloc(sizeof(int) * (length + 1)); // Dynamically allocate an array size that can hold exactly all the numbers
strcpy(array, integral); // copy the number from array to integral
int sum = 0;
for (int i = 0; i < length; i++) {
sum += array[i]; // get the sum
}
free(array);
printf("The sum is : %d", sum); // print the result
return 0;
}
But there are some errors happened after complied:
warning: passing argument 1 of ‘strcpy’ from incompatible pointer type on line 13.note: expected ‘char * restrict’ but argument is of type ‘int *’.
How to modify my code?