I don’t know how to work with scanf and get the input of it for the entry of the function readBigNum I want to make array until the user entered the Enter and also I want to write a function for assigning it into an array and return the size of the large number
I want readBigNum to exactly have the char *n but I can not relate it in my function
#include <stdio.h>
int readBigNum(char *n)
{
char msg[100],ch;
int i=0;
while((ch=getchar())!='\n')
{
if(ch!='0'||ch!='1'||ch!='2'||ch!='3'||ch!='4'||ch!='5'||ch!='6'||ch!='7'||ch!='8'||ch!='9')
return -1;
msg[i++]=ch;
}
msg[i]='\0';
i=0;
return i;
}
int main()
{
const char x;
const char n;
n=scanf("%d",x);
int h=readBigNum(&n);
printf(h);
}
If I understand your question correctly, you want to implement a function that will read numbers from stdin storing them in a buffer. If a non-number is encountered, you want to return -1. If a new-line is encountered, you want to return the number of characters that were read. If that’s correct, you’ll probably want your code to look something like the following:
The main differences from your implementation
mallocandfree. Even with this, you run the risk of a buffer overrun and will likely want to take additional precautions to prevent that.ito 0 before returning it. The original code could never return a value other than-1(on error) or0, which didn’t appear to be the intent.scanf. Given your description of what you wanted to accomplish, usingscanfdidn’t appear to be a good fit, however if you provide more information on why you were calling it might help to inform this answer.printfcall was incorrect, it has been updated to print the number of bytes returned, and an additionalprintfcall was added to print the updated buffer.