0

*tabP[] contains addresses of p1[], p2[] and p3[]. Their indexes are declared as ranq which is random value between 0-3, but despite that, compiler output is always p1[0], never p1[1] or p1[2].

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

int ranq;

int random()// Randomizer
{
    srand(time(0));
    ranq = rand() % 2;
}
int main()
{
    int i = 0;
    const char* p1[3] = { "aaa", "bbb", "ccc" };
    const char* p2[3] = { "ddd", "eee", "fff" };
    const char* p3[3] = { "ggg", "hhh", "iii" };
    const char** tabP[3] = { &p1[ranq], &p2[ranq], &p3[ranq] };
    do
    {
        random();
        printf(*tabP[i]);
        ++i;
    } while (i != 4);
    return 0;
}

Output: ( Every time I run the program)

aaa
ddd
ggg

Desired output should contain random contents of arrays. How to fix that?

xantico
  • 3
  • 2
  • 4
    Can you please make your program a self-contained example? The structure and `...` elisions don't help. – AKX Jan 20 '22 at 20:02
  • 1
    maybe change ranq = rand() % 2; to ranq = rand() % 3? – Asphodel Jan 20 '22 at 20:03
  • 2
    You seed once, not on every single invocation. If you're re-seeding the rng to the same seed (i.e., clock second) each invocation, you're going to get the same result each time. – GManNickG Jan 20 '22 at 20:09
  • @StephenNewell no. I'll specify my question. – xantico Jan 20 '22 at 20:30
  • @AndreasWenzel I did. Now it should be readable. – xantico Jan 20 '22 at 20:40
  • besides the `srand()` problem, `ranq` is used before explicit initialization, i think it defaults to `0`. – MarcoLucidi Jan 20 '22 at 20:44
  • @MarcoLucidi: You are correct that `ranq` is initialized to `0`. – Andreas Wenzel Jan 20 '22 at 20:45
  • Thanks for updating to a minimal example. As written, this is still an issue with `srand` as previously stated. Also, as mentioned above if you want to get indices 0-2, you need `%3`. – GManNickG Jan 20 '22 at 20:46
  • 1
    You are changing the value of `ranq` in every call to `random`, but you are never using this new value of `ranq` afterwards. This does not make sense. The only time you every use the value of `ranq` is when it has the value `0`, which is before the first call to `random`. – Andreas Wenzel Jan 20 '22 at 20:50
  • 1
    @AndreasWenzel Didn't even notice that too! Also, `i` goes beyond the bounds of the array it accesses... – GManNickG Jan 20 '22 at 20:57
  • @AndreasWenzel thanks for confirming. so even if op fixes `srand()` he'll still get `p1[0]`, `p2[0]`, `p3[0]`. also I tried to run the program and got a segfault right away. I agree with you that all this makes little sense. – MarcoLucidi Jan 20 '22 at 20:58
  • It would probably make sense to change the line `printf(*tabP[i]);` to `printf( tabP[i][ranq] );`. That way, it will select one random string from the first line, one random string from the second line and one random string from the third line. – Andreas Wenzel Jan 20 '22 at 21:01
  • 1
    I believe I have now fixed all bugs mentioned above. Also, I have cleaned up the code a bit. As far as I can tell, the program seems to work. See [this link](https://godbolt.org/z/G5Mfeq9j3). – Andreas Wenzel Jan 20 '22 at 21:07
  • Just for comparison: Here is [a link](https://godbolt.org/z/znzjaf6e9) to the program in which I fixed all bugs, but did not clean up the code more than absolutely necessary. – Andreas Wenzel Jan 20 '22 at 21:20

0 Answers0