Thread: Passing array of struct as a function parameter

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Passing array of struct as a function parameter

    Hello,

    I have a structure and an multidimensional array of that structure. How can I access (read and write) that array's element's fields (without defining it as a global variable) inside a function when the array is declared in main() ?

    Here's how I tried it (but failed):

    Code:
    #include <stdio.h>
    
    typedef struct { 
        double xy;
        char *message; 
    } twin;
    
    void print(twin *table, char *m, int i)
    {
        (*table[i][1]).message != m ?   <----- error: subscripted value is neither array nor pointer
            printf("\nX = &#37;f Y = %s", *table[i][0].xy, *table[i][1].message): <----- same here (twice)
            printf("\nX = %f Y = %f", *table[i][0].xy, *table[i][1].xy);   <----- same here (twice)
        return;    
    }
    
    int main()
    {    
        int i, n;
        double x, a, h;     
        char v[] = "empty";
        ...
        twin table[n-1][1];
        ...
        print(&table, &v, 1); 
      
      return 0;
    }
    Best wishes,
    Desmond5
    Last edited by desmond5; 12-04-2007 at 09:49 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why are you dereferencing table in print()?

    Hint: Try not doing that.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are passing a 2D array of structs as a simple pointer [although the second dimension is only "1" wide, looking at the code, you probably actually want to make it 2].

    Here's one way to make what you currently have work, however:
    Code:
    void print(twin (*table)[2], char *m, int i)
    {
        table[i][0].message != m ?   
            printf("\nX = %f Y = %s", table[i][0].xy, table[i][1].message): 
            printf("\nX = %f Y = %f", table[i][0].xy, table[i][1].xy);  
        return;    
    }
    I'm not entirely sure that a terniary operator is the right solution here, and you probably want to use strcmp() rather than != when comparing text strings. [Unless you rely on the address of strings with equal contents to also always be the SAME string - that is, having the same address].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    Thanks, I tried to do what you recommended and came up with this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct { 
        double xy;
        char *message; 
    } twin;
    
    void print(twin (*table)[2], char *m, int i)
    {
        strcmp(table[i][1].message, m) ?
            printf("\nX = &#37;f Y = %s", table[i][0].xy, table[i][1].message): 
            printf("\nX = %f Y = %f", table[i][0].xy, table[i][1].xy);  
        return;    
    }
    
    int main()
    {    
        char v[] = "empty";
        
        int i = 0, n = 10;       
        double somevalue = 2.0; 
        twin table[n][2];
           
        for (i = 0; i < n; i++)
        {
            // input some data for the example to actually work
            table[i][0].xy = somevalue;   
            table[i][1].xy = somevalue * 2;         
            print(&table, &v, i);  //<-- [Warning] passing arg 1 of `print' from incompatible pointer type      
                                   //    [Warning] passing arg 2 of `print' from incompatible pointer type 
        }
      
      return 0;
    }
    But it crashes right after I run it. What could be wrong ? If I comment out the first two lines of the print function it seems to work, so there's something I'm not seeing there..

    I'm no sure I understand "twin (*table)[2]". I seems to declare that there are only 2 elements in the array not n*2 as I intended to ?

    I use the struct to have a element in the array (table) that can be either a double or string. I store calculations in the array. On some terms, if the function fails to calculate, I instead want to store the error message why the calculation failed in the array.

    Best wishes, Desmond5
    Last edited by desmond5; 12-04-2007 at 11:26 AM.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    When you create an element of your struct, are you sure the member message will always be NULL and not pointing to some random location?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    print(table, &v, i);
    Remove & before table. Arrays are automatically passed as pointers w/o the need of &.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM