c++数组和指针的区别,一个小例子!

 

#include "stdafx.h"

#include "memory.h"
void memset_1(unsigned char *&v1)
{
 unsigned char *ptmp=(unsigned char *)v1;
 memset(ptmp,100,20);

}

int _tmain(int argc, _TCHAR* argv[])
{
 
 unsigned char *pchar=new unsigned char[20];

 memset(pchar,0,20);
 
 printf("before memset%d\n",pchar[1]);
 memset_1(pchar);

 printf("after memset%d\n",pchar[1]);

 delete []pchar;

 return 0;
}

 

这段代码运行ok,而下面这段代码运行编译错误!

 error C2664: “memset_1”: 不能将参数 1 从“unsigned char [20]”转换为“unsigned char *&”

// c_def.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "memory.h"
void memset_1(unsigned char *&v1)
{
 unsigned char *ptmp=(unsigned char *)v1;
 memset(ptmp,100,20);

}

int _tmain(int argc, _TCHAR* argv[])
{
 
 
 unsigned char pchar[20];
 memset(pchar,0,20);
 
 printf("before memset%d\n",pchar[1]);
 memset_1(pchar);

 printf("after memset%d\n",pchar[1]);

 delete []pchar;

 return 0;
}


 你看出区别了吗?

你可能感兴趣的:(c++数组和指针的区别,一个小例子!)