0

I need to make a function that will create an int array with a random size and random characters then return it.

Because I can't make the array a global variable as the size is unknown, I created a separate function for that. I then tried to use a pointer to return the array so that I can use it inside the main function to print the int array as a character.

This is the code I have so far but it gives me the error: 'array initializer must be an initializer list or wide string literal'

int length;

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 array[length];

    //this is a function that assigns the values into the array
    random_values(array, length);

    //just to check
    for (int i = 0; i < length; i++) {
        printf("%c", array[i]);
    }

    return array;

}


int main () {

    //to prevent rand() from producing the same value every time
    srand(time(NULL));

    set_length();

    int password[] = * ascii();


    //what i want from this code but instead gives me the error
    for (int i = 0; i < length; i++) {
        printf("%d", password[i]);
    }
}

Any help will be greatly appreciated. Thanks.

lemnel
  • 19
  • 4

2 Answers2

1

You may not return a local array with automatic storage duration from a function because it will not be alive after exiting the function.

And moreover this declaration

int password[] = * ascii();

is wrong. You may not initialize an array with a scalar value because in any case a pointer of the type int * points to a single object.

So you need to allocate an array dynamically.

For example

int *array = malloc( length * sizeof( int ) );

Also it is a bad idea to use the global variable length. You could make it a parameter of the function ascii.

In turn the function set_length could return the calculated value of length.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int length;

void set_length () {
    //generates random integer between 8 and 15 which is used as the length of the array[]
    length = (rand() % 8 + 8);
}
// Here i add new variable to your function double pointer
int ascii (int **pass)
{

    int array[length];

    //this is a function that assigns the values into the array
    // Change this whith generate random that you use : random_values(array, length);
    for (int i = 0; i < length; i++)
    {
        array[i] = i;
    }
    

    //just to check
    for (int i = 0; i < length; i++) 
    {
        printf(" random number is %d \n", array[i]);
    }
    // save your data in new pointer that we passed to the function
    *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);
    // Here we pass adress of pointer
    ascii(&password);


    //what i want from this code but instead gives me the error
    for (int i = 0; i < length; i++) {
        printf(" Sheck : %d\n", password[i]);
    }
}
benlyazid
  • 92
  • 1
  • 8
  • I actually found another very weird problem now with the code you wrote. I made the random_values() function only to assign int values from 33 to 126. This works when I use your code but when I comment out the 'just to check' part, it doesn't work. I'm guessing it's a memory problem which I really don't know anything about. Why is this happening? – lemnel Sep 20 '20 at 12:08
  • thit commet this line `*pass = array;` ? – benlyazid Sep 20 '20 at 12:12
  • No I didn't. I used rand() % 94 + 33 for the code but it's giving me values like 387. – lemnel Sep 20 '20 at 12:15
  • Do you have a problem with random function or with memory – benlyazid Sep 20 '20 at 12:31