Programming Articles - Page 2570 of 3366

What is difference between a pointer and reference parameter in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

6K+ Views

PointersPointer variables are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer=variable name;ReferencesWhen a parameter is declared as reference, it becomes an alternative name for an existing parameter.SyntaxType &newname=existing name;InitializationType &pointer; Pointer=variable name;The main differences between pointers and reference parameters 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 by pass by reference.A reference must be initialized on declaration while it is not ... Read More

Scope Resolution Operator vs this pointer in C++

Tapas Kumar Ghosh
Updated on 16-Jun-2025 17:00:00

503 Views

Both scope resolution operator and 'this' pointer are used for different purposes in the C++ programs. The scope resolution operator is used to access static or class members whereas this pointer is used to access object members when there is a local variable with the same name. Scope Resolution Operator in C++ The concept of scope resolution operator (::) is used in Object-Oriented Programming (OOPs) and namespace standard that can used to access something belongs to a specific scope, such as a class or namespace. Syntax It's syntax is as follows: :: Example In this program, we use ... Read More

How to create an immutable class with mutable object references in Java?

raja
Updated on 06-Feb-2020 11:48:17

12K+ Views

Immutable objects are those objects whose states cannot be changed once initialized. Sometimes it is necessary to make an immutable class as per the requirement. For example, All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java. String class is also an immutable class.To create a custom immutable class we have to do the following stepsDeclare the class as final so it can’t be extended.Make all fields private so that direct access is not allowed.Do not provide setter methods (methods that modify fields) for variables, so that it can not be set.Make all mutable ... Read More

RAII and smart pointers in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

2K+ Views

RAII in C++RAII (Resource Acquisition Is Initialization) is C++ technique which control the life cycle of resource. It is a class variant and is tied to object life time.It encapsulates several resources into class where resource allocation is done by constructor during object creation and resource deallocation is done by destructor during object destruction.Resource is guaranteed to be held till the object is alive.Examplevoid file_write {    Static mutex m; //mutex to protect file access    lock_guard lock(m); //lock mutex before accessing file    ofstream file("a.txt");    if (!file.is_open()) //if file is not open    throw runtime_error("unable to open file"); ... Read More

Pointers, smart pointers and shared pointers in C++

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

6K+ Views

PointersPointers are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer = variable name;Functionspointers are used to store address of variable.pointers can have a null value assigned.pointer can be referenced by pass by reference.a pointer has its own memory address and size on the stack.Example Live Demo#include using namespace std; int main() {    // A normal integer variable    int a = 7;    // A pointer variable that holds address of a.    int *p = &a;    // Value stored is value of variable "a"    cout

Is it safe to delete a void pointer in C/C++?

Tapas Kumar Ghosh
Updated on 17-Jun-2025 16:21:04

1K+ Views

The void pointer is a pointer which is not associate with any data types. It points to some data location in storage means points to the address of variables. It is also called general purpose pointer. Is It Safe to Delete a Void Pointer in C/C++? It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it's destroying, and it is impossible to do that if it doesn't know the type. C Example of Void Pointer In this example, a void pointer (void *p) can store the address of ... Read More

How to declaring pointer variables in C/C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

499 Views

A pointer is used to store the address of the variables. To declare pointer variables in C/C++, an asterisk (*) used before its name.Declaration*pointer_nameIn CExample Live Demo#include int main() {    // A normal integer variable    int a = 7;    // A pointer variable that holds address of a.    int *p = &a;    // Value stored is value of variable "a"    printf("Value of Variable : %d", *p);    //it will print the address of the variable "a"    printf("Address of Variable : %p", p);    // reassign the value.    *p = 6;    printf("Value ... Read More

How to compare pointers in C/C++?

Tapas Kumar Ghosh
Updated on 17-Jun-2025 14:57:16

8K+ Views

The pointers can be directly compared using relational operators. In this article, we will learn about the comparisons of the pointers with the help of examples. Pointers Comparison in C and C++ In C/C++, we can directly compare the pointers using relational operators (==, !=, , =). These operators are used to compare two variables, values, or pointers. It returns a Boolean true value when the comparison is correct otherwise, it is false. The core syntaxes of C and C++ pointers are similar such as declaration, dereferencing, and pointer arithmetic. All of these are identical in behavior and still use ... Read More

Function pointer to member function in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

13K+ Views

In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.Example Live Demo#include using namespace std; class AB {    public:       int sub(int a, int b) {          return a-b;       }       int div(int a, int b) {          return a/b;       } }; //using function pointer int res1(int ... Read More

Function Pointer in C

Anvi Jain
Updated on 30-Jul-2019 22:30:26

5K+ Views

Function Pointers point to code like normal pointers.In Functions Pointers, function’s name can be used to get function’s address.A function can also be passed as an arguments and can be returned from a function.Declarationfunction_return_type(*Pointer_name)(function argument list)Example Live Demo#include int subtraction (int a, int b) {    return a-b; } int main() {    int (*fp) (int, int)=subtraction;    //Calling function using function pointer    int result = fp(5, 4); printf(" Using function pointer we get the result: %d",result); return 0; }OutputUsing function pointer we get the result: 1

Advertisements