Passing a string array to another function

Tom

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 !

4386427

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.

  1. Let the function allocate dynamic memory

or

  1. 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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing a parameter to another function

Ruby on Rails: Passing string array to highchart function

Passing char array to function and copying contents to another char array

Passing array generated within a function to another called function (integration) in python

Passing a string to function but expecting an array

Passing an array parameter of a function to another function used within the previous function

Passing a String Array to a Function

Reading 2D array from a file and passing it to another function

Passing a 2D array from one JavaScript function to another

Passing a char array to a function that expects a const std::string reference

Passing string from a function into another function

Passing array of functions to another function

Passing a Function into a Parameter of Another Function

Passing string of entry to another function with tkinter

Passing the elements of an array as argument list to a function (not as a combined string)

C++ - Returning an array to main function and passing array to another function

Type mismatch error passing a string to a function from an array (in VBA)

passing an array into a class function from another class's constructor

Delphi - Passing variable pointer as string to another function

Passing char array to another function

Allocate a dynamic 2d-array by passing it to another function by reference

Passing bidimensional String Array variable from 1 activity to another

Passing dynamically allocated array from function to another fuction

Passing array to another class

Passing string parameters into function from one component to another in react

Passing a function with array parameter as a parameter to another function

Passing an array as a parameter to a function to pick a random string

Loop the JSON String using filter function by passing array values, Javascript

passing a string variable to a function np.array