I have difficulty with pointers and I was wondering how can we get the values of a array of strings with another function using pointers ?
My code is:
char *getName(const char *complete_name) {
char buffer[50];
strcpy(buffer, complete_name);
int i = 0;
char *p = strtok (buffer, ",");
char *array[2]; //array[0] = last name and array[1] = first name
while (p != NULL) {
array[i ] = p;
p = strtok (NULL, ",");
}
printf("%s\n", array[0]); // last name
printf("%s\n", array[1]); // first name
return *array;
}
and my main function is:
int main() {
const char *patient = "Doe,John";
char *p;
int i;
p = getName(patient);
for ( i = 0; i < 2; i ) {
printf("%s\n", p[i]);
}
return 0;
}
My goal is to have acces to the variable array in my main, how can I do that ?
Thank you !
CodePudding user response:
buffer
and array
are variables that only exist inside the function. So returning them (directly or indirectly using pointers) are illegal.
You have two options.
- Let the function allocate dynamic memory
or
- Allocate the memory in
main
and pass a pointer to the memory
Option 1: Dynamic memory
That could be:
char **getName(const char *complete_name) {
char buffer[50];
strcpy(buffer, complete_name);
int i = 0;
char *p = strtok (buffer, ",");
char **array = calloc(2, sizeof *array);
while (i < 2 && p != NULL) {
array[i] = malloc(strlen(p) 1);
strcpy(array[i], p);
i;
p = strtok (NULL, ",");
}
if (array[0] != NULL) printf("%s\n", array[0]); // last name
if (array[1] != NULL) printf("%s\n", array[1]); // first name
return array;
}
int main() {
const char *patient = "Doe,John";
char **p;
int i;
p = getName(patient);
for ( i = 0; i < 2 && p[i] != NULL; i ) {
printf("%s\n", p[i]);
}
free(p[0]);
free(p[1]);
free(p);
return 0;
}
Option 2: Memory allocated in main
and passed to the function
void getName(const char *complete_name, char split[][50]) {
char buffer[50];
strcpy(buffer, complete_name);
int i = 0;
char *p = strtok (buffer, ",");
while (i < 2 && p != NULL) {
strcpy(split[i], p);
i;
p = strtok (NULL, ",");
}
printf("%s\n", split[0]); // last name
printf("%s\n", split[1]); // first name
}
int main() {
const char *patient = "Doe,John";
char split[2][50] = { 0 };
int i;
getName(patient, split);
for ( i = 0; i < 2; i ) {
printf("%s\n", split[i]);
}
return 0;
}
CodePudding user response:
You cant return the reference to local variables.
char *getName(const char *complete_name)
{
const char *end = complete_name;
char *name;
while(*end && *end != ',') end ;
name = malloc(end - complete_name 1);
if(name)
{
memcpy(name, complete_name, end - complete_name);
name[end - complete_name] = 0;
}
return name;
}
int main(void)
{
char *str = "Doe,John";
char *name;
printf("%s\n", name = getName(str));
free(name);
}
If you want both:
char **getName(const char *complete_name)
{
char *wrkname;
char **sname = calloc(sizeof*sname, 2);
if(sname)
{
char *wrkname = strdup(complete_name);
sname[0] = wrkname;
while(*wrkname && *wrkname != ',') wrkname ;
if(*wrkname)
{
*wrkname = 0;
if(*wrkname) sname[1] = wrkname;
}
}
else
{ /* handle memory error */}
return sname;
}
int main(void)
{
char *str = "Doe,John";
char **name = getName(str);
if(name)
{
if(name[0]) printf("Name: %s\n", name[0]);
if(name[1]) printf("Surname: %s\n", name[1]);
free(name[0]);free(name);
}
}