Inside your swap
function, you are just changing the direction of pointers, i.e., change the objects the pointer points to (here, specifically it is the address of the objects p
and q
). the objects pointed by the pointer are not changed at all.
You can use std::swap
directly. Or code your swap function like the following:
void swap(int *r, int *s) { int temp = *r; *r = *s; *s = temp; return; }
Related Posts:
- What are the differences between a pointer variable and a reference variable in C++?
- Return array in a function
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- What is a char*?
- What exactly is nullptr?
- How to fix a “invalid operands to binary expression” error?
- What is a dangling pointer?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Pointer to incomplete class type is not allowed
- Passing a 2D array to a C++ function
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to access the contents of a vector from a pointer to the vector in C++?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- What does int & mean
- C++: Expression must have a constant value when declaring array inside function
- Sorting Linked List C++ with pointers
- Delete 2D array C++
- Is the sizeof(some pointer) always equal to four?
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- How to initialize a vector of pointers
- What is the use of intptr_t?
- C++ – Assigning null to a std::string
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- Reference to non-static member function must be called
- Why use pointers?
- What’s the difference between * and & in C?
- C++ pass an array by reference
- Function stoi not declared
- C++ Array of pointers: delete or delete []?
- Why should I use a pointer rather than the object itself?
- Function stoi not declared
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Initializing pointers in C++
- Difference between function arguments declared with & and * in C++
- Is C++ Array passed by reference or by pointer?
- Difference between the int * i and int** i
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- clearing a vector of pointers [duplicate]
- Array of Linked Lists C++
- Differences between unique_ptr and shared_ptr
- What does the explicit keyword mean?
- Expression must have class type
- Usage and Syntax of std::function
- Very basic C++ program issue – Invalid operands to binary expression
- Passing an array by reference
- How to get current time and date in C++?
- Class name does not name a type in C++
- Identifier not found error on function call
- Is there a decent wait function in C++?
- streambuf::xsgetn and state flags
- I’m getting an error “invalid use of incomplete type ‘class map’
- “Error: expression must have a pointer type” when using the “this” keyword
- This declaration has no storage class or type specifier in C++
- error: expected unqualified-id before ‘if’
- The #include
exists, but I get an error: identifier “cout” is undefined. Why? - Understanding “corrupted size vs. prev_size” glibc error
- How to convert C++ Code to C
- how to convert C# to C++
- What are helper functions in C++?
- How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?
- C++ String Variable Declaration
- How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?
- Why is my HelloWorld function not declared in this scope?
- What is the difference between include_directories and target_include_directories in CMake?
- How to add element to C++ array?
- expected constructor, destructor, or type conversion before ‘(’ token
- Passing as const and by reference – Worth it?
- expression did not evaluate to a constant- c++
- Convert char array to single int?
- https://stackoverflow.com/questions/3865946/error-generic-array-creation
- Does C++11 have C#-style properties?
- How to print a string in C++
- How to check if input is numeric in C++
- C++ Big Integer
- How do I create a random alpha-numeric string in C++?
- C++ Void Function with File Stream Error
- sorting in std::map where key is a std::string
- System not declared in scope?
- Negative RGB Values
- DEV C ++ Error: expected declaration before ‘}’ token
- “cannot be used as a function error”
- error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
- How do you clear a stringstream variable?