As my first entry I would like to say I am excited to be in this class and if my Unix/Linux online experience says anything, I believe I will be learning necessities that will aid my pursuits of the future. I've been involved with programming/scripting on and off since I was in high school and I must say that I have gotten most of the basics to a science. My hopes are to find better and new ways to create intricate and reliable programs build upon the concepts that I am quite familiar with. As a future video game programmer “data structures” sounds like a class that will be able to bring me a step closer without being a gaming directed course and even if it wasn't I am certain I will find many uses for the knowledge. So for the first week I am reflecting on my past programming exploits and attempting to refresh my memory of concepts I understand. The only worry I have is pointers… its not that I don't understand them but I do find myself having trouble knowing when to really use them which has lead to me avoiding them like the plague in the past.
All in all I say… LET THE FUN BEGIN!
This week I worked on and off on the data types assignment.. At first I thought it was going to be too simple, I was wrong. I actually found myself quite frustrated at times, but slowly I found ways to solve my dilemmas. I used and understood the template given to me for a starter and used it to the best of my current ability. Its not that I'm having trouble with C its just that when I was taught C it was in a completely different way and it wasn't super in depth on any one thing. Needless to say I already understood data types and sizes I just needed to get things to work right and slowly I did. The hardest part was the signed types because I couldnt for the life of me get them to come up right, this was finally solved using casting. The only thing I didnt get on the project was the quantity part, it just didnt want to work, I feel maybe I misunderstood how it was trying to work but after a lot of headache I can say I wont be forgetting the things I learned in this oddly hard assignment lol..
As for the questions at the end ill answer them here…
1. Long int and long long int are the same, but this can change because its implementation specific.
2. A&B. printf displays to the stdout which is the screen by default, fprintf lets you choose where it sends the data. C. each of the %s, %hhu, %hu are for printing output of different data types. D. %u and %d are for signed and unsigned. E. The 13 in %13s is the field length of the printed string.
3. If sign was left unspecified it would be automatically unsigned.
4. A. The & and | are the AND and OR operators. B. I used them to set the bits in the data types to all 0s and all 1s.
5. I experienced a lot of trouble with this but mostly due to lack of experience, but I know the last 2 data types may have experienced a problem because of the required field length to display the value.
6. Total bits for… Signed char: 8 bits… unsigned short int: 16 bits… unsigned int: 32 bits… signed int: 32 bits… long long int: 64 bits.
There was a lot of pointer review aswell and new uses for pointers in this weeks assignment. First off id like to say I thought that youtube video about pointers was both funny and extremely helpful, heres the link.. http://www.youtube.com/watch?v=5VnDaHBi8dM The assignment started by mentioning the defference between using a struct and using a struct pointer..
non-pointer instance:
struct rectangle box; box.length = 12; box.width = 10; box.area = box.length * box.width;
pointer instance:
struct rectangle *box; box = (struct rectangle *) malloc (sizeof(struct rectangle)); box -> length = 12; box -> width = 10; box -> area = box -> length * box -> width;
Theres is quite a distinction betwen them, the main one being how to set and use the elements of the struct. For a standard struct id use the “.” while with a pointer struct id use “→”. We mainly worked with the concept of nodes in this assignment so ill go into detail on those…
struct node { int value; struct node *next; };
This is essentially the style of node I used throughout the practice and the program I wrote. And as an added note for this using “typedef struct node Node;” I can set a stuct node just by declairing it as a “Node”.
The Idea of nodes is to keep connecting them through the *next struct pointers, I am essentially able to make an unlimited amount of nodes connected to each other.
Before continuing with this concept I should remind myself that when setting up any pointer there should always be a destination its pointing too and memory must be allocated for it in advance. The command for this is.. = (Node *) malloc (sizeof(Node)); for each time I need it. In the case of the nodes the “first” node will be allocated and then each instance of *next that is used will also need that memory allocated.
A few reminders of how these are used from the assignment below for reference..
to assign:
first -> value = 12;
to retrieve:
printf("first's value is %d\n", first -> value);
dealing with next– set it to an initial sane value:
first -> next = NULL;
Finally the big problem when making a bunch of connected nodes is how complex the access of the data can be.. without making a small change it can become a big chain of “→” just to get the data which can make a program very bulky and possibly not functional. The solution to this is making a temperary placeholder node pointer “tmp”. This “tmp” is used to follow the path of *next each time so it marks the node I am currently working with. This is not only used to make the nodes but also to scroll through them in a program if I wanted to display the value at each, going one by one. In that case id just set the “tmp” back to the first node and then have it follow the node path again. I find this very useful and an amazing trick!
Lastly…
This is my node chart example, it is a list of linked nodes and the values stored at them. Each arrow along the way is named next and are pointers to the next node. On the node before null is the location of the “tmp” as it was located at the last spot that would receive a value.
This week has been a lot of time spent really grinding into pointers.. It was bittersweet and took many many man hours but in the end I feel I've made progress I once thought not possible.. (getNode being the hardest), and also I covered many user input scenarios that I don't think were even required this time haha.. This was all possible largely due to the very helpful and detailed response to my mail from Matt.. I am very thankful. I'm not sure what to write this week because I've spent sooo much time working on this that its all meshed together. But ill say that I am really seeing why pointers are used. They allow so much to be done in a function and in the case of a double pointer allow a function to return essentially the entire pointer where it usually can only return a single value. All in all I have a lot more to do with this project for next weeks version and I'm both excited and nervous haha..