Friday 20 September 2013

Friday night - wine and programming! Teaching myself pointers in C

I made this blog for a different class... but I figured I might as well post other stuff on blogger now that I have it going.
I suspect this will turn up in a few search results too.

In one of my digital classes we are reviewing C programming practices, and one of the hard concepts to get is pointers. I thought I would sit down and play with them for a while to figure it all out.
This is what I came up with:

Sample program:

//========================================================
//== The purpose of this C program is to simplify the
//== mystery behind pointers with a minimal amount of
//== other crazy stuff going on at the same time.
//==                    
//== Compiled under gcc on linux, ver. 4.7.2
//== Sept. 20, 2013
//========================================================
#include <stdio.h>
#include <stdlib.h>

char msg[] = "I like cheese"; //declared here to give global scope

int main ()
{       
    char msg2[] = "CHEDDAR ROCKS!"; //declared here to put it on the stack.
                                                                 // -= only visible in the main =-
           
    char * string = msg; //create variable of type address to hold base address of msg           
    char * string2 = msg2;   
   
    printf("\n\nA string stored on the activation record: \n");   
    printf("%s at address %p, the first character is %c \n", msg, msg, *msg);
    //above code uses format specifiers for showing a string, an address
    // and the contents at the first memory location for 'msg'.
   


    while (*string != '\0')    //loop increments until the examined memory address contains
                                          //null character (\0)
    {
        printf("address: %p, Character %c \n",string, *string);    //shows address
        string++;                                                                           //and its contents
    }
   
    //The following while loop is identical to the one above, except the string is located
    //in a different area of the memory
   
    printf("\nA string stored on the stack: \n");

    while (*string2 != '\0')
    {       
        printf("address: %p, Character %c \n",string2, *string2);
        string2++;
    }
    return (0);
}


Let's look at the output:



So this might look like a lot of junk, but it's pretty interesting to see how stuff is stored in memory on your computer. This same code could be made to work on many other devices too.
Hopefully this finds its way to someone trying to understand the topic. I did this to help myself, so maybe it can help you!

Hopefully I don't geek myself up too much for my social media class :)

Friday 13 September 2013

My first Blogger blog - let's blog about it!

Hello world of blogging!

This is my very first blog, and it is a skill I am learning because of my Social Media & Society course I am taking as a continuing education course to earn my General Business Certificate!

This should be interesting, as I hadn't even heard of blogger until I started this course. I will be using this blogging medium to participate in the course, so I'm not entirely sure how entertaining it will be to read, however, I'll probably post some other exciting stuff later.

Cheers!