BI201: Linux and Shell Programming
Lecture 05: Linux C Fundamentals (II) Maoying,Woo (Çj=)
[email protected] Dept. of Bioinformatics & Biostatistics Shanghai Jiao Tong University
May 17, 2013
Maoying
BI201
Static Variables (· Cþ)
Local variables declared with the keyword static are still known in the function in which they are defined. However, unlike automatic variables, static variables retain their value even after the function exits. All numeric static variables are initialized to zero if NOT explicitly initialized.
Maoying
BI201
Static Variables by Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* An Example of a Static Variable */ float nonstat( float x){ int i = 1; i = 10*i; x = i - 5.0*x; return x; } float stat(float y){ static int i = 1; //1st round ,i=1 -> i = i*10 = 10 i = 10*i; //2nd round, i=10 -> i = i*10 =100 y = i - 5.0*y; //3rd round, i=100 -> i = i*10 = 1000 return y; // 1st round, y=2 -> y=0 } // 2nd round, y=0 -> y=100; 3rd round, y=100 -> y=500 int main(){ int i; float var1, var2; var2 = var1 = 2.0; printf(" var1 = %9.2f, var2 = %9.2f\n", var1, var2); for ( i = 1; i suit, ( *cardPtr ).face, " of ", ( *cardPtr ).suit ); return 0; }
Maoying
BI201
Print an address (• †/Œ)
1 2 3 4 5 6 7 8 9
/* ptr1.c */ int main() { int i; i = 4; printf("i = %d, address of i = %u\n", i, &i); return 0 }
1 gcc -o ptr1 ptr1.c 2 ./ptr1 3 i = 4, address of i = 3220392980
Maoying
BI201
What is a pointer? A variable that contains a memory address as its value Pointers contain the address of a variable that has a specific value (an indirect reference) Pointers in C are typed. a pointer to a variable of type int: int* a pointer to a variable of type char: char* a pointer to a user-defined type or an object.
Pointer has its own size, equal to 64 on 64-bit machines, 32 on 32-bit machines. Maoying
BI201
Indirected Reference 1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* ptr2.c */ /* Pointer can be indirect reference */ int main() { int i; int *ptr; /* pointer declaration */ i = 4; ptr = &i; printf("i = %d\n address of i = %u\n address of pointer = %u\n", i, ptr, &ptr); return 0; }
Results 1 2 3 4
./ptr2 i = 4 address of i = 3219352564 address of pointer = 3219352560
Maoying
BI201
Address in Hex (›8?›) 1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* ptr3.c */ /* Address can be expressed in hex */ int main() { int i; int *ptr; /* pointer declaration */ i = 4; ptr = &i; printf("i = %d\n address of i = %p\n address of pointer = %p\n", i, ptr, &ptr); return 0; }
Maoying
BI201
Pointer (II)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Never trust a compiler. */ int j, i; /* think globally! */ int *ptr1, *ptr2; void printit () { printf(" i = %2d, ptr1 = %p\n", i, ptr1); printf(" j = %2d, ptr2 = %p\n", j, ptr2); } int main() { i = 4; j = 8; ptr1 = &i; ptr2 = &j; printit(); *ptr2 = *ptr2 + 1; ptr1 = ptr1 - 2; /* You cannot know this */ printit(); i = 6; *ptr1 = *ptr1 + 10; printit(); return 0; }
Maoying
BI201
swap() function
1 2 3 4 5 6 7 8 9 10 11 12
/* A simple memory swap using pointers */ /* *operator can be used as alias/nickname for variable * inside of function. */ void swap(int *i, int *j) { int temp; temp = *i; *i = *j; *j = temp; }
Maoying
BI201
Swap: A Pointer Example 1 int main() 2 { 3 int i; 4 int mem1, mem2, ray[4]; 5 6 mem1 = 12; 7 mem2 = 81; 8 swap(&mem1, &mem2); /* swap two integers */ 9 printf("mem1: %4d mem2: %4d\n", mem1, mem2); 10 11 for (i=0; i < 4; i++) 12 { 13 ray[i] = 10 * i; 14 printf("ray[%d] = %4d", i, ray[i]); 15 } 16 printf("\n"); 17 18 swap(&mem1, &ray[3]); 19 swap(&mem2, &ray[2]);\ 20 printf("mem1:%4d mem2:%4d\n", mem1, mem2); 21 22 for (i=0; i < 4; i++) 23 printf("ray[%d] = %4d\n", i, ray[i]); 24 25 printf("\n"); 26 return 0; 27 }
Maoying
BI201
Pointers and Arrays (• †ê|) (I)
1 int main() 2 { 3 int i, r[6] = {1,1,1}; 4 int *ptr; 5 ptr = r; 6 *ptr = 83; 7 *(ptr + 2) = 33; 8 for (i = 0; i < 6; i++) 9 printf("r[%d] = %d\n", i, r[i]); 10 11 r[4] = *ptr; 12 ptr++; 13 *ptr = 6; 14 *(ptr + 2) = 7; 15 for (i=0; i < 6; i++) 16 printf("r[%d] = %d\n", i, r[i]); 17 return 0; 18 }
Maoying
BI201
Pointers and Arrays (• †ê|) (II) Array names are treated as pointer when called by a function; Array names are treated as array when called by operators like sizeof(). 1 2 3 4 5 6 7 8 9 10 11
int arraysize(char arr[]) { return sizeof(arr)/sizeof(arr[0]); }
int main() { char cp[20] = "hello, world!"; printf("The size of array cp: %d\n", sizeof(cp)/sizeof(cp[0])); printf("The length of string cp: %d\n", strlen(cp)); printf("The final char, converted to integer, for array cp: %d\n", cp[strlen(cp) ]); 12 printf("By calling the arraysize function, the size of cp: %d\n", arraysize(cp)); 13 printf("Confused? When called by a function, the array name will be converted to a pointer.\n"); 14 printf("So here we calculate the size of a pointer, divided by the size of a character.\n");
Maoying
BI201
Operator Precedence Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* An example of operator precedence trouble */ int main () { float x,y,z; float *ptr1, *ptr2, *ptr3; x =2.0; y = 8.0; z = 4.0; ptr1 = &x; ptr2 = &y; ptr3 = &z; printf (" %u %u %u\n", ptr1, ptr2, ptr3); *ptr3++; printf (" %f %f %f\n", x, y, z); printf (" %u %u %u\n", ptr1, ptr2, ptr3); printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); (*ptr1)++; printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); --*ptr2; printf (" %f %f %f\n", *ptr1, *ptr2, *ptr3); return 0; }
Maoying
BI201
malloc() and free()(S•© Úº˜S•) malloc() is used to dynamically allocate memory for pointer, whose return type is void*. When the pointer is used, should be casted into the specific pointer type. 1 float **dmatrix(int m, int n) 2 { 3 int i; 4 float **mat; 5 **mat = (float **)malloc(m*sizeof(float*)); 6 for ( i=0; i