c - How do I skip a part of standard input with scanf? -
i'm writing c program, , standard input looks this.
2 1 3 4 6 9 1 3 6 0 3 5 1 2 6 1 my c program stores first number in variable called rowtoconsider. read rowtoconsiderth row (0-indexed) of numbers array nums.
#include<stdio.h> int main() { int rowtoconsider; int nums[3]; scanf("%d", &rowtoconsider); (int = 0; < rowtoconsider; i++) { // attempt read row nothing. basically, skip row. scanf("%d %d %d"); } scanf("%d %d %d", nums, nums + 1, nums + 2); } however, scanf within loop triggering segmentation fault.
how skip rows before reading in array of 3 ints?
you getting segfault because first call scanf expects 3 more arguments.
you can specify optional argument *, e.g. scanf("%*s"); read string , discard it.
from scanf man page:
an optional '*' assignment-suppression character:
scanf()reads input directed conversion specification, discards input. no corresponding pointer argument required, , specification not included in count of successful assignments returnedscanf().
Comments
Post a Comment