Found 7197 Articles for C++

Pointers, smart pointers and shared pointers in C++

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

5K+ 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

493 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

Are there benefits of passing by pointer over passing by reference in C++?

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

513 Views

A pointer can receive a null parameter whereas a reference can’t. You can only use pointer if you want to pass “no object”.Explicitly passing by pointer allow us to see the whether the object is passes by reference or value at call site.These are simple example of passing by pointer and passing by reference −Passing by pointerExample Live Demo#include using namespace std; void swap(int* a, int* b) {    int c = *a;    *a= *b;    *b = c; } int main() {    int m =7 , n = 6;    cout

Removing an element from C++ std::vector<> by index?

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

424 Views

Remove an element from C++ std::vector by index can be done by following way −Example Live Demo#include #include using namespace std; int main() {    vector v; //declare vector    //insert elements into vector    v.push_back(-10);    v.push_back(7);    v.push_back(6);    // Deletes the first element (v[0])    v.erase(v.begin() );    for (int i = 0; i < v.size(); i++)       cout

The best way to check if a file exists using standard C/C++

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

18K+ Views

The only way to check if a file exist is to try to open the file for reading or writing.Here is an example −In CExample#include int main() {    /* try to open file to read */    FILE *file;    if (file = fopen("a.txt", "r")) {       fclose(file);       printf("file exists");    } else {       printf("file doesn't exist");    } }Outputfile existsIn C++Example#include #include using namespace std; int main() {    /* try to open file to read */    ifstream ifile;    ifile.open("b.txt");    if(ifile) {       cout

Read integers from a text file with C++ ifstream

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:08:03

7K+ Views

In C++, we can read the integer values from text files using the ifstream class that allows the user to perform input operations from the files. Here, we have filename (tp.txt) that used in the program and store some integers value. 21 61 24 05 50 11 12 21 66 55 Example to Read Integers from a Text File In this example, we created one text file to save the integers values and then access those value using file handling operation. #include #include using namespace std; int main() { // Define the maximum ... Read More

How to read file content into istringstream in C++?

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

1K+ Views

Here is a C++ program to read file content into isstringstream in C++.Example#include #include #include using namespace std; int main() {    ifstream is("a.txt", ios::binary );    // get length of file:    is.seekg (0, std::ios::end);    long length = is.tellg();    is.seekg (0, std::ios::beg);    // allocate memory:    char *buffer = new char [length];    // read data as a block:    is.read (buffer,length);    // create string stream of memory contents    istringstream iss( string( buffer ) );    cout

Advertisements