357mag 3 Posted 22 hours ago I'm trying to understand some basic pointer arithmetic. I guess when a pointer is incremented (like ptr++) it does not get incremented by 1. Instead the address increases by 1 multiplied by the size of the data type it is pointing to. If ptr initially held an address called 2000, then ptr++ would make it point to address 2004. What I don't understand is I read "each time ptr gets incremented, it will point to the next integer." What does that mean? The next integer? What next integer? Share this post Link to post
Anders Melander 1820 Posted 20 hours ago 1 hour ago, 357mag said: What does that mean? The next integer? What next integer? I don't know where you are reading this but the sentence is assuming that the pointer points to an element in an array of integers. So incrementing the pointer will make it point to the next integer in the array. Maybe you should stay away from pointers until you figure this out 🙂 Share this post Link to post
Roger Cigol 107 Posted 14 hours ago 8 hours ago, 357mag said: I'm trying to understand some basic pointer arithmetic. I guess when a pointer is incremented (like ptr++) it does not get incremented by 1. Instead the address increases by 1 multiplied by the size of the data type it is pointing to. This is correct. Share this post Link to post
Der schöne Günther 316 Posted 12 hours ago Make sure the type of your pointer is correct when doing stunts like pointer arithmetic. For example, PInteger when dealing with Integer. Short example: program Project1; var int: Integer; ints: TArray<Integer>; p_int: PInteger; begin int := 42; p_int := Addr(int); WriteLn('p_int points to ', p_int^); // 42 ints := [100, 101, 102]; p_int := Addr(ints[1]); WriteLn('p_int points to ', p_int^); // 101 Inc(p_int); WriteLn('p_int points to ', p_int^); // 102 Inc(p_int); WriteLn('p_int points to ', p_int^); // (some random number) end. Share this post Link to post
Roger Cigol 107 Posted 12 hours ago Just now, Der schöne Günther said: Make sure the type of your pointer is correct when doing stunts like pointer arithmetic. For example, PInteger when dealing with Integer This is good advice (more accurately: this is essential). But the example you give is in Delphi / Pascal rather than C++ so perhaps not entirely suited for the original question posed by a relative newcomer to C++. Share this post Link to post
357mag 3 Posted 11 hours ago (edited) Excuse me sir for asking a question. Jump in a lake. Edited 11 hours ago by 357mag Share this post Link to post
Lars Fosdal 1796 Posted 11 hours ago 9 minutes ago, 357mag said: Excuse me sir for asking a question. Jump in a lake. This attitude is inappropriate. Access to these forums is a privilege, not a right. Do not abuse it. 1 Share this post Link to post
Roger Cigol 107 Posted 8 hours ago 3 hours ago, 357mag said: Excuse me sir for asking a question. Jump in a lake. Whilst agreeing totally with @Lars FosdalI am puzzled by your reaction. It is clear that every posting you received was aimed at helping / educating you. I can't see any sign of anyone being patronising. I like to think that all the many experienced developers that look at Delphi-Praxis are understanding of less experienced folk who are trying to learn. Please continue to ask questions. Be patient if you don't always get a simple answer and check that you have expressed your question in as clear as way as you can. Share this post Link to post
Remy Lebeau 1451 Posted 4 hours ago 7 hours ago, Der schöne Günther said: Short example: In C++, that example would look like this: #include <iostream> int main() { int value = 42; int *p_int = &value; std::cout << "p_int points to " << *p_int << std::endl; // 42 int arr[] = {100, 101, 102}; p_int = &arr[1]; std::cout << "p_int points to " << *p_int << std::endl; // 101 ++p_int; std::cout << "p_int points to " << *p_int << std::endl; // 102 ++p_int; std::cout << "p_int points to " << *p_int << std::endl; // (OUT OF BOUNDS - UNDEFINED BEHAVIOR!) } Share this post Link to post