通过 C 中的指针将随机生成的 int 分配给数组时出现问题
Problem while assigning random generated int to an array via a pointer in C
当我尝试将 rand()
生成的值分配给指针时,出现了一些问题。
当我为指针分配随机值时,它在 rand_cards()
函数中显示正确的值。但是当我想在其他地方查看结果时,它会显示不同的数字。无论是 flop()
函数还是 main()
。
有时它在 flop()
中也显示正确的值,但在 main()
中显示错误。
有什么问题?
bool card_exists[12][4] = {false};
int table_ranks[5] = {0};
int table_aces[5] = {0};
// We will use these addresses to store random numbers in arrays above.
int *table_ranks_pos = table_ranks;
int *table_aces_pos = table_aces;
int main(void) {
srand((unsigned) time(NULL));
flop();
// Printing all elements of table_ranks
for (int i = 0; i < 3; i++)
printf("%d ", table_ranks[i]);
printf("\n");
return 0;
}
void flop() {
while (table_ranks_pos < table_ranks+3 && table_aces_pos < table_aces+3) {
rand_cards(table_ranks_pos, table_aces_pos); // Passing addresses as parameters
// Printing value of address which we used previously
printf("\nFlop %d = %p\n\n", *table_ranks_pos, table_ranks_pos);
table_ranks_pos++;
table_aces_pos++;
}
}
void rand_cards(int *rank_addr, int *ace_addr) {
int rand_rank, rand_ace;
do {
rand_rank = rand()%13+2;
rand_ace = rand()%4+1;
if (!card_exists[rand_rank][rand_ace]) {
*rank_addr = rand_rank;
printf("Rand %d = %d = %d = %p = %p = ", rand_rank, *rank_addr, *table_ranks_pos, rank_addr, table_ranks_pos);
}
} while (card_exists[rand_rank][rand_ace]);
card_exists[rand_rank][rand_ace] = true;
}
这里是输出
一个输出:
Rand 10 = 10 = 10 = 0x559220064070 = 0x559220064070 =
Flop 10 = 0x559220064070
Rand 2 = 2 = 2 = 0x559220064074 = 0x559220064074 =
Flop 2 = 0x559220064074
Rand 13 = 13 = 13 = 0x559220064078 = 0x559220064078 =
Flop 13 = 0x559220064078
10 16777218 13
另一个输出:
Rand 7 = 7 = 7 = 0x55966e907070 = 0x55966e907070 =
Flop 7 = 0x55966e907070
Rand 13 = 13 = 13 = 0x55966e907074 = 0x55966e907074 =
Flop 16777229 = 0x55966e907074
Rand 8 = 8 = 8 = 0x55966e907078 = 0x55966e907078 =
Flop 8 = 0x55966e907078
7 16777229 8
我猜你溢出了 card_exists
,而这恰好覆盖了你的 table_ranks
。
您的 card_exists
有空间容纳 12×4 元素,但您的两行 rand()
可能会生成更大的数字。随机生成有效索引的一种方法是 rand() % 12
和 rand() % 4
(回想一下第一个索引是 0),但您可能只是根据需要增加数组 card_exists
数组大小。
当我尝试将 rand()
生成的值分配给指针时,出现了一些问题。
当我为指针分配随机值时,它在 rand_cards()
函数中显示正确的值。但是当我想在其他地方查看结果时,它会显示不同的数字。无论是 flop()
函数还是 main()
。
有时它在 flop()
中也显示正确的值,但在 main()
中显示错误。
有什么问题?
bool card_exists[12][4] = {false};
int table_ranks[5] = {0};
int table_aces[5] = {0};
// We will use these addresses to store random numbers in arrays above.
int *table_ranks_pos = table_ranks;
int *table_aces_pos = table_aces;
int main(void) {
srand((unsigned) time(NULL));
flop();
// Printing all elements of table_ranks
for (int i = 0; i < 3; i++)
printf("%d ", table_ranks[i]);
printf("\n");
return 0;
}
void flop() {
while (table_ranks_pos < table_ranks+3 && table_aces_pos < table_aces+3) {
rand_cards(table_ranks_pos, table_aces_pos); // Passing addresses as parameters
// Printing value of address which we used previously
printf("\nFlop %d = %p\n\n", *table_ranks_pos, table_ranks_pos);
table_ranks_pos++;
table_aces_pos++;
}
}
void rand_cards(int *rank_addr, int *ace_addr) {
int rand_rank, rand_ace;
do {
rand_rank = rand()%13+2;
rand_ace = rand()%4+1;
if (!card_exists[rand_rank][rand_ace]) {
*rank_addr = rand_rank;
printf("Rand %d = %d = %d = %p = %p = ", rand_rank, *rank_addr, *table_ranks_pos, rank_addr, table_ranks_pos);
}
} while (card_exists[rand_rank][rand_ace]);
card_exists[rand_rank][rand_ace] = true;
}
这里是输出
一个输出:
Rand 10 = 10 = 10 = 0x559220064070 = 0x559220064070 =
Flop 10 = 0x559220064070
Rand 2 = 2 = 2 = 0x559220064074 = 0x559220064074 =
Flop 2 = 0x559220064074
Rand 13 = 13 = 13 = 0x559220064078 = 0x559220064078 =
Flop 13 = 0x559220064078
10 16777218 13
另一个输出:
Rand 7 = 7 = 7 = 0x55966e907070 = 0x55966e907070 =
Flop 7 = 0x55966e907070
Rand 13 = 13 = 13 = 0x55966e907074 = 0x55966e907074 =
Flop 16777229 = 0x55966e907074
Rand 8 = 8 = 8 = 0x55966e907078 = 0x55966e907078 =
Flop 8 = 0x55966e907078
7 16777229 8
我猜你溢出了 card_exists
,而这恰好覆盖了你的 table_ranks
。
您的 card_exists
有空间容纳 12×4 元素,但您的两行 rand()
可能会生成更大的数字。随机生成有效索引的一种方法是 rand() % 12
和 rand() % 4
(回想一下第一个索引是 0),但您可能只是根据需要增加数组 card_exists
数组大小。