i quite new c programming (started learning yesterday, actually!) receiving error messages regarding subscript operator in c, seems cyclically requiring different input each error message.
my code follows:
int i; file *f = fopen("verletdata.txt","w"); for(i=0; <= n; i++) { fprintf(f,"%i\t%3.4f\n", n[i], x[i]); } fclose(f);
for receive following error:
subscripted value neither array nor pointer nor vector. (line 74, c/c++ problem)
now, following advice cameron skinner on q&a, error: subscripted value neither array nor pointer, changed int
*int
, follows:
int* i; file *f = fopen("verletdata.txt","w"); for(i=0; <= n; i++) { fprintf(f,"%i\t%3.4f\n", n[i], x[i]); } fclose(f);
for receive following error requiring i
integer:
array subscript not integer. (line 74, c/c++ problem)
line 74 in both cases fprintf(f,"%i\t%3.4f\n", n[i], x[i]);
.
how can fix problem in code, , avoid happening again in future, subscript operator require me in case? documentation little hazy in aspect beginner.
as noted in comment, need see definition of n
, x
, because 1 of them isn't array or pointer, seem. not int i;
cause of trouble.
you're right! had accidentally defined
n
, integer, array in code snippet above.
it more question of trying treat n
array when integer, account trouble.
note when changed i
int
int *
, made other expression invalid: because x
pointer already, x[i]
involved 2 pointers, , while fixed n[i]
expression (unorthdoxly — see with c arrays, why case a[5] == 5[a]
?), broke correct x[i]
.
Comments
Post a Comment