user2758510
user2758510

Reputation: 159

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.

Upvotes: 1

Views: 1476

Answers (2)

asalic
asalic

Reputation: 949

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)

Upvotes: -1

John Dibling
John Dibling

Reputation: 101496

A pointer-to-pointer-to-float is declared and initialized as such:

float** ptr = &p;

Upvotes: 2

Related Questions