Found 34471 Articles for Programming

Pointer vs Array in C

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

391 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

79 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

235 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

Short hand array notation in C/C++

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

341 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

How to create a dynamic array of integers in C++ using the new keyword

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

10K+ Views

In C++, a dynamic array can be created using new keyword and can be deleted it by using delete keyword.Let us consider a simple example of it.Example Code Live Demo#include using namespace std; int main() {    int i,n;    cout

Passing Arrays to Function in C++

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

238 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.If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.Method-1Formal parameters as a pointer as follows −void myFunction(int *param) {    .    .    . }Method-2Formal parameters as ... Read More

How to initialize a dynamic array in C++?

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

2K+ Views

Here is a C++ program to initialize a dynamic array. Here in this program we can use dynamically allocated array to return a local array from the function Array().Example 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

How to use std::sort to sort an array in C++

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

8K+ Views

In programming language, sorting is a basic function which is applied to data to arrange these data is ascending or descending data. In C++ program, there is a function std::sort() for sorting the array.sort(start address, end address)Here,Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.Example Code Live Demo#include #include using namespace std; void display(int a[]) {    for(int i = 0; i < 5; ++i)    cout

How to pass an array by reference in C++

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

6K+ Views

If we pass the address of an array while calling a function, then this is called function call by reference. The function declaration should have a pointer as a parameter to receive the passed address, when we pass an address as an argument.Example Code Live Demo#include using namespace std; void show( int *num) {    cout

Advertisements