-1

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 !

h0r53
  • 3,034
  • 2
  • 16
  • 25
Tom
  • 1
  • 3
  • 1
    Why tag both, C and C++? – lulle2007200 Oct 26 '21 at 16:31
  • Does this answer your question? [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – πάντα ῥεῖ Oct 26 '21 at 16:34
  • A `char *` is a pointer to a character, which is valid for c-string pointers. However, you are trying to get a pointer to an array of c-strings, so you'll minimally need a `char **` as the return value for `getName`. Note, `array` is already technically a `char **`. – h0r53 Oct 26 '21 at 16:36

2 Answers2

0

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);
    }
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

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;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63