Found 34490 Articles for Programming

How to execute a command and get the output of command within C++ using POSIX?

Abhinanda Shri
Updated on 12-Feb-2020 06:15:52

11K+ Views

You can use the popen and pclose functions to pipe to and from processes. The popen() function opens a process by creating a pipe, forking, and invoking the shell. We can use a buffer to read the contents of stdout and keep appending it to a result string and return this string when the processes exit.example#include #include #include #include using namespace std; string exec(string command) {    char buffer[128];    string result = "";    // Open pipe to file    FILE* pipe = popen(command.c_str(), "r");    if (!pipe) {       ... Read More

Why can't variables be declared in a switch statement in C/C++?

George John
Updated on 27-Jan-2020 12:30:17

780 Views

Variables can be declared in a switch statement. You'll just need to declare them and use them within a new scope in the switch statement. For example,Example#include using namespace std; int main() {    int i = 10;    switch(i) {       case 2:       //some code       break;       case 10:{          int x = 13;          cout

Difference between 'struct' and 'typedef struct' in C++?

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

770 Views

In C++, there is no difference between 'struct' and 'typedef struct' because, in C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.Though there is one subtle difference that typedefs cannot be forward declared. So for the typedef option, you must include the file containing the typedef before it is used anywhere.

Why should we avoid using global variables in C/C++?

Ankitha Reddy
Updated on 24-Jun-2020 06:10:51

3K+ Views

We should avoid using global variables in any language, not only C++. This is because these variables pollute the global namespace, can cause some very nasty bugs in big projects as they can be accessed from any file and hence be modified from anywhere. These are some of the reasons why global variables are considered bad −Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use.A global variable can have no access control. It can not be limited to some parts of the program.Using global variables causes very ... Read More

What are forward declarations in C++?

Abhinaya
Updated on 12-Feb-2020 06:14:19

370 Views

Forward declaration lets the code following the declaration know that there is are classes with the name Person. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes. exampleClass Person; void myFunc(Person p1) {    // ... } Class Person {    // Class definition here };So in this case when the compiler encounters myFunc, it'll know that it's going to encounter this class somewhere down in the code. This can be used in cases where code using the class is placed/included before the code containing the class definition.

What does int argc, char *argv[] mean in C/C++?

Chandu yadav
Updated on 24-Jun-2020 06:12:03

7K+ Views

argc stands for argument count and argv stands for argument values. These are variables passed to the main function when it starts executing. When we run a program we can give arguments to that program like −$ ./a.out helloExampleHere hello is an argument to the executable. This can be accessed in your program. For example,#include using namespace std; int main(int argc, char** argv) {    cout

Regular cast vs. static_cast vs. dynamic_cast in C++

George John
Updated on 24-Jun-2020 06:13:58

10K+ Views

static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.dynamic_cast −This cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritence when you cast from base class to derived class.Regular Cast − This is the most powerful cast available in C++ as it combines const_cast, static_cast and reinterpret_cast. but it's also unsafe ... Read More

Passing two dimensional array to a C++ function

Govinda Sai
Updated on 06-Sep-2023 13:28:22

42K+ Views

C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index. There are three ways to pass a 2D array to a function −Specify the size of columns of 2D arrayvoid processArr(int a[][10]) {    // Do something }Pass array containing pointersvoid processArr(int *a[10]) {    // Do Something } // When callingint *array[10]; for(int i = 0; i < 10; i++)    array[i] = new int[10]; processArr(array);Pass a pointer to a pointervoid processArr(int **a) {    // ... Read More

Common undefined behaviours in C++ programming

George John
Updated on 12-Feb-2020 05:15:32

60 Views

The following are most common causes of undefined behaviour in C++ programming. Note that all of these are specified in the standard to lead to undefined behaviour and should be avaoided at all costs when writing programs. Signed integer overflowDereferencing a NULL pointer, a pointer returned by a "new" allocation of size zero, pointer that has not yet been definitely initialized, pointer at a location beyond the end of an array. Using pointers to objects which have gone out of scope or have been deleted Performing pointer arithmetic that yields a result outside the boundaries of an array.Converting pointers to objects of incompatible ... Read More

What are the differences between struct and class in C++?

Ramu Prasad
Updated on 30-Jul-2019 22:30:22

238 Views

The members and base classes of a struct are public by default, while in class, they default to private. Struct and class are otherwise functionally equivalent.They are however used in different places due to semantics. a struct is more like a data structure that is used to represent data. class, on the other hand, is more of a functionality inclined construct. It mimics the way things are and work.

Advertisements