• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [OT] A bit confused on pointers...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] A bit confused on pointers...


  • Subject: Re: [OT] A bit confused on pointers...
  • From: Andrew Farmer <email@hidden>
  • Date: Wed, 28 Dec 2005 16:10:20 -0800

On 28 Dec 05, at 15:20, Sanri Parov wrote:
> I've got a file read with:
>
> fread (buffer, 1,size,pFile);
>
> in a for() cycle that reads several files. "buffer" is a char* and so
> another variable called finalBuffer (char*)
>
> All I want to do is to costantly update finalBuffer so it's the sum of
> all the single buffer from every single file.
>
> finalBuffer +=*buffer
>
> is the right choice?

No. No no no no no.

*buffer isn't the size of the buffer, or the contents - it's the first
character of the buffer.

ASSUMING THAT:
1] buffer points to a memory block large enough to contain all the data
you're going to read at a time
2] finalBuffer is large enough to contain all the data you'll read,
overall

THEN, some reasonable code would be:

| char *bufferP = buffer;
|
| for(...)
| {
| size_t result = fread(bufferP, 1, size, pFile);
| if(result == 0)
| {
| handle the error and break out of the loop
| }
| bufferP += result;
| }

Remember that this will crash and burn if bufferP is too small to
contain all the data you're reading! Some more context might be
useful - what is this data being read from? Can you trust the source?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
References: 
 >[OT] A bit confused on pointers... (From: Sanri Parov <email@hidden>)

  • Prev by Date: Re: looking for a cocoa programmer ( paid job )
  • Next by Date: Re: Bezier Paths.
  • Previous by thread: [OT] A bit confused on pointers...
  • Next by thread: Re: [OT] A bit confused on pointers...
  • Index(es):
    • Date
    • Thread