Two days ago I posted this question about organizing multiple .c and .h files in a console application project. I followed everybody's guidance and advices from there and it led me to this project structuring:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"
#include "search.h"
int main () {
...
clear();
...
search();
...
return 0;
}
utils.c:
#include "utils.h"
void clear() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
utils.h:
#ifndef UTILS_H
#define UTILS_H
#include <stdlib.h>
void clear();
#endif
structs.h:
#ifndef STRUCTS_H
#define STRUCTS_H
typedef struct {
char *brand;
float fee;
} Brand;
typedef struct {
Brand brand;
char *model;
char *color;
float price;
} Car;
#endif
search.c:
#include <search.h>
Car *search () {
...
clear();
...
Car cars[255];
...
return cars;
}
search.h:
#ifndef SEARCH_H
#define SEARCH_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"
Car *search ();
#endif
This is the project's setup in the project folder:
This is the compiling command I'm running: gcc -o output main.c utils.c search.c
Now, these last two days were spent mostly on fixing errors that the terminal exhibited concerning various problems across all files. Today, I finally managed to fix all the console errors, and when I finally run the compiling command once more... nothing happened.
I'm really lost once again and don't know what I'm missing. Any help is REALLY appreciated and as always, sorry for the sloppy english! (in the screenshots you can see I'm brazilian)
I tried out compiling and running your code, and immediately, the compiler complained about your local "search.h" include file.
#include <search.h>
As noted in the answers in your other question reference, the file reference should be enclosed within double quotes.
#include "search.h"
After making that tweak, the program did compile, but the compiler registered a warning about attempting to return a local structure array value from the function back to the calling code.
/home/craig/C_Programs/Console/Cars/search.c|9|warning: function returns address of local variable [-Wreturn-local-addr]|
Car *search () {
...
clear();
...
Car cars[255];
...
return cars;
}
My guess is that when you compiled this set of code, you also received a similar compiler warning.
That methodology won't provide you with the desired results as the local structure array "cars", will be destroyed once the function is exited. If you want to define the structure array within a function, one would allocate the memory for the structure array, assign a pointer to the array, and then return the pointer.
With that in mind, following is a refactored version of the "search" function and the "main" function to provide food for thought.
#include "search.h"
Car *search (void)
{
cleared();
Car *cars = malloc(255 * sizeof(Car)); /* Allocate and pass back the pointer to the memory block */
cars[0].brand.brand = "Chevy"; /* Just initialized some sample data */
cars[0].color = "Candy Apple Red";
return cars;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "utils.h"
#include "structs.h"
#include "search.h"
int main () {
cleared(); /* Cleared the terminal */
Car * cars = search(); /* Set up the pointer to the structure array */
printf("Brand: %s, color: %s\n", cars[0].brand.brand, cars[0].color); /* Simple test output */
return 0;
}
Some key things to note.
Adding in some test code to provide a bit of illustrative data, following was the output at the terminal.
Brand: Chevy, color: Candy Apple Red
FYI, since I was coding on a Linux machine, I decided to revise the name of the "clear" routine just to avoid any conflict or confusion that a custom function was being used. You probably will be okay if you leave the function name as "clear".
Give those bits of refactoring a try. The key things to take away are be cognizant of any and all compiler warnings as well as any compiler errors. Warnings usually indicate that the program will run with some undefined behavior if not addressed. Also, you may want to delve in some more into additional "C" tutorial material as it pertains to defining elements such as arrays and allocating memory for them as well as the scope of such memory allocation.