c指针(2)
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h> typedef struct LNode
{
char data;
struct LNode *next;
}LinkList; void CreatList(LinkList *h,char a[],int n)
//void CreatList(char a[],int n)
{
int i;
LinkList *s;
//LinkList *h;
//创建头结点
h=(LinkList *)malloc(sizeof(LinkList)); /*创建头结点*/
if(h==NULL)
{
printf("内存分配失败!\n");
exit();
}
//最后一个节点空
h->next=NULL;
//头插法
for(i=;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));/*创建新结点*/
if(s==NULL)
{
printf("内存分配失败!\n");
exit();
}
s->data=a[i];
s->next=h->next; /*将*s插在原开始结点之前,头结点之后*/
h->next=s;
}
//head=h;
}
void PrintLink(LinkList *h)
{
LinkList *p=NULL; if(h->next==NULL)
{
printf("空表!\n");
return ;
}
while (h->next)
{
printf(" %c",h->next->data);
h=h->next;
}
printf("\n");
} int main()
{
char c[]={'a','b','c','d','e','f'};
LinkList *head=NULL;
CreatList(head,c,);
PrintLink(head);
return ;
}
编译通过,运行失败。
因为CreatList函数运行完成后,h就被释放掉了,与head无关,这里是传值。
修改head为全局变量,接收h:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h> typedef struct LNode
{
char data;
struct LNode *next;
}LinkList;
LinkList *head=NULL;
//void CreatList(LinkList *h,char a[],int n)
void CreatList(char a[],int n)
{
int i;
LinkList *s;
LinkList *h;
//创建头结点
h=(LinkList *)malloc(sizeof(LinkList)); /*创建头结点*/
if(h==NULL)
{
printf("内存分配失败!\n");
exit();
}
//最后一个节点空
h->next=NULL;
//头插法
for(i=;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));/*创建新结点*/
if(s==NULL)
{
printf("内存分配失败!\n");
exit();
}
s->data=a[i];
s->next=h->next; /*将*s插在原开始结点之前,头结点之后*/
h->next=s;
}
head=h;
}
void PrintLink(LinkList *h)
{
if(h->next==NULL)
{
printf("空表!\n");
return ;
}
while (h->next)
{
printf(" %c",h->next->data);
h=h->next;
}
printf("\n");
} int main()
{
char c[]={'a','b','c','d','e','f'}; //CreatList(head,c,6);
CreatList(c,);
PrintLink(head);
return ;
}

那么,如何用局部变量来接收头指针呢?
c++可以用 CreatList(LinkList *&h,char a[],int n),那么c呢?
先到这里,有空继续写。
c指针(2)的更多相关文章
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++虚函数和函数指针一起使用
C++虚函数和函数指针一起使用,写起来有点麻烦. 下面贴出一份示例代码,可作参考.(需要支持C++11编译) #include <stdio.h> #include <list> ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- c 数组与指针的使用注意事项
数组变量和指针变量有一点小小的区别 所以把数组指针赋值给指针变量的时候千万要小心 加入把数组赋值给指针变量,指针变量只会包含数组的地址信息 而对数组的长度一无所知 相当于指针丢失了一部分信息,我们把这 ...
- Marshal.Copy将指针拷贝给数组
lpStatuss是一个UNITSTATUS*的指针类型实例,并包含SensorDust字段 //定义一个数组类型 byte[] SensorDust = new byte[30] //将指针类型拷贝 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
随机推荐
- CF1141D Colored Boots
There are n left boots and n right boots. Each boot has a color which is denoted as a lowercase Lati ...
- python练习(一)----打印九九乘法表
打印九九乘法表 ,): ,i+): print("{0} x {1} = {2} \t".format(j,i,i*j),end='') //print默认end=‘\n’, pr ...
- HDU 5282:Senior's String
Senior's String Accepts: 30 Submissions: 286 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- Go语言开发环境的搭建(Goland和VSCode)
教程首发于:微信公众号<Go编程时光>,欢迎你一起来学习 1. 下载安装 Go语言 下载地址:https://golang.google.cn/dl/ 下载完成后,直接双击 msi 文件进 ...
- Vue.js(2)- 过滤器
概念:过滤器本质上就是一个函数,可被用作一些常见的文本格式化. 过滤器只可以用在两个地方:mustache 插值表达式和 v-bind 表达式. 过滤器应该被添加在 JavaScript 表达式的尾部 ...
- python刷LeetCode:13. 罗马数字转整数
难度等级:简单 题目描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ...
- vue select框change事件
vue Select 中< :label-in-value="true" @on-change="satusSelect"> satusSelect ...
- 利用zed相机为rtabmap_ros录制rosbag包及其使用
1,录制rosbag包 rosbag record /zed_node/rgb/image_rect_color /zed_node/rgb/camera_info /zed_node/depth/d ...
- MySQL中间件介绍
360 Atlas Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化 ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...