0

I keep getting compiler errors telling me its not a pointer as shown below.

enter image description here

I don't know what I am doing wrong here. I got this solution on stack overflow on another post.

 typedef struct Data{
   int x;
   int y;
}Data;

int main (void){
Data * arrData = malloc (sizeof * arrData *6);
for (int i =0; i<6;i++){
    arrData[i] = NULL;
}
for (int i =0; i < 6; i++){
    printf("This is an iteration.\n");
    arrData[i] = malloc (sizeof * arrData[i]);
    int tempx;
    int tempy;

    printf("Add x and y\n");
    scanf ("%d %d",&tempx,&tempy);
    arrData[i] -> x = tempx;
    arrData[i] -> y = tempy; 
}
for (int i =0; i<6; i++){
    printf("x: %d y: %d\n",arrData[i]->x,arrData[i]->y);
}
free (arrData);
return 0;
}
2
  • 1
    arrData[i] isn't pointer. Commented Mar 2, 2016 at 1:48
  • Pretty much exactly what it said. arrData[i] isn't a pointer, so you can't use * or -> on it or assign a pointer to it. Commented Mar 2, 2016 at 2:07

1 Answer 1

2
 typedef struct Data{
    int x;
    int y;
}Data;

int main(void){
//By doing this, you are allocating a 6 Data large memory space
Data * arrData = (Data*)malloc(sizeof(Data)* 6);

//you don't need this
//for (int i = 0; i<6; i++){
//  arrData[i] = NULL;
//}

for (int i = 0; i < 6; i++){
    printf("This is an iteration.\n");

    //you don't need this
    //arrData[i] = malloc(sizeof * arrData[i]);
    int tempx;
    int tempy;

    printf("Add x and y\n");
    scanf("%d %d", &tempx, &tempy);

    //If pointer to struct, use ->, otherwise, use .
    arrData[i].x = tempx;
    arrData[i].y = tempy;
}
for (int i = 0; i<6; i++){
    printf("x: %d y: %d\n", arrData[i].x, arrData[i].y);
}
free(arrData);
return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.