Monday, 4 April 2011

Exam Cheatsheet

So my exam for Computer Programming II is this Thursday, and having looked at the course outline my prof made for us, it clearly states that we are allowed to bring in " page during the exam time to assist you in memorizing some syntactic matters." So basically this means that we can bring in a page full of examples of everything we learned this semester. Hopefully nobody reminds her about this and makes her change her mind... She's pretty cool though, so I'm sure if we all show up with them, she'll be cool with it. 

Anyway, I decided to share mine with everyone here! Feel free to use it as a reference, or even as study help for your own class!

Download Link 1

Calling by reference:

int x=0;

int *ptr;
ptr=&x;           //ptr now points to x

void function(int *y)
       *y++;   //y points to x

int arr[5];
arr[3]; //same as *(arr+3)

pointer to function:
double (*fp)(double,double);

fp=pow; //fp now acts like pow

Useful String Functions:
int atoi( const char *nPtr )
char *gets( char *s );
char *strcpy( char *s1, const char *s2 )
char *strcat( char *s1, const char *s2 )
char *strstr( const char *s1, const char *s2 );

typedef struct dummyname
       int x;

StructName identifier;


Dynamic Memory:
ptr = (int*)malloc(5*sizeof(int));

Linked Lists:
typedef struct node
       char data;
       struct node *next;

Node *p;

typedef enum dummyname

force values:

As large as its largest element.
Holds one at a time.
Declare like a struct.

#define SQUARE(x) ((x)*(x))
#if, #ifdef, #else, #endif

#define TOKCAT(x,y) x ## y

Variable Length Args:
int funct(int n,...)
       va_list args;
       va_start(args,n);//n is first param
loop()      variable=va_arg(args,type);

Command Line Args:
int main(int argc,char *argv[])

using namespace std;

Default Args:
int func(int x=1);

template <class T>
T square(T value)
       return value*value;

int x=square(5);
float y=square(5);

Class ClassName
void func();
       const int x;
       void inlineFunct(){return 0;};
       //private ones

void ClassName::funct()

{/*default values*/}

Data Initializer:
{/*default values*/}
Class1::Class1(Class2 &identifier)

Friend Functions:
class ClassName
       friend void func();

void func(ClassName name)
       //access private stuff

new and delete:
int ptr = new int(3.141592654);
int arr = new int[10];
delete []arr;

Static Class Members:


Operator Overloading:
class ClassName
friend ostream& operator<<( ostream&, const ClassName&);

ClassName& ClassName::operator=(const ClassName x)
return *this;

ostream& operator<<(ostream &o, const ClassName x)
return o;

Sunday, 3 April 2011

The Basics of Pointers - Part 1

Now, we've learned to deal with variables. In most programming languages, you'll be able to deal with "references" to variables (more of why we would want to do this in the next post), such a thing in C is called a pointer. Let's just jump right into the code, shall we?

int x;
int *ptr;

Here, we are declaring an integer called x, and after that, we declared what is called "a pointer to integer". A pointer is declared by specifying the type, and then putting an asterisk before the desired identifier. Note that both of the following are correct syntax which do the exact same thing.

int *ptr1;
int* ptr2;

It doesn't matter is the asterisk goes before or after the space.

Last time, we learned about memory addresses. Basically, a pointer can be thought of as a variable which holds a memory address of a specific type of variable. Remember how we used "&" to access the memory location of a variable? Well here's where it comes into play. Let's say we have:

int x=5;
int *ptr;
ptr = &x;

This would make ptr point to x. In a way, this means that we can indirectly access the content of x. To do this, we put an asterisk in front of the pointer's identifier. For example, using the code above...


Result: 5

In the same way, we can modify the content of x by using the pointer.

*ptr = 42;

Result: 42

By putting the star in front of your pointer's identifier, it is like saying: "The content of..."

Note that in order for a pointer to work properly, it must be declared properly as well. By this, I mean that you cannot reference a float from a pointer to integer. This:

float y;
int *ptr3;
ptr3 = y;

will cause a compilation error. Why?

When you declare variables, bytes of memory are taken up. The amount of bytes is determined by what type of variable it is. An integer will typically take up 4 bytes, while a floating point number will take up 8. This means that when we have a pointer to integer, the pointer is expecting a memory address such that 4 bytes after it are taken up. In this example, it sees that it would need 8 bytes, that is, a pointer to integer would only hold half the data, and would be full after "filling up" half way. The compiler sees that this is a problem, and reports and error.

So we know about using pointers now. Great. So why would we ever want to do this?
Stay tuned.