tags:

views:

208

answers:

4

What is the Delphi equivalent of

unsigned char** ' 

in C

i'm not sure if its a pointer to an array or a pointer to a pointer or if the two things are the same in C.

+3  A: 

The two are the same in C. But usually when you see that, that's an array of char pointers. I'd declare it like this:

type
  PAnsiCharArray = ^TAnsiCharArray;
  TAnsiCharArray = array[0..0] of PAnsiChar;

Ugly, but then again so is most stuff that has to interact with C code. It should work, though.

Mason Wheeler
In D2009+ you can avoid this mess with {$POINTERMATH ON}. FPC has had this by default forever.
Marco van de Voort
+2  A: 

EDIT In C both pointer to an array and pointer to a pointer have different meaning to the compiler (thanks caf).

In C arrays are simply block of memory. There is no such function like Length(array), Low(array) or High(array) that you can use on Pascal arrays. For practical purpose to Pascal programmers C arrays can be usually ported to Pascal pointers, especially on function calls to various API.

If we assume that usigned char can be translated to byte in Delphi, then unsigned char * can be translated to ^byte that is usually done via type declaration that can look like:

type
  PByte = ^byte;
  PPByte = ^PByte;

If you are trying to convert some C code, especially .h header file then look at headconv.

Michał Niklas
this is what i was thinking, as PByte already exists I was going to declare PPByte = ^PByte and try that. just wanted some confirmation.
MikeT
Sorry, but your first sentence is completely incorrect. A pointer to an array (eg. `unsigned char (*x)[100]`) is treated *very* differently by the compiler to a pointer to a pointer (`unsigned char **x`). The former points to an actual block of 100 chars; the latter points to another pointer value.
caf
caf: you are right, I edited my answer, thanks!
Michał Niklas
+2  A: 

it is a pointer to a pointer.

It is only a pointer to a pointer. {*}

But the pointer indexing convention means that it is usable for accessing an array or pointers. The first place you typically see this thing is

int main(int argc, char** argv)

and this is an example of using it to index into an array of char*s (the allocation and structuring of which was managed by the OS).


{*}Because an array and a pointer are not the same thing in c. Rather an array "decays" to pointer when used in a pointer context.

dmckee
+4  A: 

A pointer to a pointer and a pointer to an array are NOT the same thing.

unsigned char** p1;
unsigned char* p2[N];
unsigned char (*p3)[N];

p1 is a pointer to a pointer to an unsigned char. It could point to an element in p2, which is an array of pointers to unsigned char. p3 is a pointer to an array of unsigned char. Check out the C FAQ for more details. Edit: Here's a better example.

Steve M
This is really just hair splitting, since C has no such thing as real arrays, anyway, just pointers and [index syntax] that can be applied to them to make the pointers look like arrays. For all practical purposes, they're the same thing.
Mason Wheeler
They are not. `p3` and `p2` are of distinct types. There is no implicit conversion between them, like for the types of `p2` and `p1`. Casting from pointer-to-array to pointer-to-pointer (or vice-versa) will lead to a crashing program.
Steve M
Too late to edit in this example to my above comment: http://c-faq.com/aryptr/ptrtoarray.html
Steve M
@Mason: While it is hard to demonstrate the different semantics of `int *a` and `int b[]`, once you apply more than one dimension it becomes clear. When `int c[5][5]` decays to a pointer it has type `int *`, not `int **`. Also note the different behavior of `c[i][j]` and `argv[i][j]`. The difference is small, and lives in the symbol, table, but they really are different. Arrays decay to pointers, and pointers can be indexed with the array notation, but they are not the same.
dmckee
caf
@dmckee: In fact when `c` decays to a pointer it has type `int (*)[5]`.
caf