Jump to content
  • Advertisement
Sign in to follow this  
bikola_p

Pointless Pointers??

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, i've been coding for 2years and avoided pointers at all costs, anyhow now i have a uni project where i have to write a movie store database, using linked lists. Now, pointers are crucial here, and i have tried hard to understand pointers but I just dont get them. Secondly, how should i have them linked, is the following a good idea?? At this stage, all i know about pointers is they point to the variable rather then storing the value, however dont understand how they are used, manipulated etc. Struct node { Char Movietitle[20]; int year; char movieid[10]; node* next; }

Share this post


Link to post
Share on other sites
Advertisement
You have to write a linked list yourself? Or just use one? If you just have to use one you could store the movie struct by value in the linked list(std::list) and not have to worry about pointers. If you have to implement your own linked list for it then yea you will need to use them. A next pointer is a simple way to do it. Probably more of the C way IMO, but it will suffice for your purposes. It's hard to picture coding for 2 years and not using pointers. No references either? :)

Share this post


Link to post
Share on other sites
Well even in that code snippet he is using pointers without knowing it, the char array.

ace

Share this post


Link to post
Share on other sites
Quote:
Original post by bikola_p
Hey guys, i've been coding for 2years and avoided pointers at all costs, anyhow now i have a uni project where i have to write a movie store database, using linked lists.


Pointers are an essential part of programming in C/C++. Don't be afraid of using them, by avoiding them you're just delaying your own progress. Experiment with them and they will suddenly just click in your mind, and you'll wonder how you could have done without them.

Quote:
Now, pointers are crucial here, and i have tried hard to understand pointers but I just dont get them.
Secondly, how should i have them linked, is the following a good idea??
At this stage, all i know about pointers is they point to the variable rather then storing the value, however dont understand how they are used, manipulated etc.

Struct node {
Char Movietitle[20];
int year;
char movieid[10];
node* next;
}


That looks reasonable, for a singly linked list. I'm reticent to offer much more help than that, this forum does have a rule about 'homework' type questions. You really need to do the work yourself and learn from it, it will be of no use to you whatsoever if we do it for you.

However, we might be able to help clear up any conceptual problems you have with the use of pointers in general.

Share this post


Link to post
Share on other sites
A pointer stores a memory address; it's that simple. A quick example to show this:

int x = 10;
int* p = &x;

First we declare an integer which gets a block of memory, let's say at address 0x0001, and stores the value 10 there. Next we declare a pointer to an integer called p. p can now store an address for an integer (this is important because different types have different sizes). The ampersand (&) in front of the x variable retrives the address of x, so it will give 0x0001 and this is stored in p. Now p holds the value 0x0001. If we were to print out p we would see 0x0001. If we want the value p points to we can use the star (*) to dereference, or get the value that p points to.

cout << p; // prints the address (0x0001 in my example)
cout << *p; // prints the value (10 in my example)

We can also use p to change the value stored in x. If we just use "p =" we would end up changing the address stored in p, but if we use "*p =" we can now change the value p points to.

p = 15; // wrong, we are now storing 15 as the new address
*p = 15; // correct, now the value stored at address 0x0001 is 15
cout << x; // after the last instruction this will print out 15
cout << *p; // also prints out 15

Just play around with a simple example such as this. Make a console program, add in some variables, including pointers, and mess around with changing their values and printing them out. Hopefully afterwards you will start to understand pointers better.

Edit: By the way, you can also print out addresses of variables to compare to address printed out by pointers, just use the ampersand:

cout << &x; // prints the memory address where x is stored

Share this post


Link to post
Share on other sites
Pointers aren't pointless at all. Pointers are just addresses that point to data. Dereferencing a pointer just says, go to this location in memory, return (or modify) the data at that address.

Here is a quick example

void modify(int var){ //modifies the value of a variable
var = 10;
}


int main(){
int z = 5;
modify(z);
}

This does not work correctly if you want z to be changed
a temporary variable is created called var, with its own
unique address. This causes some temporary variable to be
modified, which was not the intention.

To get around this, you need to pass in the address of a variable

void modify(int *var){
*var = 10; //dereference var, modify the value stored at that location
}

int main(){
int z = 5;
modify(&z);
}



Pass the address of the object to modify, a temporary copy
is created (of the address) and the original variable is
modified


arrays and pointers are also closely related. When passing arrays by just their name, what really happens is the address of the 0th element is passed.

So.. Pointers point to an address, always.

For more understanding, I suggest you go play around in DEBUG and type D a few times, and look at how data is stored in memory. The hex addresses point to data in memory, but editing that memory location by storing a diff value will cause the value of the data at that address to be changed. Play around with E to know what I mean, but dont press G whatever you do(your cpu might get injured).

Share this post


Link to post
Share on other sites
Ask yourself this: How would you write this movie store database without using pointers? How would you store a completely arbitrary (could be 10, could be 10 thousand) number of records efficiently?

Share this post


Link to post
Share on other sites
Assuming there is no need for your own list, check out using Boost libraries as well. It offers a few more options for pointers and using them safely. But then... since its a university project ignore all this. And good luck :o)

BTW, and perhaps others can comment on this. But arent pointers being used less these days in favor of references? At least where pointers arent the only way to do something (such as in a list)?

Share this post


Link to post
Share on other sites
Quote:

BTW, and perhaps others can comment on this. But arent pointers being used less these days in favor of references? At least where pointers arent the only way to do something (such as in a list)?


Not really. Pointers and references (in C++) are similar -- especially in their underlying implementation in many cases -- but they still are two distinct features that just happen to overlap a little, and thought needs to be given as to which to use in every case. You shouldn't just blanket-replace pointers with references whenever you can (for example, where function output parameters are concerned, references can be misleading to the caller).

References, also, must be initialized and cannot be re-initialized to reference a new value, which makes them rather useless in the context of linked list implementation. ;)

Share this post


Link to post
Share on other sites
Thanks for the help guys, i understand and played around with them.But in my example after re-reading wouldnt i need, 2nodes, a head and a next node. So next points to the &head. and the next of that structure, would point to the &head of the next structure.??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!