Thread: Breaking up source code into multiple files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    42

    Breaking up source code into multiple files

    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’
    Last edited by mherald81; 10-14-2010 at 08:36 PM. Reason: added errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 09:07 AM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM