Jump to content


Photo

Casting to a pointer-to-an-array


  • Please log in to reply
8 replies to this topic

#1 robzy

robzy

    Immortal

  • Hero
  • 42,882 posts

Posted 15 January 2010 - 10:18 PM

uint32 in_magic[20] = {0xa4867ada,0x8e991a16, 0xe40a2042, 0x1f35b7a7, 0x3a1e7e68,0x8ba696f7, 0x32d9af3f, 0x25dece5e,0x6dd0b728,0x91551343, 0x1601b78e, 0x0d63c329,0x17c1b244,0xdeaf4c0c, 0x22893e38, 0x89ee68c2, 0xee81d6e9,0x10dc38d8, 0xc6d85cb8, 0xf4aa7c86};
uint32 (* progmemmap)[10][2] = &in_magic;

Returns:

machoinit.c:20: warning: initialization from incompatible pointer type

Of course, I have no idea how to cast to a pointer-to-an-array. I've tried a few things, (unit32*[][]) being one of them, with no luck.

Any suggestions?

(And yes, I totally realise that I don't actually have to do this, I could just access all the data straight through in_magic, but I'd prefer not to for now - if not just for the sake of clarity)

Thanks,
Rob.

#2 Midnighter

Midnighter

    Titan

  • Atomican
  • 28,666 posts

Posted 15 January 2010 - 11:37 PM

I have no frakking idea what you are doing. I blame the Jews!! :P

Edited by Midnighter, 15 January 2010 - 11:37 PM.

Marcus Aurelius: ‘Live a good life. If there are gods and they are just, then they will not care how devout you have been, but will welcome you based on the virtues you have lived by. If there are gods, but unjust, then you should not want to worship them. If there are no gods, then you will be gone, but will have lived a noble life that will live on in the memories of your loved ones.’

#3 robzy

robzy

    Immortal

  • Hero
  • 42,882 posts

Posted 15 January 2010 - 11:49 PM

Crud! This should be in programming. My bad. Rob.

#4 freespace

freespace

    Champion

  • Hero
  • 7,725 posts

Posted 16 January 2010 - 12:22 AM

Your array is not 2 dimensional, just

int * progmemmap = in_magic;

Is sufficient. But I wonder why you are doing this: the array name "in_magic" nearly always decays to a pointer to its first element.

#5 robzy

robzy

    Immortal

  • Hero
  • 42,882 posts

Posted 16 January 2010 - 02:00 AM

Your array is not 2 dimensional, just

int * progmemmap = in_magic;

Is sufficient. But I wonder why you are doing this: the array name "in_magic" nearly always decays to a pointer to its first element.

But then I can't do "progmemmap[3][0] = 5;" can I?

[edit]:

Oh, perhaps I should point out that while my in_magic array is not 2 dimensional - I'm wanting to access it as if it is - hence the progmemmap variable (which I think should be [9][1], not [10][2] :S But I can figure that out myself).

It occurred to me shortly after posting the OP that I might be able to do something like that with a union, but I'm still wanting to know how to specify a pointer-to-an-array, as I think I need it for a function that takes a pointer-to-a-two-dimensional-array as a parameter.

Rob.

Edited by robzy, 16 January 2010 - 02:06 AM.


#6 freespace

freespace

    Champion

  • Hero
  • 7,725 posts

Posted 16 January 2010 - 11:32 AM

Well in that case, see http://www.ibiblio.o...n/append-c.html

Specifically, you can either create an auxiliary array of pointers, or use correct version of your first attempt:

uint32 (* progmemmap)[2] = (uint32 (*)[2])in_magic;

That declares a pointer to an array of length 2, which is what a 2d array's name decays into: a pointer to the first row. You don't need to specify the first dimension because you supply that when you access it.

See the array equation in the above link.

Cheers,
Steve

Edited by freespace, 16 January 2010 - 11:36 AM.


#7 robzy

robzy

    Immortal

  • Hero
  • 42,882 posts

Posted 16 January 2010 - 12:42 PM

That compiled fine, no warnings with -Wall :) Thank you. I'm not certain how it works though. For instance I don't know whether progmemmap[4][5] refers to the 4*2+5=13th element or the 4+5*2=14th element. As in, I don't know which [] is which dimension. But I'm sure I can figure that out on my own with that link and a bit of googling, thanks :) [edit]: It occurs to me that I really ought to get a book on C. I understand (more or less) all of the behind the scenes stuff, but keeping on getting into a pickle when it comes to more complicated syntax such as this. Rob.

Edited by robzy, 16 January 2010 - 12:52 PM.


#8 freespace

freespace

    Champion

  • Hero
  • 7,725 posts

Posted 16 January 2010 - 01:11 PM

Because of 0 indexing, the nth element has index n-1.

A trick I learned is that when unsure, consider a small test case, like 2x2 array. Since the logic must work for all cases, if it works for 2x2 it should work for nxm. This trick is generally applicable. It is much easier to manipulate small amounts of data in our heads / on paper than it is to consider the actual data involved.

The equations in the above link should make clear what is going on. But if not, I shall make an attempt:

If you declare
char a[10][2];
Then a decays into a pointer to the first row, which is an array of length 2.

The declaration
char (*x)[2];

Is declaring a pointer to a data type, but instead of char or int or float, it is pointing to a char array of length 2. Now there is an equivalence between a and x, and you can assign a to x and then use x like you would a in places where a decays to a pointer. Keep mind this condition: in places where a does not decay into a pointer to the first row, a is a very different beast to x.

Under the hood, a 2D array is realised in 1D memory address by concatenating the rows. So really a 2D array is a 1D array containing 1D arrays as elements, and a[n][m] is really saying "give me the nth element of a, and give me the mth element of that element". This nested-1D-array representation of multidimensional arrays extends to high dimensions.

Understanding this nested-1D-array representation of multidimensional array is essential to be able to dynamically allocate multidimensional arrays in runtime, by constructing a suitable array of pointers.

If you have done differential equations, this approach should be similar to how you rewrite high order derivatives as a set of equations using only first derivatives.

Edited by freespace, 17 January 2010 - 11:35 AM.


#9 robzy

robzy

    Immortal

  • Hero
  • 42,882 posts

Posted 17 January 2010 - 09:59 AM

Thanks freespace, I'll muck around with it a bit and see where I get :) Rob.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users