Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializer fails to determine size of character array

Tags:

c++

memory

gcc

Whenever I run the code below (Yes that's all the code) the compiler gives me an error:

error: initializer fails to determine size of ‘path’

What I want to do is copy the contents of argv[0], which is the path of the application as a character array, into the path variable.

int main(int argc, char** argv) {
    int offset = 0;
    int pathSize = 0;
    while(argv[0][pathSize] != '\0')
        pathSize++;

    char path[] = new char[pathSize];
    delete &path;
    return 0;
}
like image 929
Jeroen Avatar asked Dec 07 '22 06:12

Jeroen


1 Answers

Your error is in the below part of your code:

char path[] = new char[pathSize];
delete &path;

Change it to...

char *path = new char[pathSize];
delete[] path;
like image 150
Sabashan Ragavan Avatar answered Dec 11 '22 09:12

Sabashan Ragavan