![]() |
| | #1 |
| Registered User Join Date: Oct 2009 Location: Edmonton, Canada
Posts: 5
| Passing pointers to functions in seperate source files I'm brand new here and this is my first post... I think my question is straight forward - I'm fairly new to C but I have to this point written many small useless programs that use structs, pointers and functions. I have no problem passing the address of a struct array to a function and I have no issues getting the functions to perform their specific tasks to the struct arrays - as long as all my code is in the same source file. This is my problem. Early in my introduction to C I got in the habit of keeping the source file containing main() small, and having separate source files containing like functions. I was having little to no difficulty maintaining this until I started to pass pointers around. Now I can't seem to compile without error unless all my code is in the same source file. Any ideas on what I might be doing wrong? Is this in fact bad practice to be using separate source files? Here's an example.. this program compiles and runs as I intended it to. But if I put the functions newDriver() and displayDriver in a seperate source file I cannot get the program running to save my life. Code: #include <stdio.h>
#include <stdlib.h>
struct person
{
char lastName[20];
char firstName[20];
char age[4];
char position[20];
};
typedef struct person person;
int counter = 0;
int displayCounter;
person *empPtr;
newDriver(person *empPtr)
{
fputs("\n*New Driver Entry\n", stdout);
fputs("Enter driver last Name: ", stdout);
fgets(empPtr[counter].lastName, 20, stdin);
fflush(stdin);
empPtr[counter].lastName[strlen(empPtr[counter].lastName) - 1] = '\0';
fputs("Enter driver first Name: ", stdout);
fgets(empPtr[counter].firstName, 20, stdin);
fflush(stdin);
empPtr[counter].firstName[strlen(empPtr[counter].firstName) - 1] = '\0';
fputs("Enter driver age: ", stdout);
fgets(empPtr[counter].age, 4, stdin);
fflush(stdin);
empPtr[counter].age[strlen(empPtr[counter].age) - 1] = '\0';
fputs("Enter driver position: ", stdout);
fgets(empPtr[counter].position, 20, stdin);
fflush(stdin);
empPtr[counter].position[strlen(empPtr[counter].position) - 1] = '\0';
}
displayDriver(person *empPtr)
{
fputs("\n*Display Driver Entries\n", stdout);
for(displayCounter =0; displayCounter < counter; displayCounter++)
{
printf("%s, %s\t\t\t%s\t%s\n", empPtr[displayCounter].lastName,
empPtr[displayCounter].firstName, empPtr[displayCounter].age,
empPtr[displayCounter].position);
}
}
int main(void)
{
char userSay[10];
empPtr = malloc(20 * sizeof(person));
if(empPtr == NULL)
{
printf("There was a memory allocation problem.\n");
exit(1);
}
fputs("*** Taxi Employee Database ***\n\n", stdout);
do
{
fputs("--->", stdout);
fgets(userSay, 10, stdin);
fflush(stdin);
userSay[strlen(userSay) - 1] = '\0';
if(!strcmp(userSay, "quit"))
{
fputs("\nGoodbye!\n", stdout);
}
else if(!strcmp(userSay, "new"))
{
newDriver(empPtr);
counter++;
}
else if(!strcmp(userSay, "display"))
{
displayDriver(empPtr);
}
else
{
fputs("Unknown Entry\n", stdout);
}
}while(strcmp(userSay, "quit"));
free(empPtr);
exit(0);
}
|
| derekeverett is offline | |
| | #2 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| You should post the code that doesn't work instead of the code that works. How exactly are the functions split up? Which header files are the prototypes defined in? Are you including those header files? Is your compiler correctly linking the compiled object files? We need to see the code, and the error messages.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #3 |
| Registered User Join Date: Oct 2009 Location: Edmonton, Canada
Posts: 5
| My question was general - not specific to this example... Generally I would simply have the functions in another source file and would have the same header files seen here as well as declarations to all the external variables. This works in other programs I've coded but not with pointers. So I figure I am missing something I just can't find what. Prototypes - although I understand they are a good idea to catch compilation issues are not actually "required" for anything correct? |
| derekeverett is offline | |
| | #4 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| There is nothing at all that would prevent you from doing what you described. That's why we need to see an example which doesn't work, so we can point out what you did wrong. Quote:
__________________ bit∙hub [bit-huhb] n. A source and destination for information. | |
| bithub is offline | |
| | #5 |
| ... Join Date: Jan 2003
Posts: 1,190
| bithub is right - show us an example of what does *not* work for you. Splitting code up into various files is quite common, and should give no issues, provided you are doing things right. As for prototypes, if you are using a modern compiler, they are definitely necessary. |
| kermit is offline | |
| | #6 |
| Registered User Join Date: Oct 2009 Location: Edmonton, Canada
Posts: 5
| ok thanks - I was unaware prototypes were required. And unsure exactly how to declare them in this example using type person.... I've tried 50 different ways but here's an example of 1... Code: #include <stdio.h>
#include <stdlib.h>
extern int counter;
extern int displayCounter;
extern struct person
extern struct person *empPtr;
newDriver(person *empPtr)
{
fputs("\n*New Driver Entry\n", stdout);
fputs("Enter driver last Name: ", stdout);
fgets(empPtr[counter].lastName, 20, stdin);
fflush(stdin);
empPtr[counter].lastName[strlen(empPtr[counter].lastName) - 1] = '\0';
fputs("Enter driver first Name: ", stdout);
fgets(empPtr[counter].firstName, 20, stdin);
fflush(stdin);
empPtr[counter].firstName[strlen(empPtr[counter].firstName) - 1] = '\0';
fputs("Enter driver age: ", stdout);
fgets(empPtr[counter].age, 4, stdin);
fflush(stdin);
empPtr[counter].age[strlen(empPtr[counter].age) - 1] = '\0';
fputs("Enter driver position: ", stdout);
fgets(empPtr[counter].position, 20, stdin);
fflush(stdin);
empPtr[counter].position[strlen(empPtr[counter].position) - 1] = '\0';
}
displayDriver(person *empPtr)
{
fputs("\n*Display Driver Entries\n", stdout);
for(displayCounter =0; displayCounter < counter; displayCounter++)
{
printf("%s, %s\t\t\t%s\t%s\n", empPtr[displayCounter].lastName,
empPtr[displayCounter].firstName, empPtr[displayCounter].age,
empPtr[displayCounter].position);
}
}
|
| derekeverett is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| So it never hurts to do a valid prototype, namely one that includes a return type. Are you intending to hide your global empPtr variable with the one passed in? You still need "struct" in a prototype. |
| tabstop is offline | |
| | #8 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| You need to include whatever header file "person" was defined in.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #9 |
| Registered User Join Date: Oct 2009 Location: Edmonton, Canada
Posts: 5
| ok - that sounds maybe like what I'm looking for... Your saying I should put something like the following into a header file and include the header file in all the source files, instead of declaring them globaly? Code: typedef struct person
{
char lastName[20];
char firstName[20];
char age[4];
char position[20];
} person;
|
| derekeverett is offline | |
| | #10 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Yes, it should be included in all source files that need to use that structure.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #11 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| See i think u should have header files also where there will only be signature of the functions and where and in a separate .cc file u should define them. And where ever u want to use them just include the header files. And please put header guards |
| RockyMarrone is offline | |
| | #12 | |
| Registered User Join Date: Oct 2009 Location: Edmonton, Canada
Posts: 5
| Quote:
For the record though.. I didn't use prototypes at all. Everything compiled without even warnings. | |
| derekeverett is offline | |
| | #13 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Do not dismiss the prototypes. Calling functions without prototypes can cause hard-to-spot bugs and errors. Use them, even if they are not required. Always.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| functions, pointers, separate, source, structs |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble passing args to functions in other files | Midnight Coder | C Programming | 6 | 01-03-2009 05:13 PM |
| Passing functions as parameters in other functions | meat | C Programming | 7 | 04-19-2006 11:17 AM |
| passing pointers at structs thru functions | nunnu | C Programming | 2 | 05-12-2004 09:16 AM |
| Passing data/pointers between functions #2 | TankCDR | C Programming | 1 | 11-02-2001 09:49 PM |
| Passing data/pointers between functions | TankCDR | C Programming | 1 | 11-02-2001 12:59 AM |