C 随机数生成器返回地址
C Random Number generator returning address
我的部分代码是随机生成一个包含 0 到 999 之间的数字的数组。我使用随机数生成器来执行此操作。然而,每次它这样做时,returns 数组、intArray[8]
到 intArray[12]
都有我认为的地址。我怎样才能解决这个问题。我已经在 randomArr 函数中打印了数组的每个部分,它们都很好。
代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
#include "sorting.h"
void initRandom()
{
srand(time(NULL));
}
int random(int low, int high)
{
int number = -1;
if(low <= high)
{
number = (rand() % (high-low+1)) + low;
}
return number;
}
void randomArr(int* numArray, int intNum){
int j = 0;
initRandom();
for (j = 0; j < intNum; j++){
numArray[j] = random(0, 999);
}
}
int main(int argc, char* argv[]){
int* intArray = (int*)malloc(sizeof(double));
if (argc < 2){
printf("Error! Not enough arguments!");
} else if (argc > 3){
printf("Error! Too many arguments!");
} else{
int intNum, i;
if (argc == 2){
printf("Please specify number of integers: ");
scanf("%d", &intNum);
randomArr(intArray, intNum);
}
for (i = 0; i < intNum; i++){
printf("Num %d: %d\n", i+1, intArray[i]);
}
}
free(intArray);
return 0;
}
一个包含 20 个整数的输出示例是:
./sorting 1
Please specify number of integers: 20
Num 1: 97
Num 2: 971
Num 3: 579
Num 4: 223
Num 5: 905
Num 6: 883
Num 7: 711
Num 8: 757
Num 9: 544044366
Num 10: 891304505
Num 11: 858863928
Num 12: 842609462
Num 13: 2610
Num 14: 283
Num 15: 568
Num 16: 319
Num 17: 426
Num 18: 443
Num 19: 848
Num 20: 572
int* intArray = (int*)malloc(sizeof(double));
正在分配单个 sizeof(double)
的内存缓冲区(通常为 4 或 8 个字节)。我将其更改为 int* intArray = (int*)malloc(sizeof(int) * intNum);
以 malloc 您要存储的整数数量。为此,我将 malloc
向下移动到 if
语句中,并将空闲向上移动到相同的 if 语句中。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
void initRandom()
{
srand(time(NULL));
}
int random(int low, int high)
{
int number = -1;
if (low <= high)
{
number = (rand() % (high - low + 1)) + low;
}
return number;
}
void randomArr(int* numArray, int intNum) {
int j = 0;
initRandom();
for (j = 0; j < intNum; j++) {
numArray[j] = random(0, 999);
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Error! Not enough arguments!");
}
else if (argc > 3) {
printf("Error! Too many arguments!");
}
else {
int intNum, i;
if (argc == 2) {
printf("Please specify number of integers: ");
scanf("%d", &intNum);
int* intArray = (int*)malloc(sizeof(int) * intNum);
randomArr(intArray, intNum);
for (i = 0; i < intNum; i++) {
printf("Num %d: %d\n", i + 1, intArray[i]);
}
free(intArray);
}
}
return 0;
}
输出:
Please specify number of integers: 20
Num 1: 218
Num 2: 837
Num 3: 797
Num 4: 190
Num 5: 555
Num 6: 644
Num 7: 158
Num 8: 673
Num 9: 837
Num 10: 963
Num 11: 936
Num 12: 101
Num 13: 721
Num 14: 257
Num 15: 541
Num 16: 941
Num 17: 285
Num 18: 958
Num 19: 591
Num 20: 971
我的部分代码是随机生成一个包含 0 到 999 之间的数字的数组。我使用随机数生成器来执行此操作。然而,每次它这样做时,returns 数组、intArray[8]
到 intArray[12]
都有我认为的地址。我怎样才能解决这个问题。我已经在 randomArr 函数中打印了数组的每个部分,它们都很好。
代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
#include "sorting.h"
void initRandom()
{
srand(time(NULL));
}
int random(int low, int high)
{
int number = -1;
if(low <= high)
{
number = (rand() % (high-low+1)) + low;
}
return number;
}
void randomArr(int* numArray, int intNum){
int j = 0;
initRandom();
for (j = 0; j < intNum; j++){
numArray[j] = random(0, 999);
}
}
int main(int argc, char* argv[]){
int* intArray = (int*)malloc(sizeof(double));
if (argc < 2){
printf("Error! Not enough arguments!");
} else if (argc > 3){
printf("Error! Too many arguments!");
} else{
int intNum, i;
if (argc == 2){
printf("Please specify number of integers: ");
scanf("%d", &intNum);
randomArr(intArray, intNum);
}
for (i = 0; i < intNum; i++){
printf("Num %d: %d\n", i+1, intArray[i]);
}
}
free(intArray);
return 0;
}
一个包含 20 个整数的输出示例是:
./sorting 1
Please specify number of integers: 20
Num 1: 97
Num 2: 971
Num 3: 579
Num 4: 223
Num 5: 905
Num 6: 883
Num 7: 711
Num 8: 757
Num 9: 544044366
Num 10: 891304505
Num 11: 858863928
Num 12: 842609462
Num 13: 2610
Num 14: 283
Num 15: 568
Num 16: 319
Num 17: 426
Num 18: 443
Num 19: 848
Num 20: 572
int* intArray = (int*)malloc(sizeof(double));
正在分配单个 sizeof(double)
的内存缓冲区(通常为 4 或 8 个字节)。我将其更改为 int* intArray = (int*)malloc(sizeof(int) * intNum);
以 malloc 您要存储的整数数量。为此,我将 malloc
向下移动到 if
语句中,并将空闲向上移动到相同的 if 语句中。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
void initRandom()
{
srand(time(NULL));
}
int random(int low, int high)
{
int number = -1;
if (low <= high)
{
number = (rand() % (high - low + 1)) + low;
}
return number;
}
void randomArr(int* numArray, int intNum) {
int j = 0;
initRandom();
for (j = 0; j < intNum; j++) {
numArray[j] = random(0, 999);
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Error! Not enough arguments!");
}
else if (argc > 3) {
printf("Error! Too many arguments!");
}
else {
int intNum, i;
if (argc == 2) {
printf("Please specify number of integers: ");
scanf("%d", &intNum);
int* intArray = (int*)malloc(sizeof(int) * intNum);
randomArr(intArray, intNum);
for (i = 0; i < intNum; i++) {
printf("Num %d: %d\n", i + 1, intArray[i]);
}
free(intArray);
}
}
return 0;
}
输出:
Please specify number of integers: 20
Num 1: 218
Num 2: 837
Num 3: 797
Num 4: 190
Num 5: 555
Num 6: 644
Num 7: 158
Num 8: 673
Num 9: 837
Num 10: 963
Num 11: 936
Num 12: 101
Num 13: 721
Num 14: 257
Num 15: 541
Num 16: 941
Num 17: 285
Num 18: 958
Num 19: 591
Num 20: 971