简体   繁体   English

指针和字符

[英]Pointer and Character

My code is suposed to get the separated numbers, the thing is I have already seen numerous tutorials and I do not understand why it runs when I set: 我的代码是用来获取分开的数字,事情是我已经看过很多教程,我不明白为什么它在我设置时运行:

p = "123 23 32"
out>>
123
23
32
0
0

But when I set it equals to character array It only finds the first number, event though I "walk" in to the p pointer: 但是当我设置它等于字符数组它只找到第一个数字,事件虽然我“走”到p指针:

#include<stdio.h>
int main()
{
    int N, i=0, NUMERO = 1, FINAL = 0;
    char ORDEM[100000], *p = ORDEM;

    scanf("%s", ORDEM);
    p = ORDEM;

    for(i = 0; i<5; i = i + 1)
    {
        printf("%d\n", atoi(p));
        while (*p != ' ') p++;
        if (*p == ' ') p++;
        if (*p == '\0') break;
    }
return 0;
}
scanf("%s", ORDEM);

scanf() delimits by space characters. scanf()按空格字符分隔。 Thus ORDEM contains only "123" in your test case. 因此, ORDEM在您的测试用例中仅包含"123"

To read the whole line, you should call fgets() . 要读取整行,您应该调用fgets()

fgets(ORDEM, 100000, stdin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM