The difference between C ++ arrays and pointers is a small example!

Source: Internet
Author: User

 

# 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;
}

 

This code runs OK, and the following code runs a compilation error!

Error c2664: "memset_1": parameter 1 cannot be converted from "unsigned char [20]" to "unsigned char *&"

// C_def.cpp: defines the entry point of the console application.
//

# 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;
}

Do you see the difference?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.