- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7401 Articles for C++
955 Views
In C++, Null is defined as 0. Null or 0 is an integer.In case of a pointer, we can assign a pointer p as −Float* p = NULL; Float* p = 0; Float* p = nullptr;3 of them will produce the same result. null ptr is a keyword introduced in C++11 as a replacement to NULL.
8K+ Views
The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.It is common to all data types like int *, float * etc.
10K+ Views
The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.It has some limitations −1) Pointer arithmetic is not possible with void pointer due to its concrete size.2) It can’t be used as dereferenced.AlgorithmBegin Declare a of the integer datatype. Initialize a = 7. Declare b of the float datatype. Initialize b ... Read More
6K+ Views
A null pointer is a pointer which points nothing.Some uses of the null pointer are:a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.c) To check for null pointer before accessing any pointer variable. So that, we can perform error handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.AlgorithmBegin. Declare a pointer p of the integer datatype. Initialize *p= NULL. Print “The value of ... Read More
10K+ Views
A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer. Thus it is known as double pointers.AlgorithmBegin Declare v of the integer datatype. Initialize v = 76. Declare a pointer p1 of the integer datatype. Declare another double pointer p2 of the integer datatype. Initialize p1 as the pointer to variable v. Initialize p2 as the pointer to variable p1. Print “Value of v”. Print the value ... Read More
482 Views
In modern C++ [11,14,…] a vector is initialized in the following waystd::vector vec = {1,2,3};AlgorithmBegin Initialize the vector v. Using accumulate, sum up all the elements of the vector v is done. Print the result. End.Here is a simple example of sum up the elements of a vector:Example Live Demo#include #include #include using namespace std; int main() { vector v = {2,7,6,10}; cout
9K+ Views
There are different ways to copy a vector in C++.1) std::copystd:: copy is inbuilt to copy the elements from one vector to another.Syntaxstd::copy(first_iterator_o, last_iterator_o, back_inserter()): first_iteratot_0 = First iterator of first vector. last_iteratot_0 = Last iterator of first vector. back_inserter() = To insert values from back.AlgorithmBegin Declare v1 of vector type. Initialize some values into v1 vector in array pattern. Declare v2 of vector type. Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all elements of v1 to v2. Print “v1 vector elements are :”. for (int i=0;iRead More
16K+ Views
Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintain the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.Sorting a vector in descending order can be done by using std::greater ().AlgorithmBegin Declare v of vector type. Initialize some values into v in array pattern. Print “Elements before sorting”. for (const auto &i: v) print all the values of variable i. Print “Elements after ... Read More
15K+ Views
Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.AlgorithmBegin Decalre v of vector type. Initialize some values into v in array pattern. Print “Elements before sorting”. for (const auto &i: v) print all the values of variable i. Print “Elements after sorting”. Call sort(v.begin(), v.end()) function to sort all the elements of ... Read More
989 Views
This is a simple C++ program to pass a vector to a constructor.AlgorithmBegin Declare a class named as vector. Declare vec of vector type. Declare a constructor of vector class. Pass a vector object v as a parameter to the constructor. Initialize vec = v. Declare a function show() to display the values of vector. for (int i = 0; i < vec.size(); i++) print the all values ... Read More