Found 7401 Articles for C++

How many levels of pointers can we have in C/C++?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

122 Views

Actually, C programs one or two static levels of pointers are common. Triple indirection is rare. But infinite is very common. Infinite pointer indirection can be achieved with the help of a struct.struct list { struct list *next; ... } lst; lst->next->next->next->...->nextand in this way we can implement multiple pointer indirection.There is another alternative notation as shown below– *(*(..(*(*(*lst).next).next).next...).next).next

Basic Input/Output in C++

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

297 Views

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming.C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.The Standard Output Stream (cout)The ... Read More

How does #include work in C++?

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

2K+ Views

The is a header file. This file includes all standard library. Sometimes in some coding contests, when we have to save time while solving, then using this header file is helpful.In software engineering approach we should reduce the minimize the include. Using this header file, it will include lots of files, sometimes that may not be required in the program. So it may increase the compile time and program size. Some of the big disadvantages of this header file is listed below −This is not a standard header file of GNU C++ library. So some compiler may fail ... Read More

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

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

Dangling pointerDangling pointer is a pointer pointing to a memory location that has been freed (or deleted). There are different ways where Pointer acts as dangling pointerFunction CallThe pointer pointing to local variable becomes dangling when local variable is not static.int *show(void) {    int n = 76; /* ... */ return &n; }OutputOutput of this program will be garbage address.De-allocation of memoryint main() {    float *p = (float *)malloc(sizeof(float));    //dynamic memory allocation.    free(p);    //after calling free()    p becomes a dangling pointer p = NULL;    //now p no more a dangling pointer. }Variable goes ... Read More

Compare *ptr++, *++ptr and ++*ptr in C++

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

2K+ Views

In this section, we will see what are the differences between *ptr++, *++ptr and ++*ptr in C++.Here we will see the precedence of postfix++ and prefix++ in C or C++. The precedence of prefix ++ or -- has higher priority than dereference operator ‘*’ and postfix ++ or -- has a priority higher than both prefix ++ and dereference operator ‘*’.When ptr is a pointer, then *ptr++ indicates *(ptr++) and ++*prt refers ++(*ptr)Example Code Live Demo#include using namespace std; int main() {    char arr[] = "Hello World";    char *ptr = arr;    ++*ptr;    cout

Standard Size of character ('a') in C/C++ on Linux

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

4K+ Views

In C++ the size of the character literal is char. In C the type of character literal is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.Example Code Live Demo#include main() {    printf("%d", sizeof('a')); }Output1Example Code Live Demo#include using namespace std; main() {    cout

How do you declare an interface in C++?

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

332 Views

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.A class is made abstract by declaring at least one of its functions as a pure virtual function. A pure virtual function is specified by placing "= 0" in its declaration as follows −class Box {    public:       // pure virtual function       virtual double getVolume() ... Read More

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

365 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++

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

6K+ Views

In this section, we will see how to get the current working directory using C or C++. We have defined some flags for the current operating system.Example Code Live Demo#ifdef WINDOWS #include #define GetCurrentDir _getcwd #else #include #define GetCurrentDir getcwd #endif #include using namespace std; std::string get_current_dir() {    char buff[FILENAME_MAX]; //create string buffer to hold path    GetCurrentDir( buff, FILENAME_MAX );    string current_working_dir(buff);    return current_working_dir; } main() {    cout

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

Chandu yadav
Updated on 30-Jul-2019 22:30:25

602 Views

Dangling pointerDangling pointer is a pointer pointing to a memory location that has been freed (or deleted). There are different ways where Pointer acts as dangling pointerFunction CallThe pointer pointing to local variable becomes dangling when local variable is not static.int *show(void) {    int n = 76; /* ... */ return &n; }OutputOutput of this program will be garbage address.De-allocation of memory#include #include int main() {    float *p = (float *)malloc(sizeof(float));    //dynamic memory allocation. free(p);    //after calling free() p becomes a dangling pointer p = NULL;    //now p no more a dangling pointer. ... Read More

Advertisements