C-指针问题 [英] C - problem with pointers

查看:66
本文介绍了C-指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我试图摆脱指针的束缚,并决定创建一个动态且通用的列表结构.现在,所有内容都可以使用int正常工作,但是当我决定使用char对其进行拍摄时,结果有点混乱了.

这是我的主要功能:

Hello,
I''m trying to get the hang of pointers and decided to create a dynamic and generic struct of lists. now everything worked just fine with ints, but when I decided to give it a shot with chars it f***ed up a little bit.

This is my main function:

void main()
{
	char *nPtr;
	Leanear_Node_Ptr list, list_h;
	initLeanearList(&list); initLeanearList(&list_h);
	puts("enter list chars (last one '-')");
	nPtr = (char*)malloc(sizeof(int));
	scanf("%c", nPtr);
	while(*nPtr!='-')
	{
		if(list)
		{
			insertAfterLeanearList(list,nPtr);
			list = list->next;
		}
		else
		{
			pushLeanearList(&list,nPtr);
			list_h = list;
		}
		nPtr = (char*)malloc(sizeof(int));
		scanf("%c", nPtr);
	}
	//list = longestString(&list_h);
	while(list)
		printf("%c ", *(char*)popLeanearList(&list));
}


这是我的课:


this is my class:

#include "LeanearList.h"

void initLeanearList(Leanear_Node_Ptr *list)
{
	*list = NULL;
}
void pushLeanearList(Leanear_Node_Ptr *list, void *value)
{
	Leanear_Node_Ptr tmp = (Leanear_Node_Ptr)malloc(sizeof(Leanear_Node));
	tmp->next = *list;
	tmp->data = value;
	*list = tmp;
}
void insertAfterLeanearList(Leanear_Node_Ptr node, void *value)
{
	Leanear_Node_Ptr tmp = (Leanear_Node_Ptr)malloc(sizeof(Leanear_Node));
	tmp->next = node->next;
	tmp->data = value;
	node->next = tmp;
}
void* popLeanearList(Leanear_Node_Ptr *list)
{
	Leanear_Node_Ptr tmp = *list;
	void* value = tmp->data;
	*list = (*list)->next;
	free(tmp);
	return value;
}
void* removeAfterLeanearList(Leanear_Node_Ptr node)
{
	Leanear_Node_Ptr tmp = node->next;
	void* value = tmp->data;
	node->next = tmp->next;
	free(tmp);
	return value;
}
void freeLeanearList(Leanear_Node_Ptr *list)
{
	while(*list)
		popLeanearList(list);
}



我并没有放置整个代码,因为此时大部分代码都不需要了,我在主要功能上缺少了一些我的演员表行,如果有人可以告诉我问题出在哪里,我会非常高兴.
非常感谢.



I didn''t put the entire code because most of it is not needed at this point, I''m missing something with my casting lines at the main function, will be more than glad if anyone could tell me what the problem is.
Thank you very much.

推荐答案

PS:请忽略"leanear"中的错字,应该是线性".
PS: Please ignore the typo in ''leanear'', should have been ''linear'' instead.


没关系,我只是忘了恢复列表的标题.可以锁定它,谢谢.
Nevermind, I simply forgot to restore the list''s header. This can be locked, thanks.


这篇关于C-指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆