Thread: struct array passing issue on linux

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    3

    struct array passing issue on linux

    Hello All,

    I am migrating C program from Solaris to Linux and it's not working on Linux. I don't understand why and I googled but found no results. I have array of STRUCT and when passing to subroutine by value or by reference the object is null. Here is my code:
    Code:
    typedef    struct {
       char    x[14], y[14], z[14];   
    } COORDINATES;
    
    typedef    struct {
       char    state[3];
       char    county[4];
       COORDINATES coord[MAX_COORD];
    } ARP;
    
    ARP site[MAX_SITE];
    
    //call subroutine
    compare_positions(site[i].coord[j]);
    Before passing to subroutine the values are good. But lost (null) when passed to subroutine. However, if I declared as single array then the value is passed fine. How do I fix this for Linux? I running gcc.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    In the code you posted, you're passing a COORDINATES object by value to the compare_positions function, so it cannot possibly be null.

    Post a small but complete (compilable) program that exhibits the problem.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    3
    That's the frustration part. I don't understand the difference between Solaris vs. Linux or cc vs gcc. Everything works fine on Solaris. I will try to create a small program and test it.

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by btran007
    That's the frustration part. I don't understand the difference between Solaris vs. Linux or cc vs gcc. Everything works fine on Solaris.
    The differences should not matter here. That they matter means that either one of the compilers or your code has a bug, and I am more inclined to believe that it is your code that has a bug that results in undefined behaviour: one of the symptoms of undefined behaviour is that the code could work as expected when compiled on one compiler/for one platform, yet fail to work as expected when compiled on another compiler/for another platform.

    Quote Originally Posted by btran007
    I will try to create a small program and test it.
    Yes, you should do this. After all, in your code snippet from post #1, you did not even provide a declaration of compare_positions that is a prototype. It is difficult to reason about your program with so little to work with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    3
    Here is the prototype. Nothing special.

    void compare_positions(COORDINATES coord);

    After creating a small program I discovered the error due to previous attempts to resolve this issue. In conclusion my program seems to be working but I had to remove array since it is not needed.

    Thank you for all your comments and suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a struct into array.
    By RyanC in forum C Programming
    Replies: 37
    Last Post: 06-08-2015, 09:47 AM
  2. Passing Struct through Array
    By emverhov in forum C Programming
    Replies: 1
    Last Post: 04-02-2013, 10:18 AM
  3. Replies: 8
    Last Post: 05-10-2012, 12:07 PM
  4. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM
  5. Array of Struct issue
    By B1acksun in forum C Programming
    Replies: 9
    Last Post: 11-30-2005, 03:06 PM