gpt4 book ai didi

c - 用指针编辑 C 中的结构

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

我想创建一个编辑函数,通过引用歌曲 vector 来接收参数。 (使用指针)

用户必须选择歌曲编号并重新输入 vector 该位置的数据。

我创建了结构,我已经收到了值并且我正在玩。但我不知道如何编辑。有人能帮我开始这部分吗?

    #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>

struct registry_of_music {
char name[50];
char artist[60];
char url[80];
};
struct registry_of_music music[9];

int main() {
int i;
printf("\nRegistry of Music\n\n\n");

for(i = 0; i <= 3;i++ ){
printf("Name of Music: ");
fflush(stdin);
fgets(music[i].name, 50, stdin);

printf("Name of Artist: ");
fflush(stdin);
fgets(music[i].artist, 60, stdin);

printf("URL of Internet: ");
fflush(stdin);
fgets(music[i].url, 80, stdin);
}

int op;
do
{
printf("1 - Play\n");
printf("2 - Edit\n");
printf("3 - Exit\n");
printf("Please enter a value:");
scanf("%d", &op);
switch(op) {
case 1: play();
break;
case 2: edit();
break;
case 3: printf("Bye\n");
break;
default: printf("Try Again\n");
}
} while (op!=3);

getch();
return(0);
}
void play(){
int i;
for(i = 0; i <= 3;i++ ){
printf("Name ...........: %s", music[i].name);
printf("Artist .....: %s", music[i].artist);
printf("URL .....: %s", music[i].url);
}
}

void edit(){}

最佳答案

如果在未初始化的结构或已初始化的结构上执行,“填充结构实例”操作是完全相同的。即使实例没有初始化,它的字段中也会有一些垃圾值。

另一方面,没有办法指定默认值,该值将显示在 fgets 的提示中,并且可用于键盘编辑,除非您使用的是更复杂的(并且不是包含在 ISO C 标准中)工具。

关于c - 用指针编辑 C 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230001/

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