Home > Net >  return a modified array of char**
return a modified array of char**

Time:09-24

C- language——-Trying to call a function "test" from main(). This takes 3 arguments argc, argv and a pointer function which either turns the string into lower case or upper case. Then using the array of modified values i will just loop and print them.

int main(int argc, const char *const *argv) {

  char **val1 = test(argc, argv, &toupper);
  char **val2 = test(argc, argv, &tolower);

for (char *const *p = val1, *const *q = val2; *p && *q;   argv,   p,   q) {
    printf("[%s] -> [%s] [%s]\n", *argv, *p, *q);
  }

}

change.c file has the function definition.
Here i loop through the list. Allocate memory for each element in the array and according to the pointer function "chnge" the string is changed to upper or lower case. How do i return this array of newly modified strings back to my main?

char **test(int argc, const char *const *argv, int (*const chnge)(int)){

    char *retArr = malloc(argc * sizeof(char*));

    for (i = 0; i < argc;   i) {

        char *str = argv[i];
        int len = strlen(str) 1;

        char *low = malloc(len * sizeof(char*));
        char *up = malloc(len * sizeof(char*));

        for (int i = 0; i < len;   i) {

            if(chnge == &tolower) {

                 low[i] = chnge(str[i]);
                 retArr[i] = low[i];
                 return (char **)retArr;

            } else {

                 up[i] = chnge(str[i]);
                 retArr[i] = up[i];
                 return (char **)retArr;

            }
        }
        free(lower);
        free(upper);
        free(retArr);
 }
}

I get segmentation fault error.
I want to return the entire array. So that in my main() i will loop through it and print it. How can i do this? Where am i going wrong in my code? Any help would be great.

CodePudding user response:

Your codes have some incorrect points and are redundant as well. You can try the below codes

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

char **test(int argc, const char *const *argv, int (*const chnge)(int)){

    char **retArr = malloc((argc 1) * sizeof(char*));

    for (int i = 0; i < argc;   i) {

        const char *str = argv[i];
        int len = strlen(str);

        char* transform = malloc((len 1) * sizeof(char));

        for (int j = 0; j < (len 1);   j) {
            transform[j] = chnge(str[j]);
        }
        retArr[i] = transform;
   }
   retArr[argc] = NULL; // An array of char* terminated by NULL
   return retArr;
}


int main(int argc, const char *const *argv) {

  char **val1 = test(argc, argv, &toupper);
  char **val2 = test(argc, argv, &tolower);

  for (char *const *p = val1, *const *q = val2; *p && *q;   argv,   p,   q) {
    printf("[%s] -> [%s] [%s]\n", *argv, *p, *q);
    free(*p);
    free(*q);
  }
  free(val1);
  free(val2);
}

CodePudding user response:

I believe the answer may have to do with the fact that you are trying to directly act on a string 1. without using strcpy, and 2. using a pointer array (char*) instead of an array object (char[]) which can cause a segfault.

Sorry, this would better be suited to a comment and not an answer, but I unfortunately can't comment quite yet. This may be of help?

  • Related