gpt4 book ai didi

c - 使用指针数组输出通过随机片段配置的句子

转载 作者:行者123 更新时间:2023-11-30 14:49:30 25 4
gpt4 key购买 nike

我是编码新手,我们刚刚学习指针,我不确定为什么这段代码没有在整个程序中将句子的位存储到句子数组中,或者为什么它无法打印数据,

这是一项学校作业,我通常只是花几个小时尝试不同的方法,但我认为最好寻求建议,这样我就可以学会正确地做到这一点。

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#define SIZE 4

void nounPick(const const char *nPTR, char *sPTR );
void verbPick(const const char *vPTR, char *sPTR );
void prepPick(const const char *pPTR, char *sPTR );
void artiPick(const const char *aPTR, char *sPTR );

int main(void){

char *noun[SIZE];
char *verb[SIZE];
char *prep[SIZE];
char *arti[SIZE];
char *sent[SIZE];

noun[0] = "cat ";
noun[1] = "dog ";
noun[2] = "truck ";
noun[3] = "plane ";
noun[4] = "skateboard ";

verb[0] = "drove ";
verb[1] = "jumped ";
verb[2] = "ran ";
verb[3] = "walked ";
verb[4] = "flew ";

prep[0] = "to ";
prep[1] = "from ";
prep[2] = "over ";
prep[3] = "under ";
prep[4] = "on ";

arti[0] = "a ";
arti[1] = "one ";
arti[2] = "some ";
arti[3] = "any ";

const char *nPTR = noun[0];
const char *vPTR = verb[0];
const char *pPTR = prep[0];
const char *aPTR = arti[0];
char *sPTR = sent[0];

srand( time(NULL));
for(int c = 0; c< SIZE; c++)
*(sent + c) = 0;

nounPick(nPTR, sPTR);
verbPick(vPTR, sPTR);
prepPick(pPTR, sPTR);
artiPick(aPTR, sPTR);

printf("Made it through the picking\n");
printf("Your Random Sentence Is As Follows:\n");

for(int i = 0; i < SIZE; i++){
if(*(sent + i) == 0)
i = SIZE;
else
printf("%s", *(sent + i));
}

return 0;
}

void nounPick(const const char *nPTR, char *sPTR ){

int i = 0;
int l = rand() % 5;
printf("%d, is L\n", l);

do{
switch(*(sPTR + i)){
case 0:
*(sPTR + i) = *(nPTR + l);
break;

default:
i++;
break;
}
} while (i < SIZE);
}

void verbPick(const const char *vPTR, char *sPTR ){

int i = 0;
int l = rand() % 5;
printf("%d, is L\n", l);

do{
switch(*(sPTR + i)){
case 0:
*(sPTR + i) = *(vPTR + l);
break;

default:
i++;
break;
}
} while (i < SIZE);
}

void prepPick(const const char *pPTR, char *sPTR ){

int i = 0;
int l = rand() % 5;
printf("%d, is L\n", l);

do{
switch(*(sPTR + i)){
case 0:
*(sPTR + i) = *(pPTR + l);
break;

default:
i++;
break;
}
} while (i < SIZE);
printf("\n\n%s\n\n", sPTR);
}

void artiPick(const const char *aPTR, char *sPTR ){

int i = 0;
int l = rand() % 5;
printf("%d, is L\n", l);

do{
switch(*(sPTR + i)){
case 0:
*(sPTR + i) = *(aPTR + l);
break;

default:
i++;
break;
}
} while (i < SIZE);
}

最佳答案

您设置的指针(nPTRvPTR 等)并未指向您认为的位置。例如,nPTR 包含noun 第一个元素的值,即字符串“cat” 的地址。它并不像您想象的那样指向数组。其他具有相似名称的指针也是如此。

不需要那些额外的指针。只需将有问题的数组直接传递给您的函数,并将它们更改为接受 char *[] 类型的参数。

您的“pick”功能都是相同的,因此您不需要其中 4 个。您只需要向其传递不同参数的一个。

进入函数,有一个无限循环。当您为句子数组赋值时,不会增加 i,并且 break 只会从 switch 中出来,而不会从 switch 中出来。 做...而。将 switch 更改为 if,然后在执行分配时使用 break。您的随机数选择也应该按 SIZE 进行修改,即 4,而不是 5,否则您将读取数组的末尾。

此外,您正在单词数组中设置更多可用元素。如果大小为 4,则不应尝试设置 5 个值。

关于c - 使用指针数组输出通过随机片段配置的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com