SE250:March 26

From Marks Wiki
Revision as of 05:18, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (37 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Agenda

  • Why doesn't the piece of C code below work as I want it to? As in, it should change the C-string str so it's new value is "world" but it doesn't.
    • Also with the below code, would the memory that was allocated in the function function for the string "world" be de-allocated once the function exits?
/*
    File: strings.c
*/

#include <stdio.h>

void function( char *str )
{
    str = "world";
    puts( str );
}

int main( void )
{
    char *str;

    str = "hello";
    function( str );

    puts( str );

    return 0;
}

Compile and run:

gcc strings.c -o strings && ./strings.exe

Output:

world
hello
  • How to put screencasts on the wiki


Minutes

Screencasts

  • John started off by talking about how to put screencasts onto the wiki.
1. John told us that he'd prefer us giving him the files on a flash drive or a CD, which he'll then load
   onto the course website and it can be linked to from the wiki.

2. Another option given to us, though not recommended, was to email the files to John.

3. You will need Shockwave to run any screencast on your internet browser, which can be downloaded free of cost.


Code

  • The second matter of discussion was the code written on the agenda.

It is supposed to print:

world
world

On the contrary though, it prints

world
hello

Explanation

The reason provided for this is because the function variables are only accessed within the function. Just because the variable names in the function and the main code are the same doesn't mean the variables are the same. This can be better explained by renaming the function variable str to strx. The code now looks a like this:

/*
    File: strings.c
*/

#include <stdio.h>

void function( char *strx )
{
    strx = "world";
    puts( strx );
}

int main( void )
{
    char *str;

    str = "hello";
    function( str );

    puts( str );

    return 0;
}

Solution

To do what the programmer originally planned, the following code should work better:

/*
    File: strings.c
*/

#include <stdio.h>
typedef char* charP;
void function( charP *str )
{
    *str = "world";
    puts( *str );
}

int main( )
{
    charP str;

    str = "hello";
    //function( str );
    function ( &str);                //i think an ampersand is required here.  tested and compiles with correct output.  [stsa027]

    return 0;
}

This code will output:

world
world

End of Minutes


Notice

Notice: if there are any inconsistancies with the code or the contents of this article, please do not delete original content. Instead add a line at the bottom with the correct content with your signature. This way everyone get's to look at what may be a common mistake, or just a silly mistake, and it might help them not make the same sort of mistake. Thanks.

--Rbha033 23:28, 26 March 2008 (NZST)