123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|562|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> c pointers?

Thu, 30 Dec 2010, 08:01
spinal
I'm trying to write a simple function to load a file into memory, however I have never really understood the use of pointers. Can someone tell me, is the following anywhere near correct?



-=-=-
Check out my excellent homepage!
Thu, 30 Dec 2010, 11:08
JL235
At a glance is looks fine to me.
Thu, 30 Dec 2010, 11:43
Afr0
Pointers are 'relatively' easy (as Einstein would have said it).

A pointer is an address in memory. Or rather an address to something in the memory. You create a pointer by going int *MyPointer.
To get the address of a pointer, use the AddressOf operator (&). This is mostly used to pass function arguments by reference, meaning they don't have to be returned because any change you do inside a function is reflected in the variable.
I'm not entirely sure, but I think when you use the AddressOf operator to pass a variable by reference, it stops being the AddressOf operator and starts being the Reference operator.
To dereference a pointer, use *MyPointer. When you dereference a pointer, you get access to the data stored AT the memory location pointed to by the pointer. The dereference operator has to be used in conjunction with the Right Arrow operator (->) in order to access the data.
Example:

*MyPointer->MyData

So in essence, the Dereference operator gets the address of the pointer, and the Right Arrow operator gets the data stored at the address.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Thu, 30 Dec 2010, 12:26
Jayenkai
My issue with pointers is that they always get explained exactly like Afr0 just did.
Much like the BlitzBasic example of Types being something that really should've actually just been an Array, the amount of text above just becomes a bit of a blur in my head when I try to read it.

As such, I tend to ignore all this stuff, and stick with alternative methods.

Some day I'll learn this stuff.


...Today isn't that day.

-=-=-
''Load, Next List!''
Thu, 30 Dec 2010, 12:52
Afr0
It's all about the mental model.
If you have a mental model for a particular concept, you've nailed it.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Thu, 30 Dec 2010, 13:20
Evil Roy Ferguso
You're pretty close, honestly. The main two things that offend my delicate sensibilities are:

1. You don't need to loop and read in data value at a time. One of the arguments to fread is the number of elements to read in:



should work. The address_of_data + 1 is because you've put "size" into the first element of image data - address_of_data is effectively a pointer to the first element in that "array", so incrementing it by one gives you a pointer to second element.

2. You need to fclose() your file after you've finished reading, or the chance of bad things happening will be much larger than 0%.

|edit| d'oh, missed the wrong return type. HoboBen caught it, though.
Thu, 30 Dec 2010, 13:20
HoboBen
Are you sure the file starts with an int declaring its size?

You're defining address_of_data as int pointers, but fread'ing bytes. Though that shouldn't cause any actual bugs.

(you can also fread straight into your data in one go and omit the loop)

You're returning the int* address_of_data, so the function definition should be int* readFile.

Other than that, try to compile and test it and work from there.

edit: I don't see where imageData is defined!

-=-=-
blog | work | code | more code
Thu, 30 Dec 2010, 14:05
spinal
A small error on my part...

imageData = address_of_data




-=-=-
Check out my excellent homepage!
Thu, 30 Dec 2010, 15:54
Cower
Ignore what Afr0 wrote, because he's wrong about how the operators work. You do not need to use * and -> in conjunction. In fact, it probably won't work most of the time unless you use a lot of pointers to pointers. The only time you would use *a->b in conjunction is if a was something like a Foo** and b is a member of that type.
Thu, 30 Dec 2010, 16:05
Afr0
The only time you would use *a->b in conjunction is if a was something like a Foo** and b is a member of that type.


That is actually correct.
I forgot that this was about C pointers.
In C++ you use the Right Arrow operator alot in conjunction with the Dereference operator to access members of classes that are pointed to by a pointer.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Thu, 30 Dec 2010, 19:16
Evil Roy Ferguso
In C++, unless interfacing with code in C, pointers tend to mean you're doing it wrong, since you should be using references or smart pointers. Pointers to pointers don't tend to come up all that often.

Also, pointers to members:

Huzzah D:

Fri, 31 Dec 2010, 03:18
Afr0
That's not how I do it.
I usually have a pointer to a class, and use the Right Arrow operator to get its member.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Fri, 31 Dec 2010, 10:10
Evil Roy Ferguso
That's something else entirely.

Mind you, that something else is overwhelmingly more useful. Pointers to members are a strange and fascinating (mis?)feature of C++. If I had another Foo named bar, later on I could:



That is, it's a pointer to a member of some object, not a pointer to an object in and of itself.