Hi Everybody,
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);
}