Pointless Pointers??

Started by
16 comments, last by uckevin111 18 years, 7 months ago
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; }
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? :)
Well even in that code snippet he is using pointers without knowing it, the char array.

ace
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.
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
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).
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?

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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)?
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. ;)
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.??

This topic is closed to new replies.

Advertisement