Array of struct pointers - Losing my mind

This is a discussion on Array of struct pointers - Losing my mind within the C Programming forums, part of the General Programming Boards category; Hey everyone, I've been staring at my code for over an hour and I can't seem to figure out what's ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Angry Array of struct pointers - Losing my mind

    Hey everyone,

    I've been staring at my code for over an hour and I can't seem to figure out what's wrong. I was hoping someone could help me before I lose my mind completely:

    I would like to have an array that holds pointers to structs.

    I'd like to create these structs and shove them in this array and later be able to search the array to find a certain struct based on its name.

    This is what my code looks like:

    my struct looks like this:

    Code:
    typedef struct {
       char *entryName;
       [other vars here]
    }regEntry;    
    
    
    // global array of struct pointers...
    regEntry *entryTable[100];
    int numEntries=0;
    
    int rpcRegisterFunction(char *name, [other params]) {
        regEntry *newRegEntry = malloc(sizeof(*newRegEntry));
        
        newRegEntry->entryName = name;
        [fill the rest of struct vars here]    
    
            // put my struct pointer into the array of pointers
            entryTable[numEntries] = newRegEntry;
        numEntries++;
    }
    
    
    
    int rpcServer(void){
       // receive a message (struct), call it recMsg
       // recMsg will contain a name
       regEntry *foundRegEntry;
       foundRegEntry = (regEntry *)getRegEntry(recMsg->rpcName);   
       return;
    }
    
    //Search method - will search array by name and return pointer to the struct having the requested name
    regEntry * getRegEntry (char *name)    {
       int iter;
       
       for (iter=0; iter<numEntries; iter++)    {
          if (strcmp(entryTable[iter]->entryName, name) == 0)   { 
               return entryTable[iter];
          }
       }    
    }
    I'm not sure if it's my C (which I don't really know that well) or if it's my logic... I'm not entirely sure that the malloc thing is doing what I want either.

    When I try to compile I get this:

    rpc_server.c:138: error: conflicting types for 'getRegEntry'
    rpc_server.c:118: error: previous implicit declaration of 'getRegEntry' was here


    line 138 is my function declaration
    regEntry * getRegEntry (char *name) {

    and line 118 is my assignment when I come back from the search:
    foundRegEntry = (regEntry *)getRegEntry(recMsg->rpcName);

    Please, please please help if you can. Like I said, if I don't resolve this soon, I'm gonna start throwing things!!!

    Thanks in advance,

    Tanya

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Where do you actually have a recMsg->rpcName?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Quote Originally Posted by Dave_Sinkula
    Where do you actually have a recMsg->rpcName?
    recMsg is a struct defined elsewhere that I'm including in this file. This struct has an attribute rpcName.

    I should also mention that this code compiles fine if I comment out the getRegEntry() call from rpcServer().

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by drucillica
    recMsg is a struct defined elsewhere that I'm including in this file. This struct has an attribute rpcName.
    Y'mind showing us a snippet of code that is compilable and demonstrates the problem, or is 20 questions your game?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Will the code compile if you declare the function before using it?
    Perhaps a simple:
    Code:
    regEntry *getRegEntry(char *name);
    Near the top of the file, so that the compiler knows the return type and arguments before it encounters it in the code? (I think the cast you use on line 118 is unneeded as well.)

    Edit: As for your malloc() issue... you could output the value of sizeof(*newRegEntry)... if it's the size of the structure you're good... if it's 4 bytes, then it's probably the size of a pointer, and not what you want. I usually use types, not variables in malloc() calls. That was just the way I learned it though, not sure which is preferred/better.
    Last edited by Cactus_Hugger; 11-12-2005 at 10:47 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Quote Originally Posted by Cactus_Hugger
    Will the code compile if you declare the function before using it?
    Perhaps a simple:
    Code:
    regEntry *getRegEntry(char *name);
    Near the top of the file, so that the compiler knows the return type and arguments before it encounters it in the code? (I think the cast you use on line 118 is unneeded as well.)
    I got it - your response made me think of the header file and turns out i screwed that up, which caused the compile errors.

    And you're right about the unneeded cast - that was out of desperation trying to fix "conflicting types for 'getRegEntry'" compile error.

    Thanks a million - you rock!

    Tanya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 02:26 PM

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