Ok, I'm messing around trying to learn C and got my code working but it was just one large .c file. Now I wanted to break it up into multiple files so it would be easier to see what's going on but ran into some issues. I would appreciate any help with this.
main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
int main()
{
char *name;
name = strdup_func("Brian Gable");
struct employee *eptr = malloc(sizeof *eptr);
build_employee( eptr);
printf("Empolyee name is: %s\n",eptr->name);
printf("Empolyee age is: %d\n",eptr->age);
printf("Empolyee address is: %s\n",eptr->address);
strcpy(eptr->name,name);
printf("Empolyee name is: %s\n",eptr->name);
printf("Empolyee age is: %d\n",eptr->age);
printf("Empolyee address is: %s\n",eptr->address);
name = substrg(name,1,5);
printf("Substring: %s\n", name);
name = reverse_string(name);
printf("Substring reversed: %s\n", name);
return (0);
}
functions.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "type.h"
void build_employee(struct employee *employee_pointer)
{
strcpy(employee_pointer->name, "Michael Clark");
employee_pointer->age = 29;
strcpy(employee_pointer->address, "3302 Runner Rd Apt 107");
}
char *strdup_func(char *instr)
{
char *outstr;
if (!instr)
{
fprintf(stderr, "strdup: FATAL - NULL argument!\n");
return(NULL);
}
outstr = (char *) malloc(sizeof(char) * (strlen(instr)+1) );
if (!outstr)
{
fprintf(stderr, "strdup: FATAL - malloc failed!");
return(NULL);
}
strcpy(outstr, instr);
return(outstr);
}
char *substrg(char *first_string, int start, int numchars)
{
char *p;
int count = 0, length = 0;
length = strlen(first_string);
if(start > length || length < 2 || start < 1)
{
return (NULL);
}
p = (char *) malloc(sizeof(char) * (numchars+1));
start--;
while (count < numchars)
{
p[count]= *(first_string+start+count);
count++;
}
p[count] = '\0';
return (p);
}
char *reverse_string(char *string)
{
char *p;
int slen = 0, count = 0;
slen = strlen(string);
p = (char *) malloc(sizeof(char) * (slen+1));
while (string[count] != '\0')
{
p[--slen]=string[count++];
}
return (p);
}
void update_employee_name(struct employee *employee_pointer, char new_name[80])
{
strcpy(employee_pointer->name, new_name);
}
functions.h
Code:
#ifndef FUNC_H
#define FUNC_H
void build_employee(struct employee *employee_pointer);
char *strdup_func(char *instr);
char *substrg(char *first_string, int start, int numchars);
char *reverse_string(char *string);
void update_employee_name(struct employee *employee_pointer, char new_name[80]);
#endif
type.h
Code:
#ifndef TYPE_H
#define TYPE_H
struct employee
{
char name[80];
int age;
char address[256];
};
extern struct employee *eptr;
#endif
errors
Code:
main.C: In function ‘int main()’:
main.C:9: warning: deprecated conversion from string constant to ‘char*’
main.C:10: error: invalid application of ‘sizeof’ to incomplete type ‘employee’
main.C:10: error: invalid conversion from ‘void*’ to ‘employee*’
main.C:13: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:14: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:15: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:16: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:17: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:18: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’
main.C:19: error: invalid use of incomplete type ‘struct employee’
functions.h:4: error: forward declaration of ‘struct employee’