-1

I am about to use random numbers to choose a signal but can't assign the number to that signal's name.

In this code, I have three input ports which their name are: A1B, A2B, A3B, A4B

now I want to use them randomly by a rand function between 1 to 4.

rand = 4, then input A4B selected.
rand = 2, then input A2B selected.
rand = 3, then input A3B selected.

This is not an array that I use simple ().

for (i=1; i<5; i++)
   A[i]B = 1 + i;

something like this.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

1 Answers1

1

Simple and readable approach would be using switch cases.

If you don't want have switch or if-else.

Have a array of pointer pointing to those variables.

Example:

int A1B,A2B,A3B,A4B;
int *ptr[] = {&A1B, &A2B, &A3B, &A4B};

for (i=1; i<5; i++)
   *ptr[i-1] = 1 + i;

Note: you need to dereference ptr[i] to access the value and *ptr[1] will give you A2B.


You don't need to have array of pointers if you can have array of ports directly.

example:

int AxB[100];
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44