简体   繁体   中英

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:

#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. Thus ORDEM contains only "123" in your test case.

To read the whole line, you should call fgets() .

fgets(ORDEM, 100000, stdin);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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