《高质量程序设计(第二版)》第八章中关于指针的问题
问题一:P165 指针传递
void Allocate(char* &p, int size)
{
p=(char*)malloc(size);
}
void Test(void)
{
char* str=NULL;
Allocate(str,100);
strcpy(str, "Hello World!");
printf(str);
free(str);
}
问题一:P165 指针传递
void Allocate(char* &p, int size)
{
p=(char*)malloc(size);
}
void Test(void)
{
char* str=NULL;
Allocate(str,100);
strcpy(str, "Hello World!");
printf(str);
free(str);
}
char* &p 这样使用的含义是定义一个 char*类型的指针p,作为外部传入的char*类型指针的别名?正因为如此,及时从函数中退出,因为p就是str,所以对str 的操作也是正常的?是这样理解么?
到底怎样区分 引用的& 和 取地址的& 呢?
到底怎样区分 引用的& 和 取地址的& 呢?
问题二:P174 函数指针
typedef int (*FuncPtr)(const char*);
FuncPtr fp_1=strlen;
FuncPtr fp_2=puts;
double (*fp_3)(double)=sqrt;
typedef int (*FuncPtr)(const char*);
FuncPtr fp_1=strlen;
FuncPtr fp_2=puts;
double (*fp_3)(double)=sqrt;
第一句是否可应该怎么理解:
1、将 int* 类型的指针用FuncPtr来表示,后面的(const char*)是函数参数
2、将 int 类型用*FuncPtr来表示,后面的(const char*)是函数参数
函数名不是一个指针么,但是一个指针刚好占用4字节算是一个int数值么?函数名到底应该理解成 int 还是 int*?
1、将 int* 类型的指针用FuncPtr来表示,后面的(const char*)是函数参数
2、将 int 类型用*FuncPtr来表示,后面的(const char*)是函数参数
函数名不是一个指针么,但是一个指针刚好占用4字节算是一个int数值么?函数名到底应该理解成 int 还是 int*?