0

So, I'm trying to return a string from a char* function but I always get this warning: function returns address of local variable [-Wreturn-local-addr], and the string doesn't print to the console.

Yesterday I tried to printf(password = createPassword(quantity) and it worked butwhen I tried it today again it doesn't print the string.

This is the fucntion

char* createPassword(int a){
    char contadorTotal = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0,password[30000] = "", randomChar, temp[100000];
    int random;
    srand((unsigned)time(NULL));

    while (a > 0)
    {

        do
        {

            random = (rand()%(5-1)) + 1;

            if(random == 1){
                if(c1 < 4){

                    randomChar = (33 + rand() % (48-33));

                    sprintf(temp, "%c", randomChar);
                    strcat(password, temp);

                    c1++;
                    contadorTotal++;
                }
            }else if(random == 2){

                if(c2 < 3){
                    randomChar = rand() % 26 + 97;

                    sprintf(temp, "%c", randomChar);
                    strcat(password, temp);

                    c2++;
                    contadorTotal++;
                }
            }else if( random == 3){
                if(c3 < 3){
                    randomChar = (65 + rand() % (91-65));

                    sprintf(temp, "%c", randomChar);
                    strcat(password, temp);

                    c3++;
                    contadorTotal++;
                }
            }else if(random == 4){
                if(c4 < 3){
                    randomChar = (48 + rand() % (58-48));

                    sprintf(temp, "%c", randomChar);
                    strcat(password, temp);

                    c4++;
                    contadorTotal++;
                }
            }

        }while (contadorTotal < 13);

       a--;
       c1 = 0;
       c2 = 0;
       c3 = 0;
       c4 = 0;
       contadorTotal = 0;
       strcat(password, "\n");

    }   
    return password;

}

And this is where I'm trying to print the result


case 'c':
            int quantity;
            char* password;

            printf("Insert how many passwords you want to create: ");
            scanf("%d", &quantity);       
            printf("----------------------\n");
            password = createPassword(quantity);
            printf(password);
            break;
        }

The result should be the password created printed on the console but it shows nothing.

  • 1
    `return strdup(password);` at the end of the function and `free(password);` in the caller after you are done with it. – mch Oct 25 '19 at 13:53
  • Simple fix: `static char password[30000];`. (But do you really need that big an array?) – Steve Summit Oct 25 '19 at 13:57
  • Try and rewrite your function so that it receives a pointer where to store the password rather than storing the password in its own local array. `int createPassword(int a, char *destination) { /* your code using destination to store password */ }` and call it like this: `char password[100]; int quantity; ... if (quantity < 100) createPassword(quantity, password);` – pmg Oct 25 '19 at 13:57
  • I went with mch solution and it worked well, thanks a lot everyone! I probably have to do some C courses to get better at it! – Ricardo Sampaio Oct 25 '19 at 14:02

1 Answers1

2

To begin with, arrays are not pointers! But, arrays can decay into pointers.

Moreover, your local array is stored in stack space and lives until the function returns. To overcome the issue, either you need to make it static or allot a some space needed in heap by using malloc or calloc functions. strdup(..) is an other alternative as well.