I'm working on a project in C using layered architecture: App, UI, Domain, Repository and Controller modules. I'm beginner in C. I don't know how to pass strings as parameters from a function to another and finally write to a file. Simplified example:
UI.c:
#include "UI.h"
#include "Controller.h"
#include <string.h>
char* read_type(){
static char type[51];
printf("Type: ");
scanf("%s",type);
return type;
}
void manage_tasks(){
char cmd;
printf("Choose from options: ");
scanf("%c",&cmd);
if (cmd=='1') {
char type[51];
strcpy(type,read_type());
add_control(type);
}
}
Controller.c:
#include "Controller.h"
#include "Repository.h"
void add_control(char type[51]){
add(id, day, money, &type[51]);
}
Repository.c:
#include "Repository.h"
#include <stdlib.h>
void add(char type[51]){
FILE* f;
f=fopen("locatie.....", "a");
if (f==NULL) {
perror("Error while opening the file! \n");
exit(EXIT_FAILURE);
}
fprintf(f,"%s\n", type);
}
I know that every array is the address of the first element, but I did't understand exactly how functions work with string arrays.