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

1. ## 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, "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 ) ;

for ( i = n; i >= 0; i-- ) {

printf( "\n(%d, %d)\n" A[i]->x, A[i]->y ) ;
}

fclose( fin ) ;

return 0 ;

}``` 2. You're using n before you have given it a value. You should be receiving a warning about this, from your compiler. 3. 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. 4. 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.
Thank you for your help. 5. Originally Posted by arcadedragon 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. 6. 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, "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 ) ;

for ( i = n; i >= 0; i-- ) {

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

fclose( fin ) ;

return 0 ;

}``` 7. Originally Posted by arcadedragon I've switch everything and I'm being told A[i].x is of type * char?
It's always easier to help you if you post the real error/warning messages (copy'n'paste them, don't rewrite them)

Code:
```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 ) ;```
You're missing a comma in both printf()-calls (right after the format string).

Bye, Andreas Popular pages Recent additions #### Tags for this Thread

array, print, struct pointer 