Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

losing my mind: array of struct pointers

Status
Not open for further replies.

drucillica

Technical User
Nov 12, 2005
2
CA
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:


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
 
How: You need a forward prototype before you use it. At the top of the program just add
Code:
regEntry * getRegEntry (char *name);
Why: C compilers are generally one pass. All the declarations must come before the usage. Since you haven't provided a prototype for getRegEntry, it infers a prototype of
Code:
int getRegEntry(char*);
If it is anything other than this, you will get an error. An alternative strategy is to move the definition of getRegEntry before the routine that uses it.
 
Thanks! - you rock!!
Turns out that I screwed up my header file.. and by screwed up, I mean I didn't add these methods to it.

... I just wish the compiler's errors weren't so cryptic.
 
You should have been around when compilers just gave errors like "F$D004"... it was a real joy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top