Hello please how can i get a pointer value that is in for loop???

Dani AI

Generated

The OP asked how to "get a pointer value that is in for loop." That can mean two different things: (A) read the value pointed to during each iteration, or (B) access the pointer variable (or its final value) after the loop. and were right to mention dereferencing with *, but scope and lifetime are also important and can change what you must do. 's note about clarity applies—pick which of the two intents you need.

Declare the pointer outside the loop if you need it afterwards. Example pattern:

int *p = nullptr;
for (p = arr; p < arr + n; ++p) {
    // use *p to read the element, use p to inspect the address
}
 // p is still in scope here (points past last checked element)

To collect pointer values during a loop (for later use), store copies—do not store pointers to temporaries created inside the loop:

std::vector<int*> saved;
for (size_t i = 0; i < n; ++i) {
    if (predicate(arr[i])) saved.push_back(&arr[i]);
}
// ensure the storage owning arr outlives saved

Cautions and quick tips: printing a pointer to see its address is implementation-defined for some types—use static_cast<const void*>(p) when you need a consistent address printout, avoid storing pointers to local temporaries (dangling pointers), and prefer iterators or range-based for in modern C++ to reduce pointer errors.

Recommended Answers

All 3 Replies

Hello please how can i get a pointer value that is in for loop???

The star dereferences a pointer int x = *poionter;

[TEX]Hello please how can i get a pointer value that is in for loop???[/TEX]

[TEX]Why are you talking like this?[/TEX]

can you explain your question a bit?

you can get the value of a pointer by "*".
if

int x=6;
int *y=&x;

cout << *y << endl;    //get the value of pointer

hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.