Server Side Programming Articles - Page 2310 of 2651

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

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

962 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

Single quotes vs. double quotes in C or C++

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

8K+ Views

When coding in C/C++, we go across single quotes (' ') and double quotes (" "). These symbols have different roles in the language. Single quotes stand for single characters, while double quotes define strings, which are groups of characters. Knowing this difference has an impact on how data gets stored compared, and handled in your code. So, let us explore various approaches regarding single vs double quotes in C/C++. Character vs String Representation Memory and Size Difference Comparison Behavior Function Call Behavior ... 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

146 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

238 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

962 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

Why does std::getline() skip input after a formatted extraction?

Farhan Muhamed
Updated on 06-Jun-2025 18:58:39

529 Views

When using std::getline after a formatted extraction (like std::cin >>), you might see an unexpected behavior, where std::getline is skipping the next input line. In this article, we will understand why this happens and how to handle it. Getline skipping input after formatted extraction The code below shows how getline() is skipping input after a formatted extraction: #include #include using namespace std; int main() { string name; string city; cin

Advertisements