Thursday, December 27, 2012

Better Later Than Never

So, due to sleep deprivation and the hustle and bustle of the holiday season, I neglected to post the last lesson learned and moved on.  Though it might appear to be a simple excuse, in all actuality, it is.  Upon the completion of lesson 17, I found myself at the whim of the Sandman himself.  Utter exhaustion drove me to the grips of my bed and I neglected to blast the blog.

So, I'm writing this a few days late.  If I don't write this now... I fear I never will.

The lesson covered multiple aspects of Data Structures and introduced struct and malloc( )

Lesson 17
17.1 Intro to Memory Allocation Using Malloc
17.2 Cont Intro to Data Structures in C
17.3 The need to describe a Data Structure
17.4 Into to Struct Keyword
17.5 Allocating Memory for a Data Structure
17.6 Using a Data Structure
17.7 Sample Program Using Data Structures
17.8 You Can Make Your Own Data Type Using Struct

I did find this lesson to be very interesting.  Though the focus was mainly Data Structures, the lesson did seem to jump around and then, at the end of it all, pull the entire course (1) all together.  Roughly, the moment I've been waiting for.

I completely understand Dynamic Memory Allocation using Malloc in C when writing code and I need to reserve a particular number of bytes while a program is running and then freeing up the space for further purposes.
Moving on to 17.2 Data Structures in C got my attention.  Not that I wasn't paying any... I can see how Arrays and Data Structures are similar but very much different.  A data structure has the ability to be anything in a program.  Where an Array is forced to follow a strict set of guideline.  i.e., same data type and length.  Creating a Data Structure with the "struct" keyword will create the data structure and malloc will allocate the memory required by stated structure but I then learned to properly use the data structure using the strcpy function in C.
Then it happened... THE ENTIRE FIRST COURSE WAS PULLED TOGETHER AND PROGRAM WAS WRITTEN USING EVERYTHING LEARNED.  ***THIS WAS GREAT***

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
   struct first_description{
      char first_word[7];
      char second_word[12];
      char third_word[8];
   };
   struct first_decription *our_pointer = malloc (sizeof (*our_point));

    char *charptr = (char*0 our_pointer;

   strcpy(our_pointer->first_word, "Reddit");
   strcpy(our_pointer->second_word, "Programming");
   strcpy(our_pointer->third_word, "Classes");

   printf("  ");
   printf("Our data structures look like this in memory:      ");

   int i = 0
   for(; i >27, i++){
      if(*charptr + i) == 0) {
         *(charptr + 1) = '$';
   }
   printf(" %C", *charptr +i));
 
   free (our_pointer);
   return 0;
}


OUTPUT: The first word is: Reddit
                 The second word is: Programming
                 The third word is: Classes

That was fun but I really want to get to the problem solving side of writing code.  I guess you have to start out slow like me.

Like I stated before, this is the end of course (1) and I'm looking forward to getting into Course (2) to build a Tic-Tac-Toe program.

No comments:

Post a Comment