What is double pointers? | Sololearn: Learn to code for FREE!

0

What is double pointers?

Why we use double pointers & purpose? How to use double pointers?

10/17/2019 4:12:35 PM

#ArizmaAltair PATHAN

5 Answers

New Answer

+3

Correct terminology is pointer to a pointer. A pointer stores the address of a variable. A pointer to a pointer stores the address of a pointer.. int x = 5; int* ptr = &x; // pointer stores the address of a variable int** pptr = &ptr; // A pointer to a pointer stores the address of another pointer. A pointer to pointer can be used to simulate a 2D array. Other common use - just like to change the variable inside a function and making that change reflected back outside of function, we pass variables through pointer, similarly to make changes to a pointer inside a function and making that change reflected back outside of function, we pass address of pointer through pointer to pointer, to the function. e.g. swap strings (C Style) void swap_str(char const** s1, char const** s2) { char const* temp = *s1; *s1 = *s2; *s2 = temp; } int main() { char const* s1 = "hello"; char const* s2 = "world"; cout << "before swap s1 = " << s1 << '\n'; cout << "before swap s2 = " << s2 << '\n'; swap_str(&s1, &s2); cout << "after swap s1 = " << s1 << '\n'; cout << "after swap s2 = " << s2 << '\n'; } output: before swap s1 = "hello" before swsp s2 = "world" after swap s1 = "world" after swap s2 = "hello" Other examples - linked list manipulation.

+2

I hope this was what you were looking for. https://www.sololearn.com/discuss/1808818/?ref=app

+1

~ swim ~ good job, very good explanation :)

+1

Aaron Eberhardt Thank you ☺🙏

0

Or does he mean a pointer to a double?