I am trying to print the values that are in an array. Inside ascii(), I printed the values to check if the values are getting transferred to the main function without any problems. I also made random_values so that all integers inside the array are between 33 and 126.
Everything looked fine, but the problem is that when I comment that part code that I wrote to check inside ascii(), the values in the main function gets messed up. It gives me values like 384, 386, 387.
I think this is some kind of memory problem but I don't know much about memory and pointers.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
void random_values (int a[], int l) {
for (int i = 0; i < l; i++) {
a[i] = (rand() % 94 + 33);
}
}
void set_length () {
//generates random integer between 8 and 15 which is used as the length of the array[]
length = (rand() % 8 + 8);
}
int ascii (int **pass)
{
int array[length];
//for assigning random values to the array []
random_values(array, length);
//the printed output from the main fuction is different if I comment this part - random_values() don't work
for (int i = 0; i < length; i++)
{
printf("%d ", array[i]);
}
*pass = array;
return 0;
}
int main () {
//to prevent rand() from producing the same value every time
srand(time(NULL));
set_length();
int *password = malloc(sizeof(int) * length);
ascii(&password);
for (int i = 0; i < length; i++) {
printf("%d ", password[i]);
}
//just to check
printf("\nlength is %d", length);
printf("\n");
}