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

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    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 ;
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're using n before you have given it a value. You should be receiving a warning about this, from your compiler.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Quote Originally Posted by oogabooga View Post
    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. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by arcadedragon View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Quote Originally Posted by grumpy View Post
    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 ;
    
    
    }

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by arcadedragon View Post
    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 subscribe to a feed

Similar Threads

  1. Having trouble printing out values from a map of maps
    By Freddy92 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 08:45 AM
  2. C++ Plot Simple Points / Graph, X & Y array points
    By Khadafi in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2011, 03:47 AM
  3. Trouble Printing out Array
    By denZer in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 01:37 PM
  4. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  5. stuck printing values from array
    By Guti14 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2003, 09:56 PM

Tags for this Thread