我有一個亂數生成器,所有執行都應該是相同的數字,但是數字發生了變化,甚至不是 4 個數字?
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
srand(1);
int const numero=rand() % 7000 1000;
char *caracter;
while (scanf("%s",&caracter)!=EOF){
printf("%i\n",numero);
}
return 0;
}
我使用帶有隨機名稱的 .txt,結果是這樣的
masa6144
pupu6144
ogro6144
pelad1oo
pedo0
tucan110
lloron28271
cheso111
正如你所看到的,三個名字的長度都是 4 并且相同,但是在 3 之后都變得很奇怪,那會是什么呢?
uj5u.com熱心網友回復:
&caracter是指標本身的地址。它可以是 2(AVR)、4 或 8 位元組長。使用指標作為字符陣列完全沒有意義,但您可以在那里存盤 1、3 或 7 個字符的字串。
char *caracter; 定義一個未初始化的指標。
您需要為字串分配記憶體。
int main(int argc, char const *argv[])
{
srand(1);
int const numero=rand() % 9000 1000;
char character[100];
//or char *character = malloc(100);
while (scanf("
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348688.html
上一篇:將小塊資料寫入閃存頁面
下一篇:在C程式中實作樓梯
