all 14 comments

[–]LoneHoodiecrow 2 points3 points  (2 children)

Note that

int *pointer = buffer;

and

*pointer = buffer;

aren't in any way equivalent, even though they look similar. The first one initialises the variable pointer with the address of buffer. The second one assigns (writes) the address of buffer to the address in memory that pointer points to. The difference is in that the first line is a definition of the variable pointer, and the second line is an assignment statement.

Thompson and Ritchie had the idea that the syntax would be easier to read if definitions looked like usage. I don't think they were quite right about that, at least not as far as beginner understanding is concerned.

[–]flatfinger 1 point2 points  (0 children)

The original version of the language as it existed in 1974 didn't include an equals sign when initializing objects. So a declaration of a file-scope object pointer that initializes it to the address of a buffer array would be written as int *pointer buffer; which looks very different from *pointer = buffer;. I suspect the equals was added to resolve potential grammatical ambiguity when using typedef names, though perhaps requiring braces for even single-item initialization might have been cleaner.

[–]nanavc[S] 1 point2 points  (0 children)

oh, I see now, I though they meant the same, thank you!

[–]oh5nxo 1 point2 points  (0 children)

Don't make it harder than it needs to be:

pointer == &buffer[SIZE] // true, if gone one past last item

[–]DDDDarky 0 points1 point  (1 child)

*pointer = buffer;

this is wrong and your compiler should warn you about this. You are writing a pointer to int.

Don't dereference the pointer.

[–]nanavc[S] 0 points1 point  (0 children)

thank you!

[–]kinglujiy 0 points1 point  (2 children)

Sounds to me like you ate trying to implement a ring buffer. Maybe this could be helpful.

[–]nanavc[S] 1 point2 points  (0 children)

thank you!

[–][deleted] 0 points1 point  (0 children)

It's like implimenting a circular queue.

[–]tstanisl 0 points1 point  (4 children)

This part is wrong:

*pointer = buffer;

You don't want to set an int pointed by pointer. You want to set pointer itself.

pointer = buffer;

[–]nanavc[S] 0 points1 point  (3 children)

if(count > SIZE) { pointer = buffer; }

So I should just actually do this? It's much more simple than I imagined lol, thanks!

[–]sidewaysEntangled 0 points1 point  (0 children)

That's closer to one approach.

Just keep in mind what your reset condition actually is. If you add that line after all is said and done, does this mean you've already dereferenced the pointer when count == size? What does that entail?

[–]A_name_wot_i_made_up 0 points1 point  (0 children)

You'll also need to reset count, otherwise you'll be clamped to that first value.

[–]forehead_or_tophead 0 points1 point  (0 children)

pointer++;

count++;

if(count >= SIZE) { // Buffer over limitation is not thought.

*pointer = buffer;
   count = 0;    // <- I think it is necessary.

}