array declaration c 2b 2b

Solutions on MaxInterview for array declaration c 2b 2b by the best coders in the world

showing results for - "array declaration c 2b 2b"
Selena
14 Feb 2019
1#include <iostream>
2using std::cout;
3
4int a[] = { 1, 2, 3, 4, 5 };
5int counta()
6  {
7  return sizeof( a ) / sizeof( a[ 0 ] );  // works, since a[] is an array
8  }
9
10int countb( int b[] )
11  {
12  return sizeof( b ) / sizeof( b[ 0 ] );  // fails, since b[] is a pointer
13  }
Mariana
22 Oct 2018
1int foo [] = { 16, 2, 77, 40, 12071 };
Lauren
21 Aug 2019
1#include <iostream>
2#include <array> //for using std::array
3
4int main()
5{
6
7	int example[5];//array on stack
8	int* another = new int[5];//array on heap
9	delete[] another;//freeing up memory on heap
10	example[0] = 1;
11	example[1] = 2;
12	example[2] = 3;
13	example[3] = 4;
14	for (int i = 0; i < 5; i++) {
15		example[i] = 2;
16	}
17	int* ptr = example;//arrays are just pointers to the begining of the block of memory
18	example[2] = 5;
19	*(ptr + 2) = 6;//adding 4+4 bytes to ptr
20	std::cout << example[2] << std::endl;//output => 6
21	*(int*)((char*)ptr + 8) = 8;//adding 8 bytes to ptr using ptr arithmetic
22	std::cout << example[2] << std::endl;//output => 8
23	//std::array provide some additional functionality like bounce checking size checking but do have a performance overhead
24	std::array<int,5> stda;//creating an array named stda of int 5 size
25	std::cout << stda.size() << std::endl;//will output size of std::array ,output =>5   
26    std::cin.get();
27}
Alexander
30 Feb 2018
1int foo [5];
Noelie
10 Mar 2020
1// Two dimensional array
2int a[2][3]= {
3        {1, 2, 3},
4        {4, 5, 6}
5    };
6    
7    cout << a[1][1]; // Output is 5
8
9// Three dimensional array
10//[2] is elements; [3] is rows in elements; [4] is column in elemnents 
11int a[2][3][2]= {
12        //Element 0
13        { {1, 2}, 
14          {2, 3}, 
15          {4, 5} 
16            
17        },
18        
19        
20        // Element 1
21        { {6, 7}, 
22          {8, 9}, 
23          {10, 11} 
24            
25        }
26    };
27    
28    cout << a[0][1][1]; // Prints 3
29
Luigi
16 Feb 2017
1// datatype var_name[howmuch value you need to store] = {values, values}
2int a[5] = {1, 2 3, 4, 5};
similar questions
queries leading to this page
array declaration c 2b 2b