Home > Blockchain >  What is the difference between a regular array, and array of pointers
What is the difference between a regular array, and array of pointers

Time:11-06

Can someone plz explain me what is the diffrence between those tow lines and when we will want to use each one of then


int array[] = { 1, 2, 3 }; // init a regular array


int *numList = {1, 2, 3}; //  init an array of pointers?

I am expecting that there are probably seenarios we will want to use the second over the first, But when?

Thank You in Advance

CodePudding user response:

You're conflating some concepts. As @ikegami noted, your second line:

int *numList = {1, 2, 3};

Gets treated as:

int *numList = 1;

Which not an array, nor a valid pointer. If you want to create an array of pointers, you use the same syntax as normal arrays, with the type being a pointer:

int* numList[] = {
    &array[0],
    &array[1],
    &array[2]
};

Will create an array of 3 int pointers, pointing to your original array's elements.

  • Related