Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with my simple code? 2

Status
Not open for further replies.

Rmcta

Technical User
Nov 1, 2002
478
US
I am getting this error2:
1) error C2108: subscript is not of integral type
2) error C2102: '&' requires l-value

I tried replacing float with int and all works fine. Why do I get the problem with float?

#include <stdio.h>
#define N 15

main()
{
FILE *fdi;
fdi = fopen("input.txt", "r");

float MyArray[N][N];
float x, y;

for (x=0; x<N; x++)
for (y=0; y<N; y++)
fscanf(fdi,"%f", &MyArray[x][y]);


printf("%f", MyArray[3][2]);



return 0;
}
 
Array subscripts x & y need to be ints, not floats.

int x,y;

Other than that, it should work fine (providing of course your input is correct in input.txt).

Good luck.
 
Some addition:
integral types: char (possibly signed/unsigned), int (+ short/long and signed/unsigned), enum - that's all. Integral types present exact values. In C operator
Code:
pointer[integral]
is abbreviation for
Code:
*(pointer+integral)
Type float (and double) represents an approximation of a number. You can't add float/double values with pointers (by the C language definition). What means one and half's element?..
 
You are both 100% correct. Thank you very much for your guidance. [2thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top