s55michi
s55michi

Reputation: 67

random numbers with pointers (C)

I want to create random numbers within a range (0 - a) and return them with the pointer b and c.

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

void random (int a, int *b, int *c)
{
    srand(time(0));
    *b = rand()%(a+1);
    *c = rand()%(a+1);
    printf("%i%i", *b, *c);
}

int main()
{
    int a = 10; // upp
    int* b;
    int* c;
    random (a, b, c);
}

The Code works, although my executable crashes right after the random().

Any ideas why it crashes?

Upvotes: 1

Views: 3918

Answers (1)

Caleb
Caleb

Reputation: 125007

You need the pointers to point to actual integers. An easy way to do that is to declare b and c as int and then pass their addresses, like this:

int main()
{
    int a = 10; // upp
    int b = 0;
    int c = 0;
    random (a, &b, &c);
}

Upvotes: 3

Related Questions