Why are pointers frightening?

Started by
42 comments, last by Beer Hunter 19 years ago
I think you mean:
void fun();typedef void (*ffun)();ffun = fun;int main() {   ffun(); //here you're actually using the function pointer}void fun() {    printf("heeeelllllo");}

Beginner in Game Development?  Read here. And read here.

 

Advertisement
As a Java programmer, pointers fascinate me. On one hand I fail to see the point because I've never had a need for passing a primitive value by reference. On the other hand, I probably don't know what I'm missing out on. How does C++ handle pointers with objects? If objects are references, then they already are pretty much pointers, just like in Java. Do pointers only apply to primitive types?
Objects are not references in C++ unless you tell them to be.
void Function(){   Tree tree1;                 //object of type Tree, located on stack   Tree& treeref = tree1;      //reference to tree1   Tree* tree2 = new Tree();   //allocating Tree object on heap   tree2.grow();   delete tree2;               //deallocate, otherwise it's a memory leak}


Passing a pointer to an object and a reference to an object are very similar. References are a bit safer, as they're more likely to be a valid object.
hey guys... this might be a bit off topic but it is related to
pointers.

Why isnt this code working?
I want "p" to point to the address that "add" contains,
and the location it points to should have the value of "val".
void write_loc(char val, int add){	char *p;	p = add;	*p = val;}
Quote:Original post by AcePilot
hey guys... this might be a bit off topic but it is related to
pointers.

Why isnt this code working?
I want "p" to point to the address that "add" contains,
and the location it points to should have the value of "val".
void write_loc(char val, int add){	char *p;	p = add;	*p = val;}


void write_loc(char val, int add) {char *p;*p = &addp = val;}


Anything else?

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Quote:Original post by Nice Coder
Quote:Original post by AcePilot
hey guys... this might be a bit off topic but it is related to
pointers.

Why isnt this code working?
I want "p" to point to the address that "add" contains,
and the location it points to should have the value of "val".
void write_loc(char val, int add){	char *p;	p = add;	*p = val;}


void write_loc(char val, int add) {char *p;*p = &addp = val;}


Anything else?

From,
Nice coder


Partly correct.
If p is a pointer to char, then you can't say p=val either.
So, to make p point to the address where the add variable is, you'd do as the quote shows with the '&'. That sign is the "address of " operator. Then the second line would be
*p = val;
instead of what the above poster said. You had that line right. The "*" in that line says that the value that p points to = val. Without the "*", you are trying to set the address of the pointer to the value in val. That is a NONO, for sure.



Quote:Original post by JeremyWB
I notice how you declared that pointer, *intptr. I never knew you could just put it anywhere off to the left, like...
int* intptr = array;
int * intptr = array;
int *intptr = array;

I think I even recall seeing some books place the * to the right of the varaiable. And that reminds me of the increment and decrement operators. In some old book, you could put it on either side of the variable and it would behave slightly differently, like in a while loop . . . if you used ++x, it would perform the operation first and check the value of x second, and if you used x++, it would check the value of x first and perform the operation next. Now all I ever see is ++ to the left of variables. Was this changed or something? Sorry, I know it's off topic, my brain works weird.


C/C++ don't care about whitespace for the most part (there are a few rare exceptions), but it does care about the order in which things appear. You can't put the * to the right of the variable name, and which side of the variable increment and decrement (++/--) appear on is very important in some cases.

If the increment/decrement is on a line by itself, or part of a for statement, it makes no difference, but when used on lines of code that also perform other operations it's important to consider. ++preincrement and --predecrement change the value of the variable *before* processing the rest of the line on which the operand appears. postincrement++ and postdecrement-- perform the line's function with the original value of the variable, THEN change it accordingly.

Back somewhat on topic, to clarify the previous poster's response, as I understand it, the correct set of statements is:

char *p;p = &add*p = val;
Quote:Original post by JeremyWB
Quote:Original post by Android_s
Or to just dynamically create an array.

...


Heh, the book I was reading before this one stated that making a mistake could do anything, even erase your hard drive. I don't know if that is true, but he was obviously trying to enforce discipline when it comes to using pointers.


Hehe, well many books do...A good thing is to think twice when using pointers. It's often worth it =)

Quote:Original post by JeremyWB
Now all of this you're describing, I don't understand it completely, but that's because I haven't gotten to arrays yet. Like pointers, I am not yet aware of what purpose arrays serve. If I were to guess, I would assume it's just better than initializing a crap load of variables of the same type, like int x1, x2, x3, x4, x5, etc. Instead, why not use int x[5] = {1, 2, 3, 4, 5}; and I *think* I recall, faintly, that it would start at 0, not 1 (like, x[0] = 1, x[1] = 2) and end at 4. . .


That is absolutely correct, and is one way of using arrays. You can actally use arrays in a number of ways, storing C-style strings, building matrices for mathematical calculations etc. etc... =)

You use dynamically created arrays(an array created with new, there's an example in my previous post) for data that you don't know how many elements there is at the program start, let's say for example user inuts, number of characters in a file and...well the list goes on =)

Quote:I'm sorry, I have a lot to learn Master Jedi!


The learning never end ;)
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Quote:Original post by Fruny
void function(int **param[]) is exactly the same thing as void function(int ***param)


which is the same as void function(int **param[3]) but not the same as void function(int **(param[3]) [wow]

Quote:Original post by Kevinator
As a Java programmer, pointers fascinate me. On one hand I fail to see the point because I've never had a need for passing a primitive value by reference. On the other hand, I probably don't know what I'm missing out on. How does C++ handle pointers with objects? If objects are references, then they already are pretty much pointers, just like in Java. Do pointers only apply to primitive types?


C++ doesn't make as big a distinction between objects and primitives. Consider:

MyClass myObject;

In Java this is a reference (essentially a C++ pointer) to an object of type MyClass. In C++ it's the object itself (as if the object were a Java primitive).

And if that weren't confusing, consider references vs. pointers in C++. References and const pointers are essentially the same:

MyClass &myObject
MyClass * const myObject;

The difference is that the first cannot be NULL. Also, you assign an object to the first, but an address to the second. However, const pointers are different from poitners to const:

const MyClass *myObject;
MyClass const *myObject;
MyClass * const myObject;

The first two make pointers to const, the third is a const pointer. It is illegal to modify a const pointer but legal to modify what it points to. It is illegal to modify what a pointer to const points to but legal to modify the pointer itself. This also means that:

const MyClass &myObject
MyClass const &myObject
const MyClass * const myObject;
MyClass const * const myObject;

are all essentially the same.
Quote:Original post by Anonymous Poster
which is the same as void function(int **param[3]) but not the same as void function(int **(param[3]) [wow]


Err... that should be:
which is the same as void function(int **param[3]) but not the same as void function(int **(param[3])) [wow]

This topic is closed to new replies.

Advertisement