无法在C中将sscanf()用于char数组

问题描述 投票:1回答:2

我正在尝试获取一个非常大的数字(大于unsigned long long int)。所以我将其作为字符串获取,然后将其逐位转换为整数并使用它。

#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; 
}

在编译过程中显示warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a castnote: expected ‘const char * restrict’ but argument is of type ‘char’

并且当我运行程序时,它将给我Segmentation fault (core dumped)错误。我还尝试了更简单的代码来查找问题。

#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; 
}

但没有任何改变。

c arrays string char scanf
2个回答
0
投票

scanf不适用于字符

。一旦有了字符,只需将'0'减为一个字符即可将数字转换为整数:

0
投票

使用sscanf将字符串的单个数字

© www.soinside.com 2019 - 2024. All rights reserved.