链表出现 取访问权限冲突。
本帖最后由 Juniorboy 于 2020-5-28 22:36 编辑#include <stdio.h>
#include <graphics.h>
#include <windows.h>
#include <mmsystem.h>
#include <time.h>
#include <stdlib.h>
#pragma comment (lib,"winmm.lib")
constexpr auto WINDOWSLENGTH = 640;
constexpr auto WINDOWSHIGHT = 480;
IMAGE background, apple1, apple2;
//字母结构体
typedef struct Letter
{
//数据域
int x, y;//字母坐标
char ch;//字母ASCII码
//指针域;
struct Letter* pnext = NULL;
}LETTER;
typedef LETTER* Pt_letter;
//初始化函数
void GameInit()
{
srand((unsigned int)time(NULL));//随机数种子
//初始化窗口
initgraph(WINDOWSLENGTH, WINDOWSHIGHT);
//初始化背景音乐
mciSendString(L"open ./AppleGame/3.mp3", 0, 0, 0);
mciSendString(L"play ./AppleGame/3.mp3", 0, 0, 0);
//加载图片
loadimage(&background,L"./AppleGame/0.png", WINDOWSLENGTH, WINDOWSHIGHT);
loadimage(&apple1, L"./AppleGame/1.png",50,50);
loadimage(&apple2, L"./AppleGame/2.png",50,50);
settextstyle(25, 20, L"宋体");
setbkmode(0);
}
//创建链表头结点
Pt_letter Listinit()
{
Pt_letter p = (Pt_letter)malloc(sizeof(LETTER));
if (p == NULL)
{
return 0;
}
p->pnext = NULL;
return p;
}
//输出图片
void GamePoint(Pt_letter pletter)
{
//背景图片
putimage(0, 0, &background);
//苹果
putimage(0, 0, &background);
//字母
pletter = pletter->pnext;
while (pletter != NULL)
{
putimage(pletter->x-14, pletter->y-17, &apple1);
outtextxy(pletter->x, pletter->y, pletter->ch);
pletter = pletter->pnext;
}
//putimage(100, 100, &apple1);//为了测试苹果和字母的关系
//outtextxy(100+14, 100+17,L"B");
}
//添加链表结点
void AddListNode(Pt_letter pletter)
{
//为了释放方便,用尾插法
while (pletter->pnext != NULL)
{
pletter = pletter->pnext;
}
Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
pnode->x = rand() % 591 + 14;
pnode->y = 17;
pnode->ch = rand() % 27 + 'A';
pnode->pnext = pletter->pnext; //为了让这个结点的pnext指向NULL
pletter->pnext = pnode; //把头结点和下一个结点相连,从第二次开始,作用就是连接各个结点。
}
int main()
{
GameInit();
Pt_letter pletter = Listinit();
for (int i = 0; i < 10; i++)
{
AddListNode(pletter);
}
GamePoint(pletter);
system("pause");
return 0;
}
在62行提醒我取访问权限冲突,不知道错在哪,求解。 #include <stdio.h>
#include <graphics.h>
#include <windows.h>
#include <mmsystem.h>
#include <time.h>
#include <stdlib.h>
#pragma comment (lib,"winmm.lib")
constexpr auto WINDOWSLENGTH = 640;
constexpr auto WINDOWSHIGHT = 480;
IMAGE background, apple1, apple2;
//字母结构体
typedef struct Letter
{
//数据域
int x, y;//字母坐标
char ch;//字母ASCII码
//指针域;
struct Letter *pnext = NULL;
}LETTER;
typedef LETTER *Pt_letter;
//初始化函数
void GameInit()
{
srand((unsigned int)time(NULL));//随机数种子
//初始化窗口
initgraph(WINDOWSLENGTH, WINDOWSHIGHT);
//初始化背景音乐
mciSendString("open ./AppleGame/3.mp3", 0, 0, 0);
mciSendString("play ./AppleGame/3.mp3", 0, 0, 0);
//加载图片
loadimage(&background, "./AppleGame/0.png", WINDOWSLENGTH, WINDOWSHIGHT);
loadimage(&apple1, "./AppleGame/1.png", 50, 50);
loadimage(&apple2, "./AppleGame/2.png", 50, 50);
settextstyle(25, 20, "宋体");
setbkmode(0);
}
//创建链表头结点
Pt_letter Listinit()
{
Pt_letter p = (Pt_letter)malloc(sizeof(LETTER));
if(p == NULL)
{
return 0;
}
p->pnext = NULL;
return p;
}
//输出图片
void GamePoint(Pt_letter pletter)
{
//背景图片
putimage(0, 0, &background);
//苹果
putimage(0, 0, &background);
//字母
pletter = pletter->pnext;
while(pletter != NULL)
{
putimage(pletter->x - 14, pletter->y - 17, &apple1);
outtextxy(pletter->x, pletter->y, pletter->ch);
pletter = pletter->pnext;
}
//putimage(100, 100, &apple1);//为了测试苹果和字母的关系
//outtextxy(100+14, 100+17,L"B");
}
//添加链表结点
void AddListNode(Pt_letter pletter)
{
//为了释放方便,用尾插法
while(pletter->pnext != NULL)
{
pletter = pletter->pnext;
}
//Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
Pt_letter pnode = (Pt_letter)malloc(sizeof(*pnode));
pnode->x = rand() % 591 + 14;
pnode->y = 17;
pnode->ch = rand() % 27 + 'A';
pnode->pnext = pletter->pnext; //为了让这个结点的pnext指向NULL
pletter->pnext = pnode; //把头结点和下一个结点相连,从第二次开始,作用就是连接各个结点。
}
int main()
{
GameInit();
Pt_letter pletter = Listinit();
for(int i = 0; i < 10; i++)
{
AddListNode(pletter);
}
GamePoint(pletter);
system("pause");
return 0;
} 领育碧 鱼币 楼主代码第79行:Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
结点大小计算错误(应当是笔误导致的),所以后续访问也就产生了问题;
建议修改:Pt_letter pnode = (Pt_letter)malloc(sizeof(struct Letter));
//sizeof *pnode写法也可等价上述,但是含义不大直观明了;
//sizeof运算符本质是变量计算类型大小;并未涉及到变量值合法性检测。
楼上分析的很有道理 CodingCat_k 发表于 2020-5-29 08:49
楼主代码第79行:Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
结点大小计算错误(应当是笔误导 ...
我真想再给你5个鱼币,哈哈哈哈,谢谢您了 CodingCat_k 发表于 2020-5-29 08:49
楼主代码第79行:Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
结点大小计算错误(应当是笔误导 ...
我这个音乐也放不出来,您能再给看看吗 Juniorboy 发表于 2020-5-29 13:54
我这个音乐也放不出来,您能再给看看吗
https://blog.csdn.net/qq_40571533/article/details/104393805?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase
建议先康康这篇博客
Juniorboy 发表于 2020-5-29 13:54
我这个音乐也放不出来,您能再给看看吗
不知道API是不是MP3格式不支持,官方给的是wav格式;如果只播放出来了系统默认声音,可以将文件格式改一下试试。 CodingCat_k 发表于 2020-5-29 15:14
https://blog.csdn.net/qq_40571533/article/details/104393805?utm_medium=distribute.pc_relevant.none ...
发表新帖子了,快去领取鱼币吧{:10_254:}
页:
[1]