Search code examples
cheaderadt

ADT (using a header file, source file and main file) in C


I am trying to create a simple ADT using a structure that takes 2 dates. Then returns an age. It must use a Header file, a source file for the Header file, and a main file.

This is what I have it runs and nothing happens. Can someone tell me what i am doing wrong?

age.h

#ifndef AGE_H_

#define AGE_H_

typedef struct getage * Age;

#define MAX  5

Age get_Age(int birthYear, int yearNow);

void age_Destroy(Age a);


#endif

age.c

#include <stdio.h>

#include "age.h"


struct getage {

       int birthYear;

       int yearNow;

};

Age a[1];

Age get_Age(int birthYear, int yearNow){

             int giveAge = 0;

             giveAge = a[0]->yearNow - a[0]->birthYear;

             printf("%d",giveAge);

             return 0;

            }

void age_Destroy(Age a){

                     free(a);

     }

main.c

#include <windows.h>

#include <stdio.h>

#include "age.h"


void age_print(Age a);

void age_print(Age a){

        printf("%d\n", &a);

}



int main() {

    Age a;

    get_Age(1986, 2020);
    
    age_print(a);

    printf("%d\n", &a);

    system("pause");

    //age_Destroy(a);
    
    
}

Solution

  • What are wrong:

    • In the function get_Age:
      • Instead of allocating structures, a[0] (global variable, initialized to NULL) is dereferenced.
      • 0 (converted to NULL) is returned instead of returning an age.
    • In the function age_Destroy:
      • free() is used without declaration nor including proper header.
    • In the function age_print:
      • Data having wrong type is passed to printf(): %d requests int but Age* is passed.
    • In the function main:
      • The return value of get_Age is dropped.
      • Data having wrong type is passed to printf(): %d requests int but Age* is passed.

    Fixed code that won't cause Segmentation Fault nor undefined behavior:

    age.h (not changed)

    #ifndef AGE_H_
    
    #define AGE_H_
    
    typedef struct getage * Age;
    
    #define MAX  5
    
    Age get_Age(int birthYear, int yearNow);
    
    void age_Destroy(Age a);
    
    #endif
    

    age.c

    #include <stdio.h>
    
    #include <stdlib.h> // for malloc() and free()
    
    #include "age.h"
    
    
    struct getage {
    
           int birthYear;
    
           int yearNow;
    
    };
    
    Age get_Age(int birthYear, int yearNow){
    
            Age a = malloc(sizeof(*a)); // allocate a structure
    
            if (a == NULL) { perror("malloc"); exit(1); }
    
            a->yearNow = yearNow; // assign data
    
            a->birthYear = birthYear;
    
            int giveAge = 0;
    
            giveAge = a->yearNow - a->birthYear;
    
            printf("%d",giveAge);
    
            return a; // return pointer to the allocated structure
    
    }
    
    void age_Destroy(Age a){
    
            free(a);
    
    }
    

    main.c

    #include <stdlib.h> // more portable header for system()
    
    #include <stdio.h>
    
    #include "age.h"
    
    
    void age_print(Age a);
    
    void age_print(Age a){
    
            printf("%p\n", (void*)a); // use valid combination of format and data
    
    }
    
    
    
    int main() {
    
        Age a;
    
        a = get_Age(1986, 2020); // assign the return value
        
        age_print(a);
    
        printf("%p\n", (void*)a); // use valid combination of format and data
    
        system("pause");
    
        age_Destroy(a); // enable freeing
        
        
    }
    

    (Some behavior may look weird, but I believe this is valid because not desired behavior is described.)