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 !