Found 1339 Articles for C

Why C/C++ array index starts from zero?

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

755 Views

As Array index starts with 0, so a[i] can be implemented as *(a + i).If Array index starts with 1 then a[i] will be implemented as *(a+i-1) which will be time consuming during compilation and the performance of the program will also be effected.So, it is better to start index of the array from 0.A simple program of array is given -Example Codeint main() {    int array[5] = {7, 7, 7, 6, 6};    for (int i = 0; i < 5; i++)       cout

Pointer vs Array in C

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

457 Views

Pointers and array most of the time are treated as same in c. Some differences are:&operator:&pointer = returns the address of pointer.&array = returns the address of first element.sizeof operator:sizeof(array) = returns the total memory consumed by the all the elements of the array.sizeof(pointer) = returns the only memory consumed by the pointer variable itself.Array variable can’t be re-assigned a value whereas pointer variable can.Declaration:int a[]; //array Int *p; //pointerLet us consider that there is an integer pointer variableint *i;Now let us consider the outcome of the following assignments –a = &i; //illegal assignment. a variable can not be updated ... Read More

An Uncommon representation of array elements in C/C++

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

127 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Why C treats array parameters as pointers?

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

289 Views

C treats array parameter as pointers because it is less time consuming and more efficient. Though if we can pass the address of each element of the array to a function as argument but it will be more time consuming. So it’s better to pass the base address of first element to the function like:void fun(int a[]) { … } void fun(int *a) { //more efficient. ….. }Here is a sample code in C:#include void display1(int a[]) //printing the array content {    int i;    printf("Current content of the array is: ");    for(i = 0; i < ... Read More

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 03-Mar-2025 13:16:03

6K+ Views

In C/C++, we usually use the sizeof operator to find the size of an array. However, once an array is passed to a function, its size is lost. So, how can we find the size of an array without using sizeof? For example, consider this array: int arr[] = {1, 2, 3, 4, 5}; Without using sizeof, we can use a loop to count the number of elements in the array. In this article, we'll show you different methods to find the size of an array in C or C++ without using sizeof, with clear and simple examples. ... Read More

Initialization of a multidimensional arrays in C/C++

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

224 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

223 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?

Farhan Muhamed
Updated on 02-May-2025 18:27:39

1K+ Views

In C++, segmentation fault is a runtime error that occur when your program attempts to access an area of memory that it is not allowed to access. In other words, segmentation fault occur when your program tries to access memory that is beyond the limits that the operating system allocated for your program. These errors are type of access violation that can lead to crashing of program. Segmentation fault occur often with beginner programmers due to the lack of understanding of system level concepts like pointer. In this article, we will explain all the concepts related to segmentation fault, including ... Read More

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

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

383 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

Advertisements