【发布时间】:2019-08-11 20:05:33
【问题描述】:
这是一个家庭作业,我需要一些帮助。我似乎一切正常,但我对游戏的名称有疑问。正如你所看到的,我的名字没有正确显示在输出中,也没有在我尝试调试时出现,它有很多乱码等。有人可以解释一下是什么原因造成的,或者给我一个关于games_stack[i].name[i] 之一的例子以使其正常工作吗?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define PAUSE system("pause")
//Games Struct
typedef struct
{
char name[50]; // name of game
int mScore; // metacritic score
char rating; // ESRB rating
}GAMES;
int main(void) {
GAMES games_stack[MAX];
int numElements = 10;
int i;
//Populate array in stack
//games_stack[0].name[0] = "Bioshock Infinite";
games_stack[0].name[0] = "Bioshock Infinite";
games_stack[0].mScore = 94;
games_stack[0].rating = 'M';
games_stack[1].name[1] = "Half-life 2";
games_stack[1].mScore = 96;
games_stack[1].rating = 'M';
games_stack[2].name[2] = "Mario Kart Double Dash";
games_stack[2].mScore = 87;
games_stack[2].rating = 'E';
games_stack[3].name[3] = "Legend of Zelda: Twilight Princess";
games_stack[3].mScore = 96;
games_stack[3].rating = 'T';
games_stack[4].name[4] = "Rocket League";
games_stack[4].mScore = 86;
games_stack[4].rating = 'E';
games_stack[5].name[5] = "Counter-Strike: Global Offensive";
games_stack[5].mScore = 83;
games_stack[5].rating = 'M';
games_stack[6].name[6] = "Assassin's Creed II";
games_stack[6].mScore = 80;
games_stack[6].rating = 'M';
games_stack[7].name[7] = "Batman: Arkham Asylum";
games_stack[7].mScore = 91;
games_stack[7].rating = 'T';
games_stack[8].name[8] = "Middle-eart: Shadow of Mordor";
games_stack[8].mScore = 84;
games_stack[8].rating = 'M';
games_stack[9].name[9] = "Portal";
games_stack[9].mScore = 90;
games_stack[9].rating = 'T';
// Create an array on the heap to store the same number of populated elements from the stack array.
GAMES *games_heap = (GAMES*)malloc(numElements * sizeof(GAMES));
// if memory not allocated, exit
if (games_heap == NULL)
{
printf("\n Memory not allocated");
return EXIT_FAILURE;
}
//Copy the values from the stack array into the dynamic array.
for (i = 0; i < numElements; i++)
{
games_heap[i].name[i] = games_stack[i].name[i];
games_heap[i].mScore = games_stack[i].mScore;
games_heap[i].rating = games_stack[i].rating;
}
// print the elements from both the array
for (i = 0; i < numElements; i++)
{
printf("\n Stack : ");
printf(" Name: %c\t MetaScore: %d\t ESRB Rating: %c", games_stack[i].name[i],
games_stack[i].mScore, games_stack[i].rating);
printf("\n Heap : ");
printf(" Name: %c\t MetaScore: %d\t ESRB Rating: %c", games_heap[i].name[i],
games_heap[i].mScore, games_heap[i].rating);
}
PAUSE;
// write contents of heap to binary file
FILE *filePtr = fopen("games.bin", "w");
if (filePtr == NULL)
{
printf("Unable to open file");
return EXIT_FAILURE;
}
for (i = 0; i < numElements; i++)
{
if (i != numElements - 1)
fprintf(filePtr, "%c %d %c\n", games_heap[i].name[i],
games_heap[i].mScore, games_heap[i].rating);
else
fprintf(filePtr, "%c %d %c", games_heap[i].name[i],
games_heap[i].mScore, games_heap[i].rating);
}
fclose(filePtr);
return EXIT_SUCCESS;
}
//end of program
【问题讨论】:
-
games_heap[i].name[i] = games_stack[i].name[i];只复制一个字符。你可能想查找strcpy -
%c 打印一个字符。你想要一个字符串,所以使用 %s。
-
@John3136 谢谢你们,堆栈现在正在打印整个字符串。我还必须删除 .name 之后的 [i]。我遇到了将值从堆栈处理到堆的问题。使用 strcpy 编写此代码的正确方法是什么? 'games_heap[i].name[i] = games_stack[i].name[i];'我试着把它写成 'strcpy(games_heap[i].name) = strcpy(games_stack[i].name);'但它说它必须是一个可修改的值。我也尝试像 //'strcpy(games_heap[i].name) == strcpy(games_stack[i].name)' 这样写,但是堆栈或堆都不能正确打印。
标签: arrays c string stack heap-memory