Found 7197 Articles for C++

C++ Program to Implement Binary Heap

George John
Updated on 30-Jul-2019 22:30:25

4K+ Views

A Binary Heap is a complete binary tree which is either Min Heap or Max Heap. In a Max Binary Heap, the key at root must be maximum among all keys present in Binary Heap. This property must be recursively true for all nodes in that Binary Tree. Min Binary Heap is similar to MinHeap.Function descriptions:void BHeap::Insert(int ele): Perform insertion operation to insert element in heap.void BHeap::DeleteMin(): Perform deleteion operation to delete minimum value from heap.int BHeap::ExtractMin(): Perfrom operation to extract minimum value from heap.void BHeap::showHeap(): To show the elements of heap.void BHeap::heapifyup(int in): maintain heap structure in bottom up ... Read More

In C++ What are the differences between a pointer variable and a reference variable?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

448 Views

ReferencesWhen a variable is declared as reference, it becomes an alternative name for an existing variable.SyntaxType &newname = existing name;InitializatioType &pointer; pointer = variable name;PointersPointers are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; pointer = variable name;The main differences between references and pointers are -References are used to refer an existing variable in another name whereas pointers are used to store address of variable.References cannot have a null value assigned but pointer can.A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.A reference must be initialized on declaration ... Read More

Pointers vs References in C++

Revathi Satya Kondra
Updated on 21-Apr-2025 19:23:35

9K+ Views

In C++, both pointers and references are used to access and manipulate memory. But they behave differently. This guide explains each with simple words and examples. We understand the topic by learning how each is declared, used, and what differences exist between them. What are C++ Pointers? The pointers are used to store the address of a variable. We can change what they pointing to, and also can assign NULL to them. A pointer is similar to a signpost that contains the memory address of another variable, and you can directly access or change the variable by its address. Syntax ... Read More

Passing by pointer Vs Passing by Reference in C++

Revathi Satya Kondra
Updated on 17-Apr-2025 18:40:27

3K+ Views

In C++, If we want to pass arguments to functions we can either pass the actual value, a pointer to the value, or a reference to the value. This concept becomes crucial when we want a function to modify the original variable. So, if we pass parameter to a function either by pass by pointer or pass by reference it will produce the same result. Only difference is that References are used to refer an existing variable in another name whereas pointers are used to store address of variable. It is safe to use reference because it cannot be NULL. ... Read More

References in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

293 Views

A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.References vs PointersReferences are often confused with pointers but three major differences between references and pointers are −You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another ... Read More

Execute both if and else statements in C/C++ simultaneously

Revathi Satya Kondra
Updated on 17-Apr-2025 18:39:52

430 Views

In this article, we will see how to execute the if and else section simultaneously in a C or C++, code. This solution is a little bit tricky. When the if and else are executed one after another then it is like executing statements where if-else are not present. But here we will see if they are present how to execute them one after another. Let's go through the different ways to simulate execution of both if and else blocks. This may include using functions, macros, or separating logic completely. Using Separate Functions ... Read More

Levels of Pointers in C/C++

Revathi Satya Kondra
Updated on 11-Apr-2025 22:04:00

328 Views

In C/C++, the pointers have multiple levels, which means a pointer can point to another pointer – so the chains of indirection can go on and on. For instance, a pointer to a variable's address is stored at "*ptr" (single-level pointer) while, at "**ptr", the address of another pointer is kept (a double-level pointer), so on. This is useful in allocating memory dynamically, working with multi-dimensional arrays, and handling complicated data structures.Following is the list of different levels of pointers. Let us understand these with the help of examples: Single Level Pointer ... Read More

How does #include work in C++?

Revathi Satya Kondra
Updated on 17-Apr-2025 18:38:17

3K+ Views

In C++, the is a header file. This file includes all standard library. Sometimes in some coding contests, when we have to save time while solving, then using this header file is helpful. In software engineering approach we should reduce the minimize the include. Using this header file, it will include lots of files, sometimes that may not be required in the program. So it may increase the compile time and program size. The disadvantages of this header file is listed below. This is ... Read More

Dangling, Void, Null and Wild Pointers in C++

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

Dangling pointerDangling pointer is a pointer pointing to a memory location that has been freed (or deleted). There are different ways where Pointer acts as dangling pointerFunction CallThe pointer pointing to local variable becomes dangling when local variable is not static.int *show(void) {    int n = 76; /* ... */ return &n; }OutputOutput of this program will be garbage address.De-allocation of memoryint main() {    float *p = (float *)malloc(sizeof(float));    //dynamic memory allocation.    free(p);    //after calling free()    p becomes a dangling pointer p = NULL;    //now p no more a dangling pointer. }Variable goes ... Read More

Compare *ptr++, *++ptr and ++*ptr in C++

Revathi Satya Kondra
Updated on 17-Apr-2025 18:03:55

3K+ Views

In C++, both ptr++ and ++ptr are used to increment pointers, but they behave differently in expressions. The difference lies in when the increment happens: before or after the value is used. This is essential when working with loops, arrays, or pointer. Syntax Following is the syntax to compare ptr++ vs ++ptr in C++: ptr++: post-increment; ++ptr: pre-increment; Following is the table to compare ptr++ vs ++ptr in C++ ... Read More

Advertisements