arrays - Scanf error in C -
so have following c code:
#include<stdio.h> #include<stdlib.h> int main() { int l,r; scanf("%d",&l); scanf("%d",&r); long long int *a=malloc(l*sizeof(long long int*)); long long int *a=malloc(l*sizeof(long long int*)); int i; for(i=0;i<l;i++) scanf("%lld %lld",&a[i],&a[i]); for(i=0;i<l;i++) { printf("a(%d)==%lld , a(%d)==%lld\n",i,a[i],i,a[i]); } return 0; }
which unfortunately starter of problem, , print loop on end want determine if input values assigned correctly arrays , ( r value has 2 arrays b , b doesn't matter).
when compile , run program these inputs: 3 5 10 1 3 2 10 1
i on output: a(0)==10 , a(0)==10 a(1)==3 , a(1)==2 a(2)==10 , a(2)==1
notice puts correctly, value a(0)=10 instead of calculated 1 , scanf reads second time value 10, skips value 1 , proceeds reading rest of values correctly. i'm stuck. have idea why incident occurs?
there mistake in following line:
long long int *a=malloc(l*sizeof(long long int*));
you need use sizeof(long long int) , not sizeof(long long int*)
hope helps mitigate issues.
Comments
Post a Comment