For test my hashmap on C language I need to generate many keys (string). I wrote this code for generate keys. And this code doesn't works.
char* keys [15] = { 0 };
for (int i = 0; i < 15; i++)
{
char key [7] = { 0 };
for(int j = 0; j < 5; ++j)
{
int rand_char = rand()%26+'A';
key[j] = (char)rand_char;
}
*(keys + i) = key;
}
for (int i = 0; i < 15; i++)
printf("%s\n", keys[i]);
The problem is all keys are the same and I don't know why this happens. OUPUT:
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
I tried putting the random result into a variable (int rand_char) and also tried the debug keys during the generation process and saw that all the letters change when new letters are generated, as if all the lines were one string.
char* keys [15] = { 0 };
for (int i = 0; i < 15; i++)
{
char key [7] = { 0 };
for(int j = 0; j < 5; ++j)
{
int rand_char = rand()%26+'A';
key[j] = (char)rand_char;
for (int k = 0; k < i; k++)
printf("%s\n", keys[k]);
}
*(keys + i) = key;
}
OUPUT:
B
BM
BMQ
BMQB
BMQBH
C
C
CD
CD
CDA
CDA
CDAR
CDAR
CDARZ
CDARZ
O
O
O
OW
OW
OW
OWK
OWK
OWK
OWKK
OWKK
OWKK
OWKKY
OWKKY
OWKKY
H
H
H
H
HI
HI
HI
HI
HID
HID
HID
HID
HIDD
HIDD
HIDD
HIDD
HIDDQ
HIDDQ
HIDDQ
HIDDQ
S
S
S
S
S
SC
SC
SC
SC
SC
SCD
SCD
SCD
SCD
SCD
SCDX
SCDX
SCDX
SCDX
SCDX
SCDXR
SCDXR
SCDXR
SCDXR
SCDXR
J
J
J
J
J
J
JM
JM
JM
JM
JM
JM
JMO
JMO
JMO
JMO
JMO
JMO
JMOW
JMOW
JMOW
JMOW
JMOW
JMOW
JMOWF
JMOWF
JMOWF
JMOWF
JMOWF
JMOWF
R
R
R
R
R
R
R
RX
RX
RX
RX
RX
RX
RX
RXS
RXS
RXS
RXS
RXS
RXS
RXS
RXSJ
RXSJ
RX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