Pass-through pointer reference----function/method parameter passed by C + + language parameter

Source: Internet
Author: User

int m_value = 1;void func (int *p) {    p = &m_value;} int main (int argc, char *argv[]) {    int n = 2;    int *PN = &n;    cout << *pn << Endl;    Func (PN);    cout << *PN <<endl;    return 0;}

Look at the output.

2
2

-------actually the above examples, see 100 times, I personally feel, also do not see the actual meaning

#include <stdio.h>
#include <malloc.h>
#include <string.h>

void Allocate (char* p,int size) {

printf ("\n%x", &p);
printf ("\n%x", p);

p= (char*) malloc (size);
}
void free (char* p) {
Free (p);
}
void Main ()
{
Char *str=null;
printf ("\n%x", &str);
printf ("\n%x", str);
Allocate (str,100);
strcpy (str, "Hello world!");
printf ("\n%s", str);
Free (str);
printf ("\nstr=%s", str);

}

------above Such a mistake case, have the practical teaching significance!!!

Http://www.cnblogs.com/li-peng/p/4116349.html

http://blog.csdn.net/whzhaochao/article/details/12891329----Classics, speak with facts

Pass-through pointer reference----function/method parameter passed by C + + language parameter

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.