0

So I've been trying to do this for an embarrassingly long time now. First of all here is my (very bad) code:

char firstInfection() {            
     char cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};            
     srand(time(NULL));            
     int randnum = rand() % 5;            
     char firstCity = cities[randnum];            
     return firstCity; 
}

I didn't include the preprocessor stuff but I'm also using string, math and time (obviously stdio and stdlib too)

Line 2 errors out here, saying: error: too many initializers for ‘char []’ I'm not really sure what's going on here.

I'm having trouble understanding some of the things I need to do. I was told on tutorialspoint that I have to initialize random with line 3: srand(time(NULL)) I'm not sure why lol.

I tried shortening the names of the elements in cities[]

I'm not really sure what else to do try. I'm guessing that the whole thing is completely wrong but I really don't know.

abelenky
  • 63,815
  • 23
  • 109
  • 159
C3A1
  • 11
  • 2
  • 1
    you need `char* firstCity`. also this has undefined behavior as you are returning off the stack – Daniel A. White Mar 27 '22 at 01:41
  • 1
    you are not working with character literals correctly in C. in C, character literals require single-quotes (') instead of double quotes and must contain a single character. if you intended to work with "strings", so char-arrays, you should define and initialize your array as `char* cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};` – D3PSI Mar 27 '22 at 01:45
  • it looks like the only difference here is the pointer... im not really sure how that fixes it but i'll still try that! – C3A1 Mar 27 '22 at 01:47
  • check out "strings" in C: https://www.tutorialspoint.com/cprogramming/c_strings.htm, and array as pointer in C: https://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm – D3PSI Mar 27 '22 at 01:49

2 Answers2

0
#include <stdio.h>

const char* firstInfection() {            
     static const char* cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};            
     srand(time(NULL));            
     int randnum = rand() % 6;  // Select value 0 to 5 (inclusive)            
     const char* firstCity = cities[randnum];            
     return firstCity; 
}

int main(void) {
    
    printf("Random String: %s\n", firstInfection());
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • thats still undefined behavior! – Daniel A. White Mar 27 '22 at 01:41
  • ohhh pointers xD im not really sure what the difference here is though? Like in what situation should I use a pointer and when shouldnt i? – C3A1 Mar 27 '22 at 01:45
  • the use of `char*` as type for the elements of the array is nothing more as C's way of referring to a multi-character string as there does not exist a thing such as a "string" in C. In C, strings are represented as an array of `char`'s. An array type can be referred to by its base pointer, which is why we can use `char*` instead of `char[]` – D3PSI Mar 27 '22 at 01:52
  • oh and also this solution errors out again. ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 4 | char* cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"}; and it does this for every element in the array. – C3A1 Mar 27 '22 at 01:53
0

Answer given by @abelenky it not that good as he/she is calling srand() function inside a function which is not main(). NOTE: srand() should be called only once in any program within the main() function.

Some Improvements:

  • Instead to using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • Data type char means a single character which is enclosed within single quotes '', use char * (c-string) to store multiple characters.
  • Always use size_t to store indexes of any arrays, instead of int
  • Only C If your function is not taking any arguments then write it like this int func(void) { }, here's why.
  • Instead of using bare rand() % 5, get the size of the array stored on stack memory using sizeof() operator
  • Mark your local variable cities as static because you are returning it too.
  • Your function firstInfection() should be declared with static linkage.
  • You are calling time() function which is declared in time.h header file, so include that too

Final Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static const char *firstInfection(void)
{
    static const char *cities[] = {"aa", "bb", "cc", "dd", "ee", "ff"};
    size_t rnd_index = rand() % (sizeof(cities) / sizeof(*cities));
    return cities[rnd_index];
}

int main(void)
{
    srand(time(NULL));
    printf("%s\n", firstInfection());
    return EXIT_SUCCESS;
}

Output:

[tushar@tushar-arch Desktop]$ ./main
aa
[tushar@tushar-arch Desktop]$ ./main
ee
[tushar@tushar-arch Desktop]$ ./main
cc
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23