c指针的各种用法试验
#include <iostream.h>
void fun0(int* p)
{
int* a=new int[3];
*a=0;
*(a+1)=1;
*(a+2)=2;
p=a;
}
void fun1(int* &p)
{
int* a=new int[3];
*a=0;
*(a+1)=1;
*(a+2)=2;
p=a;
}
void fun2(int* p)
{
*p=0;
*(p+1)=1;
*(p+2)=2;
}
//warning:returning address of local variable or temporary
int* fun3()
{
int a[]={1,2,3};
return a;
}
//warning C4172: returning address of local variable or temporary
int*& fun4()
{
int* a=new int[3];
a[0]=0;
a[1]=1;
a[2]=2;
return a;
}
void fun5(int* p[3])
{
*p[0]=0; *(p[0]+1)=10;
*p[1]=1; *(p[1]+1)=11;
*p[2]=2; *(p[2]+1)=12;
}
//cannot convert from 'int [3]' to 'int *&
/*
int*& fun6()
{
int a[]={1,2,3};
return a;
}
*/
void main()
{
int* p=new int[3];
// int* p; //runtime error:the memory connot be written
//it is necessory to allocate memory
cout<<"test: void fun0(int* p)"<<endl;
fun0(p);
for(int i=0;i<3;i++){
cout<<*(p+i)<<endl;
}
delete[3] p;
p=new int[3];
cout<<endl<<"test: void fun1(int* &p)"<<endl;
fun1(p);
for(i=0;i<3;i++){
cout<<*(p+i)<<endl;
}
delete[3] p;
p=new int[3];
cout<<endl<<"test: void fun2(int* p)"<<endl;
cout<<" allocate memory for the parameter"<<endl;
fun2(p);
for(i=0;i<3;i++){
cout<<" "<<*(p+i)<<endl;
}
cout<<" donot allocate memory for the parameter"<<endl;
cout<<" memory connot be written"<<endl;
/*
int* pt;
fun2(pt); //runtime error:memory connot be written
for(i=0;i<3;i++){
cout<<*(pt+i)<<endl;
}
*/
delete[3] p;
p=new int[3];
cout<<endl<<"test: int* fun3()"<<endl;
cout<<" debug assertion failed"<<endl;
/*
p=fun3(); //debug assertion failed
for(i=0;i<3;i++){
cout<<*(p+i)<<endl;
}
*/
delete[3] p;
p=new int[3];
cout<&
相关文档:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include<linux/config.h>
#include<linux/module.h>
#include<linux/version.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/errno.h>
#include<linux/sche ......
C和C++语言学习总结(资料来自 <高质量C++/C 编程指南> 林锐博士 2001 年7 月24)
知识结构:
1、if,for,switch,goto
2、#define,const
3、文件拷贝的代码,动态生成内存,复合表达式,strcpy,memcpy,sizeof
4、函数参数传递,内存分配方式,内存错误表现,malloc与new区别
5、类重载、隐藏与覆盖区别,extern问题, ......
@ ECHO OFF
@ ECHO.
@ ECHO. Snlie
@ ECHO ---------------------------------------------------------------
@ ECHO NTFS格式是WinXP推荐使用的格式。转换 ......
C变量的存储方式-“静态存储”和“动态存储” 变量的存储方式可分为“静态存储”和“动态存储”两种。 静态存储变量通常是在变量定义时就分定存储单元并一直保持不变,直至整个程序结束。全局变量即属于此类存储方式。动态存储变量是在程序执行过程中,使用它时才分配存储单元,使用完毕立即 ......
将类成员函数用做C回调函数
提出问题:
回调函数是基于C编程的Windows SDK的技术,不是针对C++的,程序员可以将一个C函数直接作为回调函数,但是如果试图直接使用C++的成员函数作为回调函数将发生错误,甚至编译就不能通过。
分析原因:
普通的C++成员函数都隐含了一个传递函数 ......