Server Side Programming Articles - Page 2241 of 2650

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

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

501 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

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

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

521 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

439 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 write my own header file in C?

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

2K+ Views

Steps to write my own header file in C −Type a code and save it as “sub.h”.Write a main program “subtraction.c” in which −include new header file.Write “sub.h” instead of All the functions in sub.h header are now ready for use.Directly call the function sub().Both “subtraction.c” and “sub.h” should be in same folder.Sub.hint sub(int m, int n) {    return(m-n); }subtraction.cExample#include #include "sub.h" void main() {    int a= 7, b= 6, res;    res = sub(a, b);    printf("Subtraction of two numbers is: %d", res); }After running ”subtraction.c” the output will be −OutputSubtraction of two numbers is: 1Read 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