Home > Enterprise >  what are all the different ways to initialize a c array?
what are all the different ways to initialize a c array?

Time:11-22

class Solution {
public:
    int numSquares(int n) {
        int dp[n 1];
        for(int i=0;i<=n;i  ) dp[i]=0;  //initializing all the elements to zero
        for(int i=1;i<=n;i  ){
            int t = INT_MAX;
            for(int j=1;j<=(int)sqrt(i);j  ){
                t = min(t,dp[i-j*j]);
            }
            dp[i] = t 1;
        }
        return dp[n];
    }
};

The above method works perfectly fine but when I tried to initialize the array like this

int dp[n] = {0}  //variable sized array cannot be initialized

I am getting error like variable sized array cannot be initialized . Is there any why to initialize this array instead of using for loop and please explain me why I am getting this error?.

CodePudding user response:

The problem is that in C , the size of an array must be a compile time constant. So,

int n = 10;
int arr[n] ; //incorrect

The correct way would be:

const int n = 10;
int arr[n]; //correct

You can solve your problem by using:

const int n = 10;
int arr[n] = {0}; //correct now because n is a constant expression

Some more examples:

void func(int n)
{
   int arr[n] = {0}; //incorrect because n is passed as a function argument and is therefore not a compile time constant
}

Another example

int n;
cin >> n;
int arr[n] = {0}; //incorrect

You can use std::vector as it is a dynamic sized container.

CodePudding user response:

what are all the different ways to initialize a c array?

They are:

  • default initialisation (which means "no initialisation" in case of trivial objects)
  • list initialisation
  • value initialisation (which is list initialisation with an empty list)

why I am getting this error?.

Your program is ill-formed. The size of an array variable must be compile time constant, but n 1 is not.

You are using a language extension. As the error message implies, the language extension doesn't allow all forms of initialisation to be used. You can use default initialisation i.e. "no" initialisation as you did in the first code example.

but what if you are getting the array size as a parameter in a function in class?

Then create a dynamic array. I recommend using std::vector.

CodePudding user response:

what are all the different ways to initialize a c array?

That's described here: Aggregate initialization.

The main problem is that VLA:s (variable length arrays) do not exist in standard C so instead of int dp[n 1]; you should use std::vector<int> dp(n 1);

Example:

#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>

class Solution {
public:
    int numSquares(int n) {
        std::vector<int> dp(n   1);       // instead of `int dp[n 1];`
        // for(int i=0;i<=n;i  ) dp[i]=0; // no need

        for(int i =  1; i <= n; i  ) {
            int t = INT_MAX;
            for(int j = 1, end = (int) std::sqrt(i); j <= end;   j) {
                t = std::min(t, dp[i - j * j]);
            }
            dp[i] = t   1;
        }
        return dp[n];
    }
};
  • Related