首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

从子函数中怎么把头指与尾指针都返回到主函数呢

2013-08-01 
从子函数中怎样把头指与尾指针都返回到主函数呢?下面这个链表程序用子函数来建立链表,但是我想把头指针与

从子函数中怎样把头指与尾指针都返回到主函数呢?
下面这个链表程序用子函数来建立链表,但是我想把头指针与尾指针都返回,并且赋给不同的变量,因为我后面的程序需要用到头指针与尾指针,怎样操作呢?

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

#define SIZE 100

struct name 
{
    char firstname[SIZE];
    char lastname[SIZE];
    struct name *next;
};
  
struct name * CreateList(void);

int main(void)
{
   
struct name *head, *end;
head = CreateList();
end = //它该怎么办呢?

    return 0;
}

struct name * CreateList(void)
{
struct name *head, *move, *track, *end;
head = move = (struct name *)malloc(sizeof(struct name));
puts("Please input the information of a student:");
while(gets(move->firstname)!=NULL && move->firstname[0]!='\0')
{
gets(move->lastname);
move->next = NULL;
track = (struct name *)malloc(sizeof(struct name));
move->next = track;
move = track;
}
end = move;

return head;
}



[解决办法]
定义一个全局的指针变量数组 *a[2]
CreateList的时候,将头跟尾的指针放在a[0] a[1] 中就可以了
[解决办法]
1 返回值, 改成name结构体数据,存放 头指针和尾指针地址!
2 以out的参数形式传进函数体填充!
3 全局变量
[解决办法]
定义一个结构体,其中有两个指针成员,函数返回结构体对象即可;
或者通过形参(二级指针)也可以
[解决办法]
不要返回局部变量 数组.
b数组在函数返回时以及销毁..
正确的做法,应该是传进去一个数组地址,然后存储.

热点排行