Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't use sscanf() in C for char array

I'm trying to get a very large number (more than unsigned long long int). So I get it as a string and then convert it, digit by digit, into integer and use it.

#include <stdio.h>
#include <string.h>

int main() 
{ 
    char m[100];
    int x;
    scanf("%s",m);
    for(int i=0; i<strlen(m); i++){
        sscanf(m[i],"%d",&x);
        printf("%d",x);
    }

    return 0; 
}

However, during compiling it shows:

warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast

and

note: expected ‘const char * restrict’ but argument is of type ‘char’

And, when I run program it will give me the Segmentation fault (core dumped) error.

I also tried more simple code to find the problem:

#include <stdio.h>
#include <string.h>

int main() 
{ 
    char m[5];
    char n;
    int x;
    scanf("%s",m);
    n = m[1];
    sscanf(n,"%d",&x);  
    return 0; 
}

but nothing changed.

like image 480
Amir reza Riahi Avatar asked Apr 05 '20 22:04

Amir reza Riahi


People also ask

What happens if sscanf fails?

sscanf() Return value If successful, the sscanf() function returns the number of receiving arguments successfully assigned. If a matching failure occurs before the first receiving argument was assigned, returns zero.

Why do we use & in scanf for an array but not for a character array?

Because the array is already passed as an address, whereas the integer variable is not (and thus explicitly needs its address passed via the & address-of operator).

What is sscanf used for in C?

In C, sscanf() is used to read formatted data. It works much like scanf() but the data will be read from a string instead of the console.

What library is sscanf in C?

Description. The C library function int sscanf(const char *str, const char *format, ...) reads formatted input from a string.

What is int sscanf in C programming?

int sscanf(const char *str, const char *format, ...) str − This is the C string that the function processes as its source to retrieve the data. format − This is the C string that contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers

How does sscanf read data from a string?

It reads data from a character string. sscanf (string,formatspecifier,&var1,&var2,……..) String refers to the char string to read from. Format string refers to char string containing certain required formatting information. Var1,var2 etc., represent the individual input data items.

What is the difference between scanf () and sscanf ()?

The rest of the arguments of sscanf () is same as that of scanf (). It returns the number of items read from the string and -1 if an error is encountered. The following program demonstrates how sscanf () works: In line 6, we have declared and initialized a variable str of type pointer to char.

What are the arguments of sscanf ()?

The rest of the arguments of sscanf () is same as that of scanf (). It returns the number of items read from the string and -1 if an error is encountered.


Video Answer


1 Answers

scanf doesn't apply to characters. Once you have the characters just convert digits to integers by subtracting '0' as a char:

for(int i=0; i<strlen(m); i++){
    x = m[i] - '0';   // line to change
    printf("%d",x);
}

Besides, just to make sure that the buffer won't overflow, 100 bytes is good, but you may want to use an according limit in scanf and check the return code:

if (scanf("%99s",m) == 1) {
like image 183
Jean-François Fabre Avatar answered Sep 28 '22 08:09

Jean-François Fabre