对指针的进一步理解
来源:互联网 发布:openresty php 编辑:程序博客网 时间:2024/05/24 01:38
实例一:
void ptest(char *p)
{
p = malloc(20);
}
int main(int argc, char* argv[])
{
char *p;
ptest(p,"i'm a test prog", 20); /*错误, 函数为实参p作一份拷贝p_copy,真正申请到内存的是p_copy。
strncpy(p, "i'm a test prog", 20);
printf("%s/n", p);
return 0;
}
实例二
void ptest(char **p)
{
*p = malloc(20);
}
int main(int argc, char* argv[])
{
char *p;
ptest(&p); /*没有问题,二级指针*/
strncpy(p, "i'm a test prog", 20);
printf("%s/n", p);
return 0;
}
实例三
char* ptest(char *p)
{
p = malloc(20);
}
int main(int argc, char* argv[])
{
char *p;
p = ptest(p); /*没有问题,有返回值*/
strncpy(p, "i'm a test prog", 20);
printf("%s/n", p);
return 0;
}
实例四
#include <stdio.h>
#include <string.h>
struct rec
{
char* p;
};
void ptest(struct rec *str)
{
str->p = malloc(20);
}
int main(int argc, char* argv[])
{
struct rec *str = (struct rec *)malloc(sizeof(struct rec));
ptest(str); /*没问题, 结构体里面的变量可以在外部申请空间*/
strncpy(str->p, "i'm a test prog", 20);
printf("%s/n", str->p);
return 0;
}
- 对指针的进一步理解
- 对指针的进一步理解
- 指针的进一步理解
- 指针学习的进一步理解
- 进一步对C语言的文件理解
- 对Java instanceof的进一步理解
- 对指针的理解
- 对指针的理解
- 对指针的理解
- 对指针的理解
- 对cgal的polyhedron的off文件格式的进一步理解
- 对内存有了进一步理解!
- 对Web应用程连接应用程序的进一步理解
- poj2002~对哈希表的理解进一步加深了
- 对js函数作用域的进一步理解
- 对Q-learning和sarsa的进一步理解
- webservice的进一步理解
- LiveMesh的进一步理解
- 上传图片到本地和数据库
- 课程设计器(VC# 2008 Express开发,WinForms项目)
- GT寻址
- 推荐一位牛同学的计算机学习经验~~~~
- 我的CMD工程(5) 主程序Command.cpp的定义 请指点
- 对指针的进一步理解
- Linux-install
- 信号处理的不错的网站
- SQL SERVER – Fix: Error Msg 128 The name is not permitted in this context. Only constants, expressions, or variables allowed her
- 最近AIX的练习
- Python异常的使用
- 我的CMD工程(6) Control.cpp的定义 用于控制的命令函数 请指点
- 以命令行的方式调用Windows的动态链接库
- Caused by: org.hibernate.SessionException: Session is closed