1

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.

user2758510
  • 159
  • 5
  • 20
  • Do you mean "pointer to another pointer" or do you mean "pointer that points to the *same data* as another pointer"? – benjymous Oct 31 '13 at 12:50
  • 1
    sizeof p will give you the size of your pointer on your machine. If you want an array, you will need the length too, or a mark for the end of the array (and iterate until you encounter that mark). You can't measure the size of an array using it's header and sizeof. – asalic Oct 31 '13 at 12:50
  • 2
    Just stop. There is not a single meaningful line of code here. Please stop using pointers. It's not necessary in C++ for you right now. Use std::string, std::vector and such containers. – stefan Oct 31 '13 at 12:50
  • 1
    If you want to learn C++, web tutorials and asking basic questions on SO is not the way to go. Pick up one of our [recommended texts](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – John Dibling Oct 31 '13 at 12:50
  • if *p points to an array/vector just iterate that – drtf Oct 31 '13 at 12:50
  • @benjymous: pointer to another pointer I mean. – user2758510 Oct 31 '13 at 12:52

2 Answers2

2

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

float** ptr = &p;
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • I was really confused for a second by your use of "&" in your text as you don't talk about the address-operator there :D – stefan Oct 31 '13 at 13:00
-1

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)

asalic
  • 949
  • 4
  • 10
  • what will it return at the end? – user2758510 Oct 31 '13 at 12:58
  • @user2758510 At the end, ptr will point to the last element in the array (but be careful because it is pointer to pointer...) – asalic Oct 31 '13 at 12:59
  • I want it to return the last element. how to do that? – user2758510 Oct 31 '13 at 13:01
  • 1
    -1: Teaching an obvious newbie to use `malloc` in C++ is just poor form. – John Dibling Oct 31 '13 at 13:02
  • @john dibling: what do you mean? a newbie should not try to learn from others? – user2758510 Oct 31 '13 at 13:04
  • @user2758510 A newbie shouldn't deal with pointers, a newbie shouldn't dealing with raw arrays, a newbie shouldn't deal with dynamic memory allocation, a newbie shouldn't know about C-style allocation. Shall I go on? – stefan Oct 31 '13 at 13:05
  • @stefan Okay, but he is dealing and on top of that with pointer to pointer. Let him learn C too. I can't see the problem with this. With this attitude, go and use Java. – asalic Oct 31 '13 at 13:07
  • @asalic He's using C++, why on earth should he be infected with C? Java is a completely different thing. Why do you think does the inventor of C++ itself rarely speak of raw pointers and mentions them late in his own introductory books? Because they are evil and should be handled with care. By experts. References and standard containers are enough to begin with. – stefan Oct 31 '13 at 13:10
  • He's not learning C; he's learning C++. You should almost never use `malloc` in C++. By extension of your logic we should be teaching him assembly and manipulating bits with a magnetized needle as well, or else we might as well stick with Java. – John Dibling Oct 31 '13 at 13:12
  • ok stefan please as an expert in c++ and whole programming world, let me know how do you solve my question in c++. – user2758510 Oct 31 '13 at 13:12
  • @stefan infected with C? anyway... So if the inventor of C++ rarely speaks about pointers, should we all just ignore them? Not a valid argument. Pointers are not evil, you have to pay more attention to what you're doing and they can be handled by newbies too... After all, C is the main language used in many Universities, especially for algos... – asalic Oct 31 '13 at 13:13
  • @user2758510 I don't know why you want a pointer to a pointer. Do you want to modify an array element perhaps? I need more context to answer this. – stefan Oct 31 '13 at 13:13
  • @JohnDibling Yes I agree that using malloc in C++ is not recomendable. But I can't agree with stefan – asalic Oct 31 '13 at 13:15
  • I am working with a huge code! I need to extract some information from that. what I know is that: there is a pointer which is pointing to some values(I do not know how much you are familiar with image processing--it is pointing to frames) now I want to get access to the last frame by using a pointer. this is what I want. – user2758510 Oct 31 '13 at 13:16
  • I have yet to see a newbie dealing with pointers correctly. Some academics also use Fortran, some use C, some use Matlab, some use C++. It's dependent on the thing you want to achieve. Using pointers in C++ is only rarely necessary, so especially beginners should avoid it. – stefan Oct 31 '13 at 13:17
  • @user2758510 That depends on how the frames are stored. I assume you have a float array for that? You need to know the size. The pointer to the last float would then be `p + (size - 1)` with `p` the pointer to the 0^th element of the array and `size` entries. – stefan Oct 31 '13 at 13:20
  • @user2758510 I updated the code using new. I didn't consider the title correctly so I fixed it. – asalic Oct 31 '13 at 13:21
  • @user2758510: re: "what do you mean? a newbie should not try to learn from others? " No, of course that's not what I mean. After all, I answered the question I thought you were asking. What I meant was that a newbie should not be taught the Wrong Way to do things in such a way as to give the impression they are the Right Way to do things. And that's what this answer did, in my opinion. You were advised to use `malloc`, which is almost always wrong in C++, but not told anything about the pitfalls of using `malloc`. Hence, without further info, you may think `malloc` is the Right Way. – John Dibling Oct 31 '13 at 13:22
  • @stefan: what will it be if I have a float vector instead? – user2758510 Oct 31 '13 at 13:26
  • @JohnDibling You're welcome. Thanks. It was my mistake and I take full credit for it :) – asalic Oct 31 '13 at 13:27
  • @user2758510 `yourVector.back()` is the last element of the vector (if there is one!). – stefan Oct 31 '13 at 13:27
  • I returned to the first :( – user2758510 Oct 31 '13 at 13:28
  • Well you can use the address operator as Johns answer shows you: `&yourVector.back()` is the pointer to the last element. – stefan Oct 31 '13 at 13:28
  • all of your answers are correct and appreciable. but the problem is that I am using a huge code that a part of that should be completed by me. – user2758510 Oct 31 '13 at 13:31
  • @user2758510 Please consider this post: http://stackoverflow.com/questions/641864/returning-a-pointer-to-a-vector-element-in-c when you are getting the addresses of elements in a vector especially the second answer... – asalic Oct 31 '13 at 13:31
  • @user2758510 Well, if you have to use a pointer which you receive from ... somewhere... use what I showed you – asalic Oct 31 '13 at 13:36