0

I am confused as to how to approach problem set 5. I have watched the lectures and shorts but I am still confused on the subject of pointers. Are there any additional CS50 videos or walkthroughs that explain pointers?

My pseudo-code for load is as follows:

open dictionary
for each word in dictionary
   while not at the end of the dictionary
       malloc a node
       point to node

I'm also having trouble understanding how to work with files and pointers. I saw that, in the videos, they used different variables such as node*, *node, and &node. What do these variables mean, and are they used for different purposes? Thanks in advance!

2
  • 1
    There's a new walkthrough to this problem set. Have you seen it yet? Maybe that will clear all your doubts: youtube.com/watch?v=0x9rSfV85g8 Oct 20 '16 at 22:22
  • @Yuri Thank you for giving me the link to the video. I'm thinking of using tries to load the dictionary. I'm still a little confused, though. How exactly do I iterate through each word in the dictionary? Surely I can't do strlen(dictionary), so what else should I do? Oct 21 '16 at 17:16
1

Well, first off let's address the immediate question.

'node *' is how you will declare a variable that is a pointer to a node.

Pointers are really hard, so you'll have to spend a bit of time to understand it, but when you create a pointer, what fills in that variable are (in my understanding) characters which make up an address. When you use malloc, what malloc returns are characters to an address. So a line like:

node * example = malloc(sizeof(node));

first declares a variable, example, which is of type 'pointer to a node', and then assigns it (ie makes it equal to) an address returned by malloc. Malloc will create a chunk of memory to the size of node.

So now in memory you have

example: 123fakestreet - the address will actually look something like 0x633000 but don't worry about it.

Then if you go to

123fakestreet: memory with null / garbage data that is the size of a node.

Now let's talk about * node - you can't really * a node itself - * 'dereferences' a pointer which means it goes to the address and sees what is there. So the thing you have to put after a * has to be a pointer in this instance. I'm not sure what will happen if you don't - probably a segfault or an error.

When you dereference the pointer you would write it as:

*example.field = 5;

the *example part essentially tells the computer to go into the memory at the address pointed to by example (remember, the 'example' variable itself is ONLY the address itself). Because nodes are structs, I've arbitrarily assigned a 'field' to it, which could be anything but I've made it an int.

So basically star in front dereferences and must be used with a pointer.

In terms of how to start PSET5, it's not easy - you'll spend a lot of time wrestling with it - it's much harder online given that it can take hours / days to get basic questions answered.

The best advice I can give (not having finished it myself) is to keep going, keep reading stack, keep asking questions and watch the overviews / videos a few times. The concepts take a while to build.

I'd also suggest reading and going through the examples on LinkedLists described in the course: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

1
  • Thank you for this answer! This helped me understand the *node better. I'm still worried about finishing this on time and getting it right, but this helped me. Thanks! Oct 21 '16 at 17:19
1

To the best of my knowledge, there is no difference between node* and *node. The & is the "address of" operator. If you have a variable declared as a pointer, it can point to &node. If it's a regular variable, it would equal the address of node, which would be some random looking number... rather than point to the address itself.

I was recently looking at some tutorials on linked lists that may be able to answer your question more thoroughly, if you're unable to find the info in the CS50 tutorials, or just need some additional method of learning.

It's like everything else in C - easy to understand once you understand it. Until then, it can be a little fuzzy.

This will explain the * and the & more: http://www.tutorialspoint.com/cprogramming/c_pointers.htm

This talks about *nodes: http://www.exforsys.com/tutorials/c-language/c-linked-lists.html

2
  • Thank you for this answer. I'm grateful for the many people that responded so quickly. I'm still a tad confused on iterating through a trie and exactly loading the dictionary, but I have a better idea for what to do. Thank you! Oct 21 '16 at 17:18
  • 1
    Great @CoderGirl101. Thanks for the update. :) Oct 22 '16 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .