I am new to c.i read that array subscript operator and pointer arithmetic are interchangeable for access member variable.But in the following code i get error when i used the ->
operator for access the member variable i get error .i am bit confuse what is happening under the hood .what am i missing .
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x, y;
} Point;
int main ()
{
int numOf;
Point *myPoints = NULL;
FILE *myfile = fopen ("c:\\jobs.txt","r");
if (myfile == NULL)
perror ("Error opening file");
else
{
fscanf(myfile, "%d", &numOf);
myPoints = (Point *)malloc(sizeof(Point) * numOf);
while ( !feof (myfile) && numOf-- )
{
fscanf(myfile, "%d %d", &(myPoints+numOf)->x, &myPoints[numOf].y);// compile and works
//fscanf(myfile, "%d %d", &myPoints[numOf]->x, &myPoints[numOf].y);// dos not compile or &(myPoints[numOf])->x
}
printf("%d %d \n",myPoints->x,myPoints->y);//compile and works
}
fclose(myfile);
//Do stuff with array
free ((void *)myPoints);
getchar();//Press enter to close debugger etc.
return 0;
}
is the cases when i use subscript operator i can't use the ->
with it .
like &myPoints[numOf]->x