I’ve this simple program and need to know on which basis should I choose the have for the variable (howToPredectThisNumber) (i.e. the size of the char* string).
And Which is best to choose in this case, char[] or char*??
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char* name;
}Emp;
void init(Emp** emp)
{
int howToPredectThisNumber = 50;
*emp = malloc(sizeof(Emp));
(*emp)->name = NULL;
(*emp)->name = calloc(howToPredectThisNumber, sizeof(char*));
}
void release(Emp** emp)
{
free((*emp)->name);
free(*emp);
}
void setName(Emp* emp, char* newName)
{
strcpy(emp->name, newName);
}
char* getName(Emp* emp)
{
return emp->name;
}
int main(void)
{
Emp* emp;
init(&emp);
setName(emp, "Muhammad Abdullah");
printf("%s", getName(emp));
release(&emp);
return 0;
}
I guess you should delay that deduction until you know what you want to copy: