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. First, warning: passing argument 1 of ‘strcpy’ from incompatible pointer type on line 13. Second, note: expected ‘char * restrict’ but argument is of type ‘int *’.
How to modify my code?
Thank you all.
CodePudding user response:
What you are trying to do can NOT work.
You try to use the strcpy(restrict char*, restrict char*)
, even if it can only take two char
pointer which do not point to the same address. However, you try to pass an int
pointer to the function.
The reason for your code being utterly erroneous is quite simple : a char
is encoded on 1 byte, whereas an int
is encoded on 2 or 4 bytes (depending on your architecture).
Moreover, even if by whatever shenanigan your compiler accepted to copy as you wish, there would be no conversion. Instead, you would just have the ascii value. For example, '1' would give 49.
You must not use the strcpy
function and you must convert your characters.
So assuming you have to use a dynamic array and assuming the string given by the user contains only digits, you should write :
for (int i = 0; i < length; i) {
array[i] = (int) (integral[i] - '0');
}
Then you can compute your sum
CodePudding user response:
As commented by others, you do not have to dynamically allocate an array of integers just to calculate the sum of them (unless required by your instructor). Would you please try:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LEN 10
int main(void) {
int sum = 0;
char integral[LEN]; // char array to store at most LEN-1 letters
char *p = integral; // a pointer within "integral"
char *err; // remaining string after strtol
printf("Enter a string that include number: ");
fgets(integral, LEN, stdin);
while (1) {
sum = (int)strtol(p, &err, 10);
for (p = err; *p && !isdigit(*p); p ); // increment pointer to next digit
if (! *p) break; // end of string
}
printf("The sum is : %d\n",sum); // print the result
return 0;
}
If you are forced to use an array of integers, here is an alternative with an array:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LEN 10
int main(void) {
int sum = 0;
char integral[LEN]; // char array to store at most LEN-1 letters
char *p = integral; // a pointer within "integral"
char *err; // remaining string after strtol
int *ary = NULL; // array of integers
int i;
int n = 0; // count of integers
printf("Enter a string that include number: ");
fgets(integral, LEN, stdin);
for (; *p && !isdigit(*p); p ); // increment pointer to next digit
while (1) {
ary = realloc(ary, sizeof(int) * (n 1));
ary[n ] = (int)strtol(p, &err, 10);
for (p = err; *p && !isdigit(*p); p ); // increment pointer to next digit
if (! *p) break; // end of string
}
for (i = 0; i < n; i ) {
sum = ary[i];
}
printf("The sum is : %d\n",sum); // print the result
return 0;
}