Found 1401 Articles for C

Short hand array notation in C/C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

337 Views

If there are repeated values are present in C then we use shorthand array notation to define that array.Here is an example:Example Code#include int main() { int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}; for (int i = 0; i < 10; i++) printf("%d ", array[i]); return 0; }Output7 7 7 7 6 6 2 2 2 2In this program,int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}is similar as,int array[10] = {7, 7, 7, 7, 6, 6, 2, 2, 2, 2}.If there is any gap in the middle of the array it will be filled up by 0.In C++ above program will give the same output but it will give warnings with the output.

Flexible Array Members in a structure in C

Nitya Raut
Updated on 30-Jul-2019 22:30:25

1K+ Views

Flexible Array members in a structure in C means we can declare array without its dimension within a structure and its size will be flexible in nature. Flexible array member must be the last member of class.Here is an example:Example#include #include #include //structure of type employee and must contain at least one more named member in addition to the flexible array member. struct employee {    int emp_id;    int name_len;    int emp_size; //‘emp_size’ variable is used to store the size of flexible    character array emp_name[].    char emp_name[]; //Flexible array member emp_name[] should be the last member ... Read More

Find size of array in C/C++ without using sizeof

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

5K+ Views

In this program, we find out Find size of array in C/C++ without using sizeof.AlgorithmBegin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the ... Read More

Initialization of a multidimensional arrays in C/C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

131 Views

In multidimensional array, the array should have dimension more that 1. The following diagram shows the memory allocation strategy for a multidimensional array with dimension 3 x 3 x 3.This is a C++ program to initialize a multidimensional array.AlgorithmBegin    Initialize the elements of a multidimensional array.    Print the size of the array.    Display the content of the array. EndExample#include using namespace std; int main() {    int r, c;    int a[][2] = {{3,1},{7,6}};    cout

How to return a local array from a C/C++ function

Nitya Raut
Updated on 30-Jul-2019 22:30:25

139 Views

This is a C++ program return a local array from a function.AlgorithmBegin We can use dynamically allocated array to return a local array from function Array(). Print the elements of the array. EndExample Code Live Demo#include using namespace std; int* Array() { int* a = new int[100]; a[0] = 7; a[1] = 6; a[2] = 4; a[3] = 5; return a; } int main() { int* p = Array(); cout

What is a segmentation fault in C/C++ program?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

943 Views

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.Segmentation faults are mostly caused by pointers that are:Used to being properly initialized.Used after the memory they point to has been reallocated or freed.Used in an indexed array where the index is outside of the array bounds.

How to detect integer overflow in C/C++?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

290 Views

The only safe way is to check for overflow before it occurs. There are some hacky ways of checking for integer overflow though. So if you're aiming for detecting overflow in unsigned int addition, you can check if the result is actually lesser than either values added. So for example, Example Codeunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively "value < y" should also workThis is because if x and y are both unsigned ints, if added and they overflow, their values can't be greater than either of them ... Read More

How do you reverse a string in place in C or C++?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

478 Views

In this section we will see how to reverse a string in place. So we will not use some other memory spaces for reversing. In C++, we can use the std::string. But for C we have to use the character array. In this program we are using the character array to take the string. Then reversing it.Input: A string “This is a string” Output: The reversed string “gnirts a si sihT”Algorithmreverse_string(str)Input − The stringOutput − The reversed string.len := the length of the string i := 0 and j := (len-1) while i < j, do swap ... Read More

Convert a floating point number to string in C

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

7K+ Views

In this section we will see how to convert a number (integer or float or any other numeric type data) to a string.The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.Input: User will put some numeric value say 42.26 Output: This program will return the string equivalent result of that number like “42.26”AlgorithmStep 1 − ... Read More

C/C++ Macro for string concatenation

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

4K+ Views

In this program we will see how the macros are used to concatenate two strings. We can create two or more than two strings in macro, then simply write them one after another to convert them into a concatenated string. The syntax is like below:#define STR1 "str1" #define STR2 " str2" #define STR3 STR1 STR2 //it will concatenate str1 and str2Input: Take two stringsOutput: Return concatenated string.AlgorithmStep 1:Take two strings Step 2: Use macro to concatenate the strings Step 3: EndExample Code Live Demo#include #define STR1 "Hello" #define STR2 "World" #define STR3 STR1 STR2 main() { printf("%s", STR3); ... Read More

Advertisements