Mail Archives: djgpp/2000/08/14/11:23:12
Vermin <ratspl AT hotmail DOT com> wrote:
> but how should I use the array in the procedure??
> Let's say that I wanted to fill it with a byte stream from a file, how would
> I then use it??
> something like:
> void readStream(char fileName[], unsigned char **memPos){
> ..open file, get length of byte stream etc..
> *memPos = new (unsigned char)[streamLength];
> for (int i = 0; i <= streamLength; i++){
> // I know that I should use only one read for it all, but this is just
> an example...
> fread(memPos[i], 1, 1, inFile);
No. Make that
fread ((*memPos)+i, 1, 1, inFile);
I know this syntax looks weird, but it's necessary in C. 'memPos'
itself is a pointer to pointer to char, so '*memPos' is a pointer to
char. This you can then access via the array notation ()[i], or like a
pointer by adding the offset i. Or you may feel more comfortable with
the 'long form' notation:
fread(&((*memPos)[i]), 1, 1, inFile);
Which can be read as: "the address of element [i] of the array at
address *memPos".
The "memPos[i]" you wrote would also be a pointer to char, but it
spells out as:
*(memPos + i)
Note the difference in parentheses between this and the correct code,
above.
In C++, it might be possible to use a reference argument 'unsigned
char * mempos&' or whatever the proper syntax is...
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -