Wendy
7
Q:

c++ initialize array

int foo [] = { 16, 2, 77, 40, 12071 };
9
int nCount[] = {1, 2, 3, 4, 5};
3
// Array declaration by specifying size and initializing 
// elements 
int arr[6] = { 10, 20, 30, 40 } 
  
// Compiler creates an array of size 6, initializes first 
// 4 elements as specified by user and rest two elements as 0. 
// above is same as  "int arr[] = {10, 20, 30, 40, 0, 0}" 
4
#include <iostream>
using std::cout;

int a[] = { 1, 2, 3, 4, 5 };
int counta()
  {
  return sizeof( a ) / sizeof( a[ 0 ] );  // works, since a[] is an array
  }

int countb( int b[] )
  {
  return sizeof( b ) / sizeof( b[ 0 ] );  // fails, since b[] is a pointer
  }
1
int nums[100] = {0}; // initiallize all values to 0

int nums[5] = {1,2,3,4,5};

// type name[size] = {values};
1
int foo [5];
0
int arr[3] = {1, 5, 4};
1

New to Communities?

Join the community