Sunday, October 17, 2010

vstrcat (char* str,......)

#include
#include
#include

void vstrcat(char* str, ...){
int NumOfWords = 0;
va_list args;
int arg;
int i;

va_start(args,str);
while(va_arg(args,char*)_) NumOfWords++;


va_start(args, NumOfInts);
for(i=0;i
strcat(str,va_arg(args, char*);

}
va_end(args);

}

int main(){
char str[500];
vstrcat(str, "Hello!", " ", "I", " ", "am", " ", "right here!", NULL);
printf(str);
printf("\n");
return 0;

}

stdarg.h

#include
#include

int IntAvr(int NumOfInts, ...){
va_list args;
int sum=0;
int arg;
int i;
va_start(args, NumOfInts);
for(i=0;i arg = va_arg(args, int);
sum += arg;
}
va_end(args);
return sum/NumOfInts;
}

int main(){
printf("%d\n", IntAvr(4, 10, 20, 30, 10));
return 0;
}

Result: 17

Saturday, October 2, 2010

Pointers do not have to point to single variables. It can also point at the cells of an array. For example:

int *ip;
int a[10];
ip = &a[3];

and we would end up with ip pointing at the fourth cell of the array a (remember, arrays are 0-based, so a[0] is the first cell). We could illustrate the situation like this:

We'd use this ip just like the one in the previous section: *ip gives us what ip points to, which in this case will be the value in a[3].

Once we have a pointer pointing into an array, we can start doing pointer arithmetic. Given that ip is a pointer to a[3], we can add 1 to ip:

ip + 1

What does it mean to add one to a pointer? In C, it gives a pointer to the cell one farther on, which in this case is a[4]. To make this clear, let's assign this new pointer to another pointer variable:

ip2 = ip + 1;

Now the picture looks like this:

If we now do

*ip2 = 4;

we've set a[4] to 4. But it's not necessary to assign a new pointer value to a pointer variable in order to use it; we could also compute a new pointer value and use it immediately:

*(ip + 1) = 5;

In this last example, we've changed a[4] again, setting it to 5. The parentheses are needed because the unary ``contents of'' operator * has higher precedence (i.e., binds more tightly than) the addition operator. If we wrote *ip + 1, without the parentheses, we'd be fetching the value pointed to by ip, and adding 1 to that value. The expression *(ip + 1), on the other hand, accesses the value one past the one pointed to by ip.

Given that we can add 1 to a pointer, it's not surprising that we can add and subtract other numbers as well. If ip still points to a[3], then

*(ip + 3) = 7;

sets a[6] to 7, and

*(ip - 2) = 4;

sets a[1] to 4.

Up above, we added 1 to ip and assigned the new pointer to ip2, but there's no reason we can't add one to a pointer, and change the same pointer:

ip = ip + 1;

Now ip points one past where it used to (to a[4], if we hadn't changed it in the meantime). The shortcuts we learned in a previous chapter all work for pointers, too: we could also increment a pointer using

ip += 1;

or

ip++;

Of course, pointers are not limited to ints. It's quite common to use pointers to other types, especially char. Here is the innards of the mystrcmp function we saw in a previous chapter, rewritten to use pointers. (mystrcmp, you may recall, compares two strings, character by character.)

char *p1 = &str1[0], *p2 = &str2[0];

while(1)
{
if(*p1 != *p2)
return *p1 - *p2;
if(*p1 == '\0' || *p2 == '\0')
return 0;
p1++;
p2++;
}

Friday, October 1, 2010

I have learned how to used function pointer for keep the entry address of the function.
For example:

There are 10 functions.

int fun1(){...}
int fun2(){...}
......
int fun(10){...}


If you want to execute them by order, then you can use the function pointer.

int (*table[10]()) // defined a function pointer array
{
fun1,
....
fun10,
};

for (int i = 0; i< 10; i++)
{
table[i};
}

Wednesday, September 22, 2010

I created my personal svn directory under my group directory, added a sample C text file to my testdir, then I did checkout the whole group repository to my local repository. I also modified, updated the C file and committed the changes.

It works for me.

Saturday, September 11, 2010

Welcome to Lynn's Blog

Hi,

This is my first blog ever.
Please stay tuned for more update from what I am learning in OOP344.