gpt4 book ai didi

c - C中内存的动态分配

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:28 25 4
gpt4 key购买 nike

<分区>

我需要使用动态分配在内存中存储五个歌曲名称,然后将它们打印到屏幕上。

我的代码有什么问题?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define MAXIM 5

void main() {
char song[30];
char *p;

p = (char*)calloc(MAXIM, sizeof(song) + 1);
for (int i = 0; i < MAXIM; i++) {
printf("Name of the song %d:\n", i);
scanf("%s", song);
strcpy(p[i], song);
};
for (int i = 0; i < MAXIM; i++) {
printf("%c\n", p[i]);
free(p[i]);
}

getch();
}

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