안녕하세요, 힘들어합니다. 임의의 문자열 (길이 2에서 6)을 만들고 각 'A'에서 'Z'까지 임의의 문자를 생성해야합니다. 문제는 구조체를 char *로 사용하려고한다는 것입니다. 다음 임의의 데이터를 생성하려고 할 때 동적으로 각 구조체를 할당하십시오.임의의 길이의 임의의 길이 문자열
struct TStruct
{
int ID;
float Value;
int a[4];
char *Name;
};
//create pointer to TSruct
typedef struct TStruct *ptrStruct;
//have ptrStruct point to 10 structs
ptrStruct structs[NUM_STRUCTS];
void genStruct(ptrStruct *alpha, int countID){
//declare variables
//ID counter
countID+=1;
int i;
int temp;
int tempChar;
int nameSize;
*alpha = (ptrStruct)malloc(sizeof(struct TStruct));
srand(time(0));
//put the ID in
(*alpha)->ID=countID;
//random number 0 to 999.99
(*alpha)->Value= (float)rand()/((float)(RAND_MAX)+1000)/100;
//store 4 ints 0 to 100 into array a
for (i = 0; i < 4; i++) {
//generate random number
temp = rand() % 100;
//put into the array
(*alpha)->a[i] = temp;
}
//generate a random length for the name 2 to 6
nameSize = rand() % 4 + 2;
char buffer[2];
//run a for loop based on the size of nameSize
//THIS IS THE PROBLEM CODE!!!
for (i = 0; i < nameSize; i++) {
snprintf(buffer,2, "%d",(rand() % 25)+65);
strcat((*alpha)->Name,buffer);
}
}
도움이 될만한 의견이 있습니다. 감사합니다.
당신은 질문을 잊어 버렸습니다 .... –
나는 임의로 이름을 만들 수 없다. 나는 그 문제가 무엇인지 또는 정말로 그것을 묻는 법을 모른다. –