C++ Articles - Page 650 of 719

Which one is better in between pass by value or pass by reference in C++?

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

565 Views

In C++ we can pass arguments into a function in different ways. These different ways are −Call by ValueCall by ReferenceCall by AddressSometimes the call by address is referred to as call by reference, but they are different in C++. Incall by address, we use pointer variables to send the exact memory address, but in call by reference we pass the reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer to get that effect.In this section, we will see what are the advantages of call by reference over call ... Read More

Find out the current working directory in C/C++

Revathi Satya Kondra
Updated on 17-Apr-2025 18:00:07

7K+ Views

To find the Current Working Directory (CWD) in C or C++ is like asking your program: "Hey, where am I right now?". Simply we can say that it is like a folder of your program which is present and used to operate in. We can use functions like getcwd() from unistd.h in C/C++ or filesystem::current_path() from C++17. Below are the list of the ways to achieve this. Using getcwd() in C/C++ Using filesystem in C++17 Using getcwd() Function in C/C++ In C/C++, we use the getcwd() function. This function gets ... Read More

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

Revathi Satya Kondra
Updated on 17-Apr-2025 18:02:08

996 Views

In C++, direct memory access is possible using pointers. However, the improper use of pointers can lead to problems such as dangling pointers, null pointers, void pointers, and wild pointers. You must have to fix these problems properly for correct code compilation and execution. Let us learn how these problems occur and how you can fix them. Dangling Pointer A dangling pointer is a variable that points to invalid or freed memory, causing errors if accessed. It is like calling a disconnected phone number. When the local variable is not static, the pointer pointing to it becomes dangling. Syntax Following ... Read More

C++ Program to Print the Kind of Rotation the AVL Tree is Undergoing When you Add an Element or Delete an Element

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

153 Views

AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodesTree rotation is an operation that changes the structure without interfering with the order of the elements on an AVL tree. It moves one node up in the tree and one node down. It is used to change the shape of the tree, and to decrease its height by moving smaller subtrees down and larger subtrees up, resulting in improved performance of many tree operations. The direction of a rotation depends on the side which ... Read More

Why use static_cast(x) instead of (int)x in C++?

Revathi Satya Kondra
Updated on 17-Apr-2025 18:01:00

2K+ Views

The (int)x is C-style typecasting, where static_cast(x) is used in C++. This static_cast() gives a compile-time checking facility, but the C-style casting does not support that. This static_cast() can be spotted anywhere inside a C++ code. And using this C++ cast, the intentions are conveyed much better. In C like cast, sometimes we can cast some type pointer to a point some other type data. Like one integer pointer can also point character type data, as they are quite similar, the only difference is character has 1-byte, integer has 4-bytes. In C++, the static_cast() is more strict than C-like casting. ... Read More

Why is it considered a bad practice to omit curly braces in C/C++?

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:54

1K+ Views

In C/C++, omitting the curly braces assumes that only the first statement is the block and this leads to quite a few issues during debugging, as code is pretty tough to read and comprehend. Curly braces help us prevent errors and confusion, which also helps with the flow of the program. Proper Use of Curly Braces In C++, we can omit the curly braces after if-else statements, or after any loop. If we do not use curly braces, then only one statement after the if-else or loop will be considered under that block. Syntax The following is the syntax: − ... Read More

Understanding cin.clear() and cin.ignore() in C++

Revathi Satya Kondra
Updated on 11-Apr-2025 17:33:40

1K+ Views

When we attempt to work with user input in C++, unwanted behavior may be caused by errors or leftover characters in the input buffer. So, in that case cin.clear() and cin.ignore() are functions that can help in dealing with this kind of problem. cin.clear() cin.ignore() cin.clear and cin.ignore() cin.clear() The cin.clear() resets the error flags on the cin stream and is used when an input operation fails (e.g., entering a non-numeric value for an integer variable). Without clearing the error flags, further input operations will not work. ... Read More

C++ Program to Print only Odd Numbered Levels of a Tree

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:01

247 Views

In C++, to print the odd-numbered levels of a binary tree, the levels are numbered from the root as Level 1. The odd-numbered levels are Level 1, Level 3, and so on. This program prints the nodes present at these odd levels. It uses level-order traversal to process the tree level by level and prints the nodes found at these levels. Algorithm to Print only Odd Numbered Levels of a Tree Following is the Algorithm to print odd numbered levels of a tree − Create a structure for tree nodes with data and pointers to left ... Read More

Differences between pass by value and pass by reference in C++

Revathi Satya Kondra
Updated on 11-Apr-2025 22:19:12

24K+ Views

In C++, there are two main ways to pass arguments to functions: pass by value and pass by reference. When you use pass by value, a copy of the variable is made, so the original variable doesn't change. With pass by reference, the function works with the original variable, so it can be changed directly. Each method is used based on what you want the function to do. We can pass arguments into a function in different ways. These different ways are − Call by Value Call by Reference ... Read More

What do single quotes do in C++ when used on multiple characters?

Farhan Muhamed
Updated on 06-Jun-2025 18:59:11

980 Views

Single quotes in C++ are used to denote characters, not strings. When you use single quotes around multiple characters or strings, C++ compiler will treat it as a multi-character literal, which will be stored as an integer value, representing the combined ASCII values of those characters. In this article, we will learn all about single quotes in C++ and how they behave when used with multiple characters. Single Quotes in C++ In C++, single quotes are used to represent a single character. For example, 'a' is a character literal representing the english alphabet 'a'. For each character, C++ assigns a ... Read More

Advertisements