C程序中的错误 - 预期“char *”但参数是char类型 [英] Error in C program- expected "char *" but argument is of type char

查看:191
本文介绍了C程序中的错误 - 预期“char *”但参数是char类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在编写这个程序,我是一个新手所以请原谅我的意大利面条代码,当我试图执行这个代码时我遇到了一个错误,我只是想弄清楚原因。


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

struct listNode
{
char data;
struct listNode * nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode * ListNodePtr;


/ * prototypes * /
char insert(ListNodePtr * head,char value);
void printList(ListNodePtr currentPtr);
int isEmpty(ListNodePtr sPtr);


void main()
{

ListNodePtr startPtr = NULL;
char item;

printf(输入字符\ n);
insert(& startPtr,item);
printList(startPtr);

}


char insert(ListNodePtr * head,char value)
{

ListNodePtr newptr;
ListNodePtr currentptr;
ListNodePtr previousptr;


newptr = malloc(sizeof(ListNode));

if(newptr!= NULL)
{
strcpy(newptr-> data,& value);
newptr-> nextPtr = NULL;
previousptr = NULL;
currentptr = * head;


if(previousptr == NULL)
{
newptr-> nextPtr = * head;
* head = newptr;
} / * end if * /

else / *在previousPtr和currentPtr之间插入新节点* /
{
previousptr-> nextPtr = newptr;
newptr-> nextPtr = currentptr;
} / * end else * /
/ * end if * /
}
else
{
printf(%s未插入。无记忆available.\\\
,value);
}



}



void printList(ListNodePtr currentPtr)
{

if(isEmpty(currentPtr))
{
puts(List is empty.\);
}
其他
{
puts(列表是:);

while(currentPtr!= NULL)
{
printf(%s - >,currentPtr-> data);
currentPtr = currentPtr - > nextPtr;
}
puts(NULL\\\
);
}
}

int isEmpty(ListNodePtr sPtr)
{
return sPtr == NULL;
}







我得到的错误就是这个;







[ ^ ]



请帮帮我。伙计们'



我尝试了什么:



错误似乎来自这部分代码;

 strcpy(newptr-> data,& value); 
newptr-> nextPtr = NULL;
previousptr = NULL;
currentptr = * head;





就像我说的,我是一个绝对的新手,除了google之外,我还没有尝试过多少但是仍然无法找到解。在此先感谢您的帮助!

解决方案

查看代码:

 struct listNode 
{
char数据;
struct listNode * nextPtr;
};
...
strcpy(newptr-> data,& value);

data 定义为单个字符:'A'到'Z','0'到'9','。',','等等 - 但是你试图将它传递给 strcpy 将一串字符(即由空字符终止的char值数组)从一个位置复制到另一个位置。



猜测, data 应该是 char * (即指向 char 的指针)或者 char [] (即一个字符数组)而不是一个 char ,但这意味着你需要检查并修复你的其余代码。



我建议你做的是坐下来你的导师给你的任务,并认真思考在开始进行任何更改之前的几分钟 - 然后围绕您需要存储的内容重新设计节点。我还建议您首先编写一个小的代码 - 可能是创建一个单独的节点位 - 并在开始添加更多代码之前测试它以确保它正常工作。这样,解决方案的每一步都建立在你知道正在运行的代码的基础之上,这使得调试问题变得更加容易!


要将字符串用于字符串,您需要提供该缓冲区和有两个选项,简单的固定大小或更动态的大小方法。但是要保证,你不要复制比缓冲区更多的字符



  struct  listNode 
{
char 数据[ 20 ];
struct listNode * nextPtr;
};
...
strncpy(newptr-> data,& value, sizeof (newptr-> data)); // copy max count

动态大小:

< pre lang =c ++> struct listNode
{
char * data;
struct listNode * nextPtr;
};
...
newptr-> data = strdup(,& value); // alloc data

但是你必须始终在某处释放数据。所以它更受到质疑。


So Ive been writing this program, and Im a novice so please excuse my spaghetti code, I run into an error when Im trying to execute this code and I just cant figure out why.

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

struct listNode
{
	char data; 
	struct listNode *nextPtr; 
};

typedef struct listNode ListNode; 
typedef ListNode *ListNodePtr;


/*prototypes*/
char insert( ListNodePtr *head, char value);
void printList( ListNodePtr currentPtr );
int isEmpty( ListNodePtr sPtr);


void main()
{

ListNodePtr startPtr = NULL;
char item;

     printf("Enter the character\n");
			insert(&startPtr,item);
			printList(startPtr);

}
 

char insert( ListNodePtr *head, char value)
{

ListNodePtr newptr;
ListNodePtr currentptr;
ListNodePtr previousptr;


newptr = malloc( sizeof( ListNode ));

if(newptr !=NULL)
{
		strcpy(newptr->data,&value);
		newptr->nextPtr= NULL;
		previousptr=NULL;
		currentptr = *head;


 if ( previousptr == NULL )
		{
			newptr->nextPtr = *head;
			*head = newptr;
		} /* end if*/
		
		else   /* insert new node between previousPtr and currentPtr*/
		{
			previousptr->nextPtr = newptr;
			newptr->nextPtr = currentptr;
		} /* end else*/
	 /* end if*/
}
	else
	{
		printf( "%s not inserted. No memory available.\n", value );
	}
	


}



void printList( ListNodePtr currentPtr )
{

	if ( isEmpty( currentPtr ))
	{
		puts( "List is empty.\n" );
	} 
	else
	{
		puts( "The list is: " );
		
		while ( currentPtr != NULL )
		{
			printf( "%s --> ", currentPtr->data );
			currentPtr = currentPtr ->nextPtr;
		} 
		puts( "NULL\n" );
	} 
}

int isEmpty( ListNodePtr sPtr )
{
	return sPtr == NULL;
}




The error im getting is this;



[^]

Please help me.out guys'

What I have tried:

The error seems to be from this part of code;

strcpy(newptr->data,&value);
newptr->nextPtr= NULL;
previousptr=NULL;
currentptr = *head;



And like I said, Im an absolute novice and I havent tried much other than google this but still cant find the solution. Thanks in advance for your help!

解决方案

Look at the code:

struct listNode
{
	char data; 
	struct listNode *nextPtr; 
};
...
strcpy(newptr->data,&value);

data is defined as a single character: 'A' to 'Z', '0' to '9', '.', ',', and so on - but you are trying to pass it to strcpy which copies a string of characters (i.e. an array of char values terminated by a null character) from one location to another.

At a guess, data should be a char* (i.e. a pointer to a char) or a char[] (i.e. an array of characters) instead of a single char, but that would mean you need to check and fix the rest of your code as well.

What I'd suggest you do is sit down with the task your tutor gave you and think good and hard for a few minutes before you start doing any changes - and then redesign your node around what you need to store. I'd also suggest that you code a minor bit first - the "create a single node" bit perhaps - and test that to make sure it's working before you start adding more code. That way, each step of the solution builds on code you know is working, which makes debugging problems a whole lot easier!


To use the struct for a string you need to provide that buffe and have two options the simple fixed size or the more dynamic size approach. But assure, that you dont copy more chars than the buffer has

struct listNode
{
	char data[20]; 
	struct listNode *nextPtr; 
};
...
strncpy(newptr->data,&value, sizeof(newptr->data));//copy max count

With dynamic size:

struct listNode
{
	char *data; 
	struct listNode *nextPtr; 
};
...
newptr->data = strdup(,&value);//alloc data

But you must always free the data somewhere. So it is more complaicated.


这篇关于C程序中的错误 - 预期“char *”但参数是char类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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