1

I am having a hard time putting a string into an array.

The purpose of my program is to generate random strings. And I want those random strings generated to be placed on an array so I can access those strings again later.

char main()
{
    srand(time(NULL));
    char* rStr[9] = {0};

    int i;
    for (i = 0; i < 7; i++)
    {
        randomString(rStr, 8);    //This is the function that generates a 
                          //random string with 8 characters
        printf("%d---%s\n", i, rStr);
    }   
}

The above code will produce 7 randomly-generated strings with 8 characters. But my problem is I want every strings generated to be placed on an array so I can call them or display them again.

I have tried this below:

char main()
{
    srand(time(NULL));
    char* rStr[9] = {0};
    int i;
    for (i = 0; i < 7; i++)
    {
        rStr[i] = randomString(rStr, 8);
        printf("%d---%s\n", i, rStr[i]);
    }   
}

but my program would just crash.

EDIT:

int randomNumber(int min, int max)     //This function is responsible for 
                                       //the randomness of the string
{
    max -= min;
    return (rand() % max) +min;
}

char randomString(char *str, int randomCharCount)
{
    const char *charSet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i;
    for (i = 0; i < randomCharCount; i++)
    {
        str[i] = charSet[randomNumber(0, 35)];
    }
}

Credits of the code to the tutorial right here:

https://www.youtube.com/watch?v=3l1TKWVKyAY&index=58&list=PL0170B6E7DD6D8810&fbclid=IwAR2qq87y9qKt4PKPSZrwoRF10j1V9NU9W055j3-1YBXNBU7YtFDMMa1mxMg

https://www.youtube.com/watch?v=3l1TKWVKyAY&index=58&list=PL0170B6E7DD6D8810&fbclid=IwAR2qq87y9qKt4PKPSZrwoRF10j1V9NU9W055j3-1YBXNBU7YtFDMMa1mxMg

Mike
  • 3,513
  • 4
  • 14
  • 31
ktl159
  • 61
  • 9
  • The problem is not in the code you show, but in the code you *don't* show. What does `randomString` *really* do? What does it *return*? Please create a [mcve] to show us. – Some programmer dude Jan 02 '19 at 03:45
  • `char* rStr[9] = {0}` creates an ***array of 9 pointers to char***, not an array of `char [9]`?? (and be very wary of learning how to code from youtube...) – David C. Rankin Jan 02 '19 at 04:02
  • Your `randomString` function is declared to return a single `char`. And in fact doesn't return anything at all. You also dereference the pointer being passed, which is not the correct type (you pass a pointer to a pointer to `char`, i.e. `char **`). – Some programmer dude Jan 02 '19 at 04:07
  • Unless you are coding for a *freestanding* embedded system without an Operating System your declaration for `main` is incorrect. For hosted systems, the proper declarations for `main` are `int main (void)` and `int main (int argc, char **argv)` See: [C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Jan 02 '19 at 04:09

2 Answers2

1

Think you're looking for strcpy.

Initialising and then "pushing" to the array:

char stringarray[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; 
strcpy(stringarray[0], "blah");

Also main should return an int see valid options below for main:

int main(void);

or:

int main(int argc, char* argv[]);

References:

How do I create an array of strings in C?

What are the valid signatures for C's main() function?

Adam Dad
  • 11
  • 4
1

Your code has following problems

  1. No return type for main().
  2. You have to declare separate array for saving strings.

Try the following code :-

char main()
{
  srand(time(NULL));

  char rStr[9] = {0};
  // sStr is where the rStr are saved .
  char sStr[50][9];  // max 50 rStr
  int i;
  for (i = 0; i < 7; i++)
  {
    randomString(rStr, 8);
    strcpy(sStr[i],rStr);
    printf("%d---%s\n", i, sStr[i]);
  }
 // some return value here.
return 0;
} 
anoopknr
  • 2,505
  • 2
  • 14
  • 25
  • 2
    Thank you very much for answering my question and providing a code, anoopknr! This is a huge help for me. God bless! – ktl159 Jan 02 '19 at 04:18