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.