问一个数组的问题
例如,我想用一个数组p来装输入的数字123(输入的时候就是输入123 ,不是输入1 2 3 这种有空格的)其中p[0]=1 p[1]=2 p[3]=3
这种效果可以实现吗?该怎么写呢
#include <stdio.h> #include <ctype.h> #define N 10 int main(void) { int ch, a[N] = {0}, *p = a; while((ch = getchar()) != '\n' && isdigit(ch) && p <= &a[N - 1]) printf("%d ", *p++ = ch - 48); puts(""); return 0; }
#include <stdio.h> #include <stdlib.h> #define N 5 int main(void) { int a[N] = {0}, *p = a; char s[3] = {0}, *end; while(scanf("%2[0-9]", s) == 1 && p <= &a[N - 1]) printf("%d ", *p++ = strtol(s, &end, 10)); puts(""); return 0; }
#include <stdio.h> int main(void) { char str[11] = {0}, *p = str; int i = 0, a[5] = {0}; while(scanf("%10[0-9]", str) != 1) while(getchar() != '\n'); while(i < 5) { sscanf(p, "%2d", a + i++); p += 2; } for(i = 0; i < 5; i++) printf("%d ", a[i]); puts(""); return 0; }