Thread: Help - Program Challenge for C Buffs

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Help - Program Challenge for C Buffs

    Ok I'm writing a program and had a question. I've attached the declare.h, main.c, and printdata.c files i've created. My question is this...I'm trying to create a log file to record errors and success's to record stages the program goes through. Successful openings, etc. like setup.logs that come with anything. In my declare.h I've made two global variables int rc and char filename[100]...In main I've said rc=create_log_file(filename);

    First question: Does the statement rc=create_log_file(filename); send the array to the function create_log_file and if so why do I have to receive the filename as int create_log_file(char* filename)...Why do I have to do pointer. Is it because since it is in a different c file you have to use pointer in the printdata in order to see filename?? I'm going to use filename to store just that the directory ie: C:\\Hern\\HR on where to store log file and create. Help me if I am understanding this wrong. Maybe I do not understnad that when yuou pass array you must accept as pointer. Help please

    declare.h
    Code:
    #ifndef DECLARE_H
    #define DECLARE_H
    
    
    //Function prototypes
    void print_menu();
    
    //Global Variables
    char filename[100];
    int rc;
    #endif;
    main.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    #include "declare.h"
    #include "employee_data.h"
    
    int main()
    {
    	
    	int choice; //local variable
    	
    	rc=create_log_file(filename);
                    return 0;
    }
    printdata.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    #include "declare.h"
    #include "employee_data.h"
    
    extern int rc;
    extern char filename[100];
    
    /****************************************
     create_log_file() - create new log file
     Return: 0 - Success
             1 - Error
     ****************************************/
    int create_log_file(char* filename)
    {
    }

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You can't send an entire array in C you must use pointers when you send an array ex
    Code:
    int array[]={1,2,5,3};
    do_func(array);
    you are actually sending the memory address of that array that is why you use a pointer.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    why are you declaring rc and filename in declare.h and in printdata.c?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Maybe I do not understnad that when yuou pass array you must accept as pointer.
    Arrays decay into pointers to the first element in all but three cases: As the operand of sizeof, as the operand of the address-of operator and as a string literal initializer for an array of char. For any other use you should recognize that you end up working with a pointer to the first element instead of the array as a whole, and that includes passing arrays as a function parameter.
    Code:
    #ifndef DECLARE_H
    #define DECLARE_H
    
    
    //Function prototypes
    void print_menu();
    
    //Global Variables
    char filename[100];
    int rc;
    #endif;
    Header guards, good. Global variables, bad. If you must use them then the preferred way to go about it is to have a single .c file for the definitions and use the header for declarations:

    declare.h
    Code:
    #ifndef DECLARE_H
    #define DECLARE_H
    
    //Function prototypes
    void print_menu(void);
    int create_log_file(void); /* No need for an argument, filename is global */
    
    //Global Variables
    extern char filename[];
    extern int rc;
    #endif
    declare.c
    Code:
    #include "declare.h"
    
    char filename[100];
    int rc;
    main.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    #include "declare.h"
    #include "employee_data.h"
    
    int main()
    {	
      int choice;
    	
      rc=create_log_file();
      return 0;
    }
    printdata.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    #include "declare.h"
    #include "employee_data.h"
    
    /****************************************
     create_log_file() - create new log file
     Return: 0 - Success
             1 - Error
     ****************************************/
    int create_log_file(void)
    {
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Ok so anytime I pass an array as a parameter for function I must use pointers. Got ya! Thanks all..Prelude, for the way I'm doing this program it makes more sense to keep the few global in declare.h instead of using extern there. When I finish I'll send thanks to all who replied

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM