int main(int args, char* argv[]) {
string* list;
listDevices(list);
printf("device 0: %s\n", list[0]); // prints some junks
}
void listDevices(string* list) {
list = new string[1];
list[0] = "abc";
printf("device 0: %s\n", list[0]); //prints "abc"
}
In the above code, list array is initialized and assigned some values in the listDevices method, but when I print it outside the function, some junks would be printed.
Why does it work correctly if I send string list[1] as input to the above method but not when sending string* list (although both are pointers)?