# Trouble printing x and y values of struct points in an array?

• 10-02-2012
Trouble printing x and y values of struct points in an array?
So I've got a program where I have to save a list of coordinate points into an array. I have to print the x and y coordinates of a specific point from the array but when I use %d it tells me it expects a type int.
The function index just finds the lower leftmost point in the array and returns its place in the array, and swap just swaps two points. Any advice here would be immensely appreciated!

Code:

```#include <stdio.h> #include <stdlib.h> typedef struct point{ int x ; int y ;} POINT ; int index( POINT *A ) ; void swap( POINT x, POINT y ) ; int main( int argc, char *argv[] ) {   int n, i, j ;   POINT *A[n-1] ;   FILE *fin ;   fin = fopen( argv[1], "r" ) ;   fscanf( fin, " %d ", &n) ;   A[n-1] = (POINT *) malloc( n * sizeof(POINT) ) ;   for ( i = 0; i < n; i++ ){ fscanf( fin, "%d %d", &A[i]->x, &A[i]->y ) ;}     j = index( A[n-1] ) ;   printf( "\n A[%d], (%d, %d)\n" j, A[j]->x, A[j]->y ) ;   swap( *A[j], *A[0] ) ;   for ( i = n; i >= 0; i-- ) {   printf( "\n(%d, %d)\n" A[i]->x, A[i]->y ) ;    }   fclose( fin ) ;   return 0 ; }```
• 10-02-2012
• 10-02-2012
oogabooga
POINT *A[n-1];
n is uninitialied at this point.
You actually just want
POINT *A;

And this line
A[n-1] = (POINT *) malloc( n * sizeof(POINT) ) ;
should be
A = malloc(n * sizeof(*A));

And please don't put a space before the semicolon at the end of the line. That is a rare and pointless practice.
• 10-03-2012
Quote:

Originally Posted by oogabooga
POINT *A[n-1];
n is uninitialied at this point.
You actually just want
POINT *A;

And this line
A[n-1] = (POINT *) malloc( n * sizeof(POINT) ) ;
should be
A = malloc(n * sizeof(*A));

And please don't put a space before the semicolon at the end of the line. That is a rare and pointless practice.

I've made these changes but still won't accept A[j].x or A[j]->x as integer types when I attempt to print them with %d. Any other advice?

I wouldn't space out semi-colons, but my Professor requires it.
• 10-03-2012
grumpy
Quote:

I've made these changes but still won't accept A[j].x or A[j]->x as integer types when I attempt to print them with %d. Any other advice?

Check that you've followed ALL of oggabooga's advice, not just a part of it. If you followed his advice completely, A[j].x will be an int. If you followed only parts of his advice, your code will almost certainly not compile.

Not related to your problem, but the arguments of your swap() function need to be pointers, not just of type POINT, if you want effects inside the function to be visible outside the function. Look up the meaning of "pass by value" to understand why.
• 10-03-2012
Quote:

Originally Posted by grumpy
Check that you've followed ALL of oggabooga's advice, not just a part of it. If you followed his advice completely, A[j].x will be an int. If you followed only parts of his advice, your code will almost certainly not compile.

Not related to your problem, but the arguments of your swap() function need to be pointers, not just of type POINT, if you want effects inside the function to be visible outside the function. Look up the meaning of "pass by value" to understand why.

I've switch everything and I'm being told A[i].x is of type * char? This is everything after I've edited it.
And thanks for the tip about swap, I would've completely missed that.

Code:

```#include <stdio.h>#include <stdlib.h> typedef struct point{ int x ; int y ;} POINT ; int index( POINT *A ) ; void swap( POINT *x, POINT *y ) ; int main( int argc, char *argv[] ) {   int n, i, j ;   POINT *A ;   FILE *fin ;   fin = fopen( argv[1], "r" ) ;   fscanf( fin, "%d", &n) ;   A =  malloc( n * sizeof(*A) ) ;   for ( i = 0; i < n; i++ ){ fscanf( fin, "%d %d", &A[i].x, &A[i].y ) ;}     j = index( A ) ;   printf( "\n A[%d], (%d, %d)\n" j, A[j].x, A[j].y ) ;   swap( &A[j], &A[0] ) ;     for ( i = n; i >= 0; i-- ) {   printf( "\n(%d, %d)\n" A[i].x, A[i].y ) ;    }   fclose( fin ) ;   return 0 ; }```
• 10-03-2012
AndiPersti
Quote:

```printf( "\n A[%d], (%d, %d)\n" j, A[j].x, A[j].y ) ; ... printf( "\n(%d, %d)\n" A[i].x, A[i].y ) ;```