Main Menu
Main Page
Forums
New pages
Recent changes
Random page
Help

Glitches
Arbitrary code execution
Pokémon cloning
Pomeg glitch
Tweaking
Glitches by generation
Glitch categories

References/Resources
Databases
Disassembly projects
The Big HEX List
Pokémon cheat codes
Pokémon glitch terminology
Useful tools
More

Affiliates
Legendary Star Blob 2 (Hakuda)
Pokémon Speedruns wiki
PRAMA Initiative
Become an affiliate!

Technical
Site source code

Search Wiki

 

Search Forums

 

Author Topic: [C] pointer to array  (Read 927 times)

0 Members and 1 Guest are viewing this topic.

ISSOtm

  • The French Lord of Laziness (and a huge The Legend Of Zelda fan)
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • Pewter City (B)rocks !
    • View Profile
    • My Little Website
[C] pointer to array
« on: September 28, 2016, 03:16:33 am »
In C, you declare a variable like this :
Code: [Select]
unsigned int numberOfGen1Glitches = 9001;
And an array like this :
Code: [Select]
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 :
Code: [Select]
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 :
Code: [Select]
unsigned int array[3];
unsigned int** pArray = &array;
gives a "assignment from incompatible pointer type" when compiled with GCC, which I use.)
"THOU SHALL NOT PASS !!"  RIVAL's effect, Gandalf.

Proudly glitching Pokémon Red and Yellow on a Black & White GB, Pocket GB, GB Color, GBA SP and new 3DS.

My Twitter (beware, I'm French)
My YouTube (same warning)

Here is an online tool to build 8F setups : GBz80 to Items !

They see me layzin', they ha-tin'...
Heavy contributor of the global augmentation of entropy (my room's is too damn high !)

TheZZAZZGlitch

  • Distinguished Member
  • *
  • Offline Offline
  • Gender: Male
  • Unknown opcode fc at 801a
    • View Profile
Re: [C] pointer to array
« Reply #1 on: September 28, 2016, 07:47:42 am »
Code: [Select]
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]);
}
« Last Edit: September 28, 2016, 07:47:57 am by TheZZAZZGlitch »
qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF qÁF

ISSOtm

  • The French Lord of Laziness (and a huge The Legend Of Zelda fan)
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • Pewter City (B)rocks !
    • View Profile
    • My Little Website
Re: [C] pointer to array
« Reply #2 on: September 29, 2016, 05:40:33 pm »
Oi thanks, it works like a charm !
Now that I've saved some sweet bytes, let's work on the serious stuff...
"THOU SHALL NOT PASS !!"  RIVAL's effect, Gandalf.

Proudly glitching Pokémon Red and Yellow on a Black & White GB, Pocket GB, GB Color, GBA SP and new 3DS.

My Twitter (beware, I'm French)
My YouTube (same warning)

Here is an online tool to build 8F setups : GBz80 to Items !

They see me layzin', they ha-tin'...
Heavy contributor of the global augmentation of entropy (my room's is too damn high !)