c++指针作为形参常见问题--1

【错误案例1】 错误原因,函数createCharLink中定义的指针p虽然是在堆上申请的内存空间,但是函数createCharLink没有返回值,所以main函数中的指针p未定义,因此编译未通过。解决办法是,把指针p的声明放在main函数体外,紧跟结构体charlink的定义之后,并且仅仅在createCharLink函数中定义它。或者,把createCharLink更改为返回值为CharLink *类型的函数。

#include<iostream>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

typedef struct charlink
{
	char ch;
	struct charlink *next;
}CharLink;

//CharLink *p; //在此处,取消注释这一句

void createCharLink(const char *str1)
{
	CharLink *p = (CharLink*)malloc(sizeof(CharLink)); //删除或者注释掉这一句
    // p = (CharLink*)malloc(sizeof(CharLink)); //修改办法,取消注释这一句
	p->next=0;
    while(*str1)
	{
        CharLink *t = (CharLink*)malloc(sizeof(CharLink));
	    t->ch = *str1;
	    t->next = NULL;
	    t->next = p->next;
	    p->next = t;
	    ++str1;
	}

}

int main()
{

	const char *str1 = "235";
	CharLink *p;//修改办法,删除或者注释这一句
    createCharLink(str1);
    while(p->next)
	{
		cout << p->next->ch << endl;
		p = p->next;
	}
	return 0;
}

【错误案例2】错误原因,虽然p指针是外部传入的参数,但p指针的定义是在函数内部定义的,也就是说传入函数的p指针是未定义的。解决办法是,确保p指针的定义在传入函数之前已经完成。

#include<iostream>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

typedef struct charlink
{
	char ch;
	struct charlink *next;
}CharLink;

void createCharLink(CharLink *p, const char *str1)
{

	p = (CharLink*)malloc(sizeof(CharLink)); //删除或者注释掉这一句
	p->next=0;
    while(*str1)
	{
        CharLink *t = (CharLink*)malloc(sizeof(CharLink));
	    t->ch = *str1;
	    t->next = NULL;
	    t->next = p->next;
	    p->next = t;
	    ++str1;
	}

}

int main()
{

	const char *str1 = "235";
	CharLink *p; //删除或者注释掉这一句
	//CharLink *p = (CharLink*)malloc(sizeof(CharLink)); //取消注释这一句
    createCharLink(p,str1);
    while(p->next)
	{
		cout << p->next->ch << endl;
		p = p->next;
	}
	system("pause");
	return 0;
}

【正确案例1】采用无返回值的子函数

#include<iostream>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

typedef struct charlink
{
	char ch;
	struct charlink *next;
}CharLink;

void createCharLink(CharLink *p, const char *str1)
{
	p->next=0;
    while(*str1)
	{
        CharLink *t = (CharLink*)malloc(sizeof(CharLink));
	    t->ch = *str1;
	    t->next = NULL;
	    t->next = p->next;
	    p->next = t;
	    ++str1;
	}

}

int main()
{

	const char *str1 = "235";
	CharLink *p = (CharLink*)malloc(sizeof(CharLink));
    createCharLink(p,str1);
    while(p->next)
	{
		cout << p->next->ch << endl;
		p = p->next;
	}
	system("pause");
	return 0;
}

【正确案例2】采用有返回值的子函数

#include<iostream>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

typedef struct charlink
{
	char ch;
	struct charlink *next;
}CharLink;

CharLink* createCharLink(const char *str1)
{
	CharLink *p = (CharLink*)malloc(sizeof(CharLink));
	p->next=0;
    while(*str1)
	{
        CharLink *t = (CharLink*)malloc(sizeof(CharLink));
	    t->ch = *str1;
	    t->next = NULL;
	    t->next = p->next;
	    p->next = t;
	    ++str1;
	}
    return p;
}

int main()
{

	const char *str1 = "235";
    CharLink *p = createCharLink(str1);
    while(p->next)
	{
		cout << p->next->ch << endl;
		p = p->next;
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u012751110/article/details/103794647