Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   C++ pointer help (http://zelaron.com/forum/showthread.php?t=38671)

sciencekid 2006-02-05 02:29 PM

C++ pointer help
 
I'm just starting C++ and i do have a for dummies book on the subject so at least im covered there, anyway, i read the book(not all the way though, but I got past those parts) and I don't understand the point of using pointers and much less how to use them. can anyone help me? i'd appreciate it a lot. thanks!

Acer 2006-02-06 12:09 PM

from my experience, do any of the excercises they tell you to do with pointers, its one of those things you really learn by using it. like i learned the point of them from when i use to make cheats for CS... but yea

Demosthenes 2006-02-06 03:43 PM

Check this out: http://www.daweidesigns.com/pointers.php

And as Acer said, you will use pointers for many different reasons. For now, just learn how they work, you will soon see their usefulness. They are essential in making certain data structures, as well as many other things.

sciencekid 2006-02-06 11:09 PM

ok, what about this peice of code, what is it saying(from the "for dummies" book):

--------------------------------
#include <stdio.h>
#include <iostream.h>

class Student
{
public:
int semesterHours;
float gpa;
float addCourse(int hours, float grade){return 0.0;};
};

int main(int argc, char* pArgs[])
{
// create a Student object
Student s;

// now create a pointer to a Student object
Student* pS;

// make the Student pointer point to our Student object
pS =& s;

return 0;
}
------------------------
even w/ the comments, i still don't get what each pointer symbol means/does

Medieval Bob 2006-02-07 06:52 AM

pS is a pointer that, as its value, has the address of s.

In that context, it's pretty useless. What you're going to be using it for in the beginning is passing it to functions. Since, if you pass a variable, you can only change it in the main function if you actually return it, pointers will become useful when you want to change more than one variable in a function.

If you pass the pointer, you can change the value of the pointer (which you'd never really want to do in this situation), and, more importantly, you can change the value of the variable that the pointer points to.

Really, just work these basic examples, and it'll become evident soon enough.

sciencekid 2006-02-07 11:17 AM

all right then, i'll try to practice it. but one more question..... mjordan2nd's link says that the pointer point to an address in the memory, right? but you say that it has to do w/ the variable's value, right? so which one is right? :confused: they can't both be correct, right? so which one is it? and sorry for being a pain in the rear w/ all this confusion i have. i just don't catch things so easily sometimes :confused: :( thanks for your help guys.

Demosthenes 2006-02-07 04:56 PM

They are both correct. A pointer holds an address in memory. If you outputted the value of a pointer, you would get a memory address, which pretty much would look like random garble. However, you can use a pointer to change the value of a variable as well. By using the correct command, you basically say change the value of the address a pointer is pointing to. For instance, lets say pointer i points to variable x, and variable x's location in memory is 1234:5678. Lets say x is initially 10. If you wanted to change x's value to 20, you could do so using the pointer like so:

Code:

*i = 20;
What that code does is it says, go to the address that i is holding, which also happens to be variable x's location, and change the value that address i is holding to 20, which therefore also changes x's value.


All times are GMT -6. The time now is 07:49 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.