Archive

Archive for February, 2010

Passing arguments to a C program from terminal

February 21, 2010 Leave a comment

Have you ever wondered how some UNIX commands take arguments? It can be done very easily with C.

You can code the rm, cp, ect commands in no time. Here is a sample code which you can use to understand passing arguments.

int main(int argc, char* argv[]){
  int i;
  for(i = 0;i<argc; i++){
    printf("%d: %s\n", i+1, argv[i]);
  }
  return 0;
}

argc is the number of arguments being passed in, and argv is a char pointer array which has the arguments.

Categories: OOP344

enum intro in C

February 20, 2010 Leave a comment

Enums can be used to reduce #define statements in a C program, although they can have more uses.

Here is an example of enum.

#include  
int main()
{
enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

Days TheDay;
int j = 0;
printf("Please enter the day of the week (0 to 6)\n");
scanf("%d",&j);
TheDay = Days(j);

if(TheDay == Sunday || TheDay == Saturday)
printf("Hurray it is the weekend\n");
else
printf("Curses still at work\n");

return 0;
}

One thing to keep in mind is that enums start from 0 so in the following line of code

enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

Sunday is assigned 0, Monday is assigned 1 and so on..

It is possible to make them start at a different number if we assign a number to it as shown below.

enum Days{Sunday = 1,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

In the example above, Sunday would have the value 1, Monday would have 2 and so on….

Feel free to ask any questions in the comment box below.

Categories: OOP344

Pointers to functions in C/C++ intro

February 10, 2010 Leave a comment

Pointers to functions are a very interesting and elegant programming technique. They can be used to replace long switch or if statements!

Anyone can start using pointers to functions all you have to do is to understand what they are and to learn its syntax.

What is a Function Pointer?

Function pointers are simply pointers that point to a function! Functions have addresses in memory just as variables do.

Syntax

The syntax can be a little strange if you are seeing for the first time but its very easy to grasp.

Suppose we have the following function


int add(int a, int b){
return a+b;
}

we can start creating a pointers to function by writing

int (*fptr)(int, int);

int is the return type of the function and (int, int) is the arguments the function accepts.

The next step is to give our pointer to function the function’s address. We do that with the following code

fptr = add;

Now that fptr points to the “add” function we can actually start calling out function using the fptr pointer. We use the code below to do it.


int c = 0;
c = (*fptr)(a, b);

As you can see pointers functions are easy to learn 🙂 I will make more posts soon to show how you can get rid of a long switch statement by using pointers to functions. I have also posted a compilable example below so you can experiment with.


/* Author: Fardad Soleimanloo */
#include
int add(int a, int b){
return a+b;
}
int main(){
int (*fptr)(int, int);
int a = 10;
int b = 20;
int c;
fptr = add;
c = (*fptr)(a, b);
printf("%d\n", c);
return 0;
}

Categories: OOP344