Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

Programming/Scripting/Development/Web Design

[C] pointer to array - Page 1

[C] pointer to array

Posted by: ISSOtm
Date: 2016-09-28 04:16:33
In C, you declare a variable like this :

unsigned int numberOfGen1Glitches = 9001;

And an array like this :

unsigned int partyTime[42] = {1, 3, 3, 7};


Okay. When I ask GDB about "&partyTime", I get "unsigned int(*)[42] (stuff)".
But this does NOT work :

unsigned int(*) lelzNotWorking[42] = &partyTime;

GCC throws an error about parentheses. Placing a space before the parentheses doesn't work either.

How do I declare a variable that has type "pointer to unsigned int array" ?
How do I declare an array of pointers to arrays ? (An array of variables with the above type)

Thanks in advance !

(Note :

unsigned int array[3];
unsigned int** pArray = &array;

gives a "assignment from incompatible pointer type" when compiled with GCC, which I use.)

Re: [C] pointer to array

Posted by: TheZZAZZGlitch
Date: 2016-09-28 08:47:42
int a, b, c, d;

// An array of pointers
int *stuff[4] = {&a, &b, &c, &d};
// A pointer to an array of pointers
int *(*ptr_to_stuff)[4] = &stuff;
// An array of pointers to arrays
int *(*array_of_ptr_to_stuff[2])[4] = {&stuff, &stuff};

int main(){
    // Will print the exact same thing
    printf("%p\n", &a);
    printf("%p\n", stuff[0]);
    printf("%p\n", (*ptr_to_stuff)[0]);
    printf("%p\n", (*array_of_ptr_to_stuff[1])[0]);
}

Re: [C] pointer to array

Posted by: ISSOtm
Date: 2016-09-29 18:40:33
Oi thanks, it works like a charm !
Now that I've saved some sweet bytes, let's work on the serious stuff…