-2

I want to change from command line input to variable input.

int main(int argc, char *argv[])
{
    std::cout << argc << std::endl;
    std::cout << argv[0] << std::endl;
    std::cout << argv[1] << std::endl;
}

Change to this, but when I want to compile this an error appears.

int main()
{
    int argc = 2;

    char *argv[] = 0;

    argv[0] = "./server";
    argv[1] = "127.0.0.1";
}

This error appears: error: array initializer must be an initializer list char *argv[] = 0;

TopMaax
  • 3
  • 3

1 Answers1

2

You have to provide the size of the array, since you did not provide an initializer from which the compiler can deduce the size. Again, from C++11 you cannot have a string-literal bind to char*, use const char*.

int main()
{
    constexpr int argc = 2;
    const char *argv[argc] = {};

    argv[0] = "./server";
    argv[1] = "127.0.0.1";
}

You may want to explore good use of std::array<std::string, 2> instead.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68