简体   繁体   中英

how to use a pointer to point to another pointer in c++

suppose that I have a pointer called P with float type which is points to some values. I want to creat another pointer called Ptr to go through where the P is pointing to and return the last element. How Can I do that?

this is what I tried until now...

float *p;
float *ptr;
for(int i=0; i< sizeof p; i++){
    ptr++;
   return &ptr;
}

is that correct? sorry that my question is basic.

指向float指针的指针的声明和初始化如下:

float** ptr = &p;

Maybe what you try to do is this:

size_t sizeArr = N;
float *p = new float[sizeArr];
float **ptr = &p;
for(int i=0; i< sizeArr-1; i++){
    (*ptr)++;
}
return (**ptr)

...
delete[] p; //careful with this one, since there is another pointer to the same memory

where sizeArr is the length of the array to which p is pointing

UPDATE

Do not use malloc in C++ unless it is really necessary (as suggested in comments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM