2010年7月10日星期六

Some link list issue

// this function add a element at the end of linklist in Kathy's Example
// temp as last, temp2 as the end-1

void addEnd (int v)
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data
temp->data_=v;
temp->next = NULL;

// Set up link to this node
if (start_ == NULL)
start_ = temp;
else
{ temp2 = start_;

while (temp2->next != NULL)
{
temp2 = temp2->next; // Move to next link in chain
}
temp2->next = temp;
}
}

// this function delete a element at the end of linklist in Kathy's Example
void removeEnd()
{ node *temp1, *temp2;
if (start_ != NULL)
{ temp1 = start_;

while (temp1->next != NULL)
{ temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
Publish Post

}

2010年6月25日星期五

printf function sequence

In printf function, the sequence of calculating is from right to left.

Example:

#include
void main()
{
int a=5,b=2;
printf("%d %d\n",b=a+1,a=a+1);
}

output =>> 7 6
NOT 6 6


a=a+1 // 6
b=a+1// 7
then printf

2010年6月22日星期二

parameters of main()

In C, main() function has 3 parameters in command line.

main(int argc, char *argv[], char *env[])

int argc : the numbers of strings in in command line.
char *argv[] : the array of input srings.
char *env[] : the environment value

In general situation, we just use
main(int argc, char *argv[])

Example:


#include
main(int argc,char *argv[],char *env[])
{
int i;

printf( "These are the %d command-line arguments passed.\n", argc);
for(i=0; i environment values like path and so on */
//printf( "\nThe environment string(s)on this system are: \\n\n ");
//for(i=0; env[i]!=NULL; i++)
//printf( " env[%d]:%s\n ", i, env[i]);
}

output =>>

@matrix:~/oop344> a.out 12 23 34
4 command-line arguments passed.
argv[0]:a.out
argv[1]:12
argv[2]:23
argv[3]:34

const

const int* p

means you can change the target:
p = &a;
p = &b;
but can not change the value
*p = 100; // WRONG

int* const p
means you can NOT change the target:
p = &a;
p = &b; //WRONG
but can change the value
*p = 100; // YES

const int* const p
All can NOT change

2010年6月12日星期六

Bitwise Operators



Example of Bitwise Operators


http://en.wikipedia.org/wiki/Bitwise_operation

In the C programming language, the bitwise XOR operator is "^" (caret).

XOR

A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:

    0101
XOR 0011
= 0110

2010年6月6日星期日

void pointer

void pointer is a no type pointer can be assigned to other pointer with type.

eg:

void *vp;
int * p;

vp = p;

However, in ANSI

void pointer can not do arithmetical operation.

eg:

#include ;

int main()
{

int a = 10;
int * p = &a;
void * vp;
vp = p;

printf("p = %p,vp = %p\n",p,vp);
printf("%d\n", *p);
p++;
printf("p = %p,vp = %p\n",p,vp);
printf("%d\n", *p);
vp++;
return 0;
}

output:

p = 0012FF94,vp = 0012FF94 //output address
10 //output value
p = 0012FF98,vp = 0012FF94 //p got correct result, vp got wrong
1245120 // p move, the content value is wrong

Point and Array

a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer