Need help debugging C code
Just learnt C this week. My task is to take a large big integer input from
the user, store it into a struct Integer, and make a function to print out
the appropriate struct integer to the standard output. The program works
as such, but as soon as it gives the output, it stops responding. I dont
get any direct error in the compiler and cannot figure out what is wrong.
Any other advice/tips to improve upon programming style would also be
really appreciated :)
//Header Files Go Here
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Function Declarations Go Here
struct integer* convert_integer(char* stringInt);
void print(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++;
}
}
No comments:
Post a Comment