这周刚学了C。我的任务是从用户那里获取一个大整数输入,将其存储到一个结构整数中,然后创建一个函数来将适当的结构整数打印到标准输出中。该程序是这样工作的,但是一旦它给出输出,它就会停止响应。我在编译器中没有得到任何直接错误,也无法找出问题所在。任何其他改进编程风格的建议/技巧也将不胜感激:)
// Header Files Go Here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function Declarations Go Here
struct integer * convert_integer(char * stringInt);
void printer(struct integer * p);
struct integer {
int * arr;
int length;
};
// Main Program
int main() {
char * x;
x = (char *) malloc(sizeof(char) * 10000);
printf("Enter a small string\n");
scanf("%s",x);
int j = 0;
struct integer * book1;
book1 = convert_integer(x);
printer(book1);
return 0;
}
// Function Definitions Go Here
struct integer * convert_integer(char * stringInt) {
struct integer * x = malloc(sizeof(int) * 100);
int j = 0;
while (stringInt[j] != '\0') {
if (stringInt[j] < 48 || stringInt[j] >= 57) {
printf("Invalid input. Enter a number ");
return;
}
x->arr[j] = stringInt[j] - 48;
j++;
}
x->length = j;
printf("\n the length is %d\n", x->length);
return x;
}
void printer(struct integer * p) {
int j = 0;
while (j < p->length) {
printf("%d", p->arr[j]);
j++;
}
}