Sunday, July 12, 2009

C++ pointers question?

I understand points and stuff but where would I use it? they seem useless and seem like something I would never use.





can you give me some examples and maybes some codes to help me understand when I would use them?





Thanks,


S_R_S

C++ pointers question?
Pointer code is more educational than practical. You really need to fully understand the usage of memory and pointers to store values in order to write efficient code and manage memory correctly.





My recollection from school is that most pointer operations were performed in lists, data collections, and sorting methods. These are high level ops that will already be written into the pre-existing utility libraries that you will frequently use. But you really need to understand what is going on under the covers.
Reply:We can define a variable in C++ to store a memory address. A pointer in C++ is said to "point to" the memory address that is stored in it. Also, when defining a C++ pointer variable, we must specify the type of variable to which it is pointing. For example, to define a pointer, which will store a memory address at which exists an int, we can do the following:


int *p,a;


here a is a ordinary variable,p is a pointer variable to store the address of a.


a=5;


p=%26amp;a;


where a contains the value 5 and p contains the address of a(memory address)


here '%26amp;' is a 'reference' or 'address operator'
Reply:Scott, C++ is a language of pointers. It's probably difficult for you to understand this now; however, if you keep at it, you'll eventually see the power of pointers. All the crap you're learning about classes are in fact teaching you about pointers.





Example. We have a class called location. In the location class, we have GPS Latitude and GPS Longitude and other stuff.





class location


{


DOUBLE GPSLat;


DOUBLE GPSLong;


DOUBLE Altitude;





VOID function1(....)


}





From the location class, we'll derive a Ship class and Human class.





class Ship::location


{


DOUBLE heading;


DOUBLE speed;


etc.


}





class Human::location


{


BOOL Man;


DOUBLE Height;


DOUBLE Weight;


etc.


}





From our main program we have five boats (from ship) and four Eskimos


from human.





Ship boat[5];


Human Eskimo[4];





main()


{


......





// somewhere in here we want to print the location data


// of a ship or an Eskimo. It'll be user or programmatically


// selectable





location* myTarget;


myTarget = %26amp;boat[4]; or myTarget = %26amp;Eskimo[0]; or, etc. (SOME INPUT)





printf( "%f, %f", myTarget-%26gt;GPSLat, myTarget-%26gt;GPSLong );





Note how the printf function does NOT need to change. It will


work on all ships or Eskimos and it does not include even an index


into the arrays. You would obviously make a more meaningful


routine than just a printf.





Hope this helps. (C++ in one minute).
Reply:Pointers are not easier to understand but they are much easier to understand if you go back to the C programming language. C was written originally on computers so simple you can buy calculators today which are more complicated and have more memory. For that reason, a lot of under the hood stuff was and is pretty much out in the open. An example of this would be pointers and arrays. Everything in C, we are told, is passed as a value except arrays. These are passed by the address of the first element Array[0]. In practical terms, this means when you pass an array to a function what you have is a pointer to that array, on which you can (though it's not recommended) do pointer arithmatic. You are better off creating a temporary pointer, assigning it Array[0]'s address and using that because it will help you keep track of things. With C++ you can use the %26amp; operator to pass anything by reference, and it is certainly less cumbersome, but the reason C code is supposed to compile into such fast and efficient machine code is it stays close to what the hardware is actually doing and what you are trading off is readability for efficiency. Read Kernighan and Ritchie's The C Programming Language even if you are not particularly interested in programming in C. You'll find source code for many functions in cstdio and cstlib which use pointers, and frankly which if you come to understand (and these guys are good at explaining) will help you understand pointers better than anyone at Yahoo! Answers

survey software

No comments:

Post a Comment