www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/08/14/11:23:12

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ...
Date: 14 Aug 2000 12:34:17 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 44
Message-ID: <8n8p09$dk6$1@nets3.rz.RWTH-Aachen.DE>
References: <39940CD1 DOT 5691 DOT 11C46C5 AT localhost> <yBEl5.2112$By6 DOT 30353 AT news1 DOT online DOT no>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 966256457 13958 137.226.32.75 (14 Aug 2000 12:34:17 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 14 Aug 2000 12:34:17 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019