Problem with passing an array of structures to a function

This is a discussion on Problem with passing an array of structures to a function within the C Programming forums, part of the General Programming Boards category; Having some trouble with this. I know, basic stuff but i'm a noob and i really cant figure out what ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Problem with passing an array of structures to a function

    Having some trouble with this. I know, basic stuff but i'm a noob and i really cant figure out what else to do. First off, here's the code:

    Code:
    void data_entry(struct rep_info *rep[], int *rep_number)
    {
         int counter;             //counter for the loops
         char instring[32];       //just temp storage before a integer conversion
         
         printf("Enter the Representative's name:\n");
         gets(rep[rep_number]->rep_name);
    The problem I'm having is the compiler reports this during compile:
    Code:
    49 D:\Code\Rep Average\0.04a.cpp invalid types `rep_info**[int*]' for array subscript
    I'm using Dev C++ by bloodshed software btw, and only coding in ANSI C, dunno if thats important or not. the function is supposed to be passed an array and a number. The number is for the subscript indicating which Rep's info that is going to be entered. I'm trying to ask the user how many rep's information they'll be entering, and then creating an array based on that input, and passing the array onto the function data_entry. Data entry is called in a for loop until say 5 reps information is filled in. Then the loops breaks out and the program moves on. Any help with this would be much appreciated. I'm sorry if i didnt supply enough information, but i think that covers most of it.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can't pass arrays as aparamaters in C. You can pass a pointer to the first element, like you would with a string, or you can put the array inside another structure and pass a pointer to that.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    yeah, i prolly shoulda known that. but anyway, thx for the help. i assumed i could write it like this:
    Code:
    data_entry(rep, &counter);
    i figured since rep was an array it would pass the address on like a pointer and i could still get a hold of the elements of the array. atleast that's what the CBT i'm doing led me to believe. Thx for the help tho

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    To add onto what sean said:

    void data_entry(struct rep_info *rep[], int *rep_number)

    The error you are getting is referring to the fact that you are passing rep_number by reference and yet trying to access it directly here:

    gets(rep[rep_number]->rep_name);

    You need to dereference rep_number to obtain the value that the pointer points to, aka

    gets(rep[*rep_number]->rep_name);

    BTW gets is bad, look at this FAQ
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    i actually did try that before i came here, then i got a linker error that said this:
    Code:
      [Linker error] undefined reference to `data_entry(rep_info*, int*)' 
    collect2 D:\Code\Rep Average\collect2 ld returned 1 exit status
    when i got that i figured i was on the wrong track so i removed the indirection operators. that and i have no idea what any of that means. i haven't covered Linkers? or whatever that error is referring to.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, quit compiling as C++:
    49 D:\Code\Rep Average\0.04a.cpp invalid types `rep_info**[int*]' for array subscript
    Second, if you get an "undefiend reference to" error, it means you're trying to call a function you don't have any place usually. Check your spelling. Try a very small sample with the function in question:
    Code:
    #include <stdio.h>
    
    struct rep_info
    {
        int foo;
    };
    
    void data_entry( struct rep_info *r, int *size )
    {
        r = NULL; /* just so we don't get 'variable not used' warnings... */
        size = NULL; /* just so we don't get 'variable not used' warnings... */
        printf( "Wheeee!\n" );
    }
    
    int main( void )
    {
        struct rep_info r;
        int size = 0;
    
        r.foo = 0;
    
        data_entry( &r, &size );
    
        return 0;
    }
    Something simple, like so.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    thanks quzah, i was unaware the compiler did things differently based on the file extension. it actually got rid of the "undefined reference to" error that i was getting. i had a load more errors afterwards, but i worked with it and got it compiled and running fine. thanks for all the help tho guys, you have no idea how long i was searching on the net and in my code for the solution to my prob. now to fix that buffer overflow issue gets() has.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21