360 likes | 582 Views
C Programming Array, Pointer and Structure. 2008/04/23 Jimin Hwa (jmhwa@se.kaist.ac.kr). Contents . Array Pointer Structure. One-dimensional Array. Array? A Sequence of data with homogeneous type Synopsis Example>. type array_name [ size ];.
E N D
C ProgrammingArray, Pointer and Structure 2008/04/23 Jimin Hwa (jmhwa@se.kaist.ac.kr)
Contents • Array • Pointer • Structure
One-dimensional Array • Array? • A Sequence of data with homogeneous type • Synopsis • Example> type array_name[size]; int grade0, grade1, grade2; /* unique id */ int grade[3]; /* grade[0], grade[1], grade[2] */
One-dimensional Array(Cont’d) • Example : int n[10]; int a[size]; /*size = An integer number */ /* space for a[0], a[1], … a[size-1] is allocated */ /* lower bound = 0 */ /* upper bound = size – 1 */ /* size = upper bound + 1 */ n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
Array Index • Example1> n[1] = 7; n[2] = n[1] + 1; • Example2> n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] 7 8 #define SIZE 50 int a[SIZE], i, sum = 0; for (i = 0; i < SIZE; ++i) sum += a[i];
Array Initialization • Initializing all elements simultaneously • By enumerating all data in a brace • Initializing one by one • A loop can be used for it int a[4] = {3, 4, 5, -9}; #define SIZE 100 int a[SIZE] ; for (int i = 0; i < SIZE; ++i) a[i] = i+100;
Multi-dimensional Array • In C, array of any type is allowed. • Array of array, array of pointer, and so on. • Synopsis> • Example> int a[100]; /* 1-D Array */ int b[3][3]; /* 2-D Array */ int c[7][9][2]; /* 3-D Array */ type array_name[number_1][number_2] … [number_n];
Multi-dimensional Array(Cont’d) • Example> int b[3][3] = {1,2,3,4,5,6,7,8,9} int b[3][3] = {{1,2,3},{4,5,6},{7,8,9}} b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[2][0] b[2][1] b[2][2] 1 2 3 4 5 6 7 8 9 b[0] b[1] b[2]
Array as a Function Argument • Example> double sum(double a[ ], int n) { /* n is the size of an array */ int i; double sum = 0.0; for (i = 0; i < n; ++i) sum += a[i]; return (s); }
String(Array of Char) • In C, we can emulate string with an 1-D array of char terminated by NULL character (‘\0’) • Example> char n[5] = “John”; n[0] n[1] n[2] n[3] n[4] char n[5] = {‘J’, ‘o’, ‘h’, ‘n’, ‘\0’}; printf(“%s”, n); ‘j’ ‘o’ ‘h’ ‘n’ ‘\0’
Pointer • Pointer is a variable that contains the address of an another variable. • Pointer is used to access memory and manipulate addresses. int age; Pointer of “int age” address: 0x1234 28 0x1234
Pointer(Cont’d) • Pointer declaration • Example> int *ptr_int; /* Pointer of int variable */ float *ptr_float; /* Pointer of float variable */
Pointer Operators: & and * • Unary operator & gives the address of an object. • Example> • Unary operator * is the dereferencing operator • when applied to a pointer, it accesses the object the pointer points to. • Example> int age; /* age == 0 */ int *ptr_age = &age; /* ptr_age “points to” age */ *ptr_age = 27; /* Now, age == *ptr_age == 27 */
Call-by-Value • void swap(int x, int y); void swap(int a, int b) { int temp; temp = a; a = b; b = temp; return; } void main(){ int x = 100; int y = 200; swap(x, y); /* x == 100, y == 200 */ }
Call-by-Reference • Pointers as function arguments void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; return; } void main(){ int x = 100; int y = 200; swap(&x, &y); /* px = &x; py = &y */ }
Pointer Arithmetic • If p is a pointer of a particular type, p+1 yield the correct machine address for accessing the next variable of that type • Example> p+i /* int *p */ /* pointer to int variable is increased by sizeof(int) * i */ int *p; *p *p+1 *p+2 *p+3 *p+4 4bytes 4bytes 4bytes 4bytes 4bytes
Pointer and Array • Any array operations also can be applied to pointers. int a[10]; /* a == &a[0] */ int *pa = a; /* *pa = &a[0] */ /* pa[0], pa[1], … pa[9] */ • If pa points to a particular element of an array a, then by definition, • pa-i points to the i-th element before pa, • pa+i points th the i-th element after pa.
Pointer and Array(Cont’d) • pa = &a[3]; /* *(pa-3) == a[0], … */ pa-3 pa-2 pa-1 pa pa+1 pa+2 pa+3 a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Pointer and Array(Cont’d) • Example> int a[100], *p; int sum = 0; /* case 1 */ p = a; for (; p<&a[100]; ++p) sum += *p; /* case 2 */ for (i=0; i<100; ++i) sum += *(a+i); /* sum += a[i] */
Example int x = 1, y = 2; int z[10] = {0,1,2,3,4,5,6,7,8,9}; int *ip, *iq; /* ip, iq are pointers to int */ ip = &x; /* ip now points to x */ y = *ip; /* y == 1 */ *ip = 0; /* x == 0, y == 1*/ ip = &z[0]; /* ip now points to z[0] */ ip = ip + 3; /* ip now points to z[3] and *ip == z[3] == 3 */ *ip = 0; /* *ip == z[3] == 0 */ *ip = *ip + 10; /* *ip == z[3] == 10 */ iq = ip; /* iq now points to z[3] and *iq == z[3] == 10 */ *iq = 0; /* *iq == *ip == z[3] == 0 */
Structure • A structure is a collection of variables, possibly of different types, grouped together under a single name for convenience. • Using structure, we can keep together different pieces of information as a single data record • Data packing mechanism • Example> structure student attributes: Name, student ID and grade
Structure(Cont’d) struct student { /* struct struct_name{ */ char name[32]; /* definition of members */ int student_id; /* }; */ char grade; }; struct class { struct student member[100]; int num_of_students; int average_grade; }; struct class CC510; /* struct struct_name var_name */
Structure Operators • Structure member operator for non-pointer: . (dot) • connects the structure name and the member namestructure_name.member • Example> struct student john; john.grade = ‘A’; john.student_id = 4473; int id = john.student_id; • Structure member operator for pointer : -> • Exmple> struct student *pJohn; pJohn->grade = ‘A’; /* (*pJohn).grade = ‘A’; */ pJohn->student_id = 4473; int id = john->student_id;
Structure Operators(Cont’d) • Structure assignment: = • Assigning the contents of a structure to another one • Example> struct student John; struct student John_clone; John.name = “John”; John.grade = ‘A'; John.student_id = 945433; John_clone = John; /* John_clone.name = = “John”, John_clone.grade = = ‘A‘, John_clone.student_id = = 945433 */
Structure Comparison • We need to compare all fields one by one explicitly. • Example> if(john == john_clone){ … } /* Wrong */ if(strcmp(John.name, john_clone.name) == 0 && John.student_id == john_clone.student_id && …){…} /*Right */
Structure as a Function Argument • They are passed to functions by the usual parameter-passing mechanism • The entire structure is copied to the function • Example> int fail(struct student of_classA[], int size) { inti, count = 0; for (i=0; i < size; ++i){ if(of_class[i].grade == ‘F’) count ++; } return count }
LAB #6 C++ String Library • Write following two string functions: replace and substring. • Do not use C string functions declared in <string.h> • Write a demonstration program to show how your string functions work. “1” is selected • Replace • substring Input a string : “apple” “2” is selected Input char1 : ‘p’ … ‘b’ Input char2 : Result : abble
LAB #6 C++ String Library(Cont’d) • Char *replace(const char *s, const char oldChar, const char newChar); • Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar • Examples> • replace("mesquite in your cellar“, 'e', 'o') returns "mosquito in your collar"
LAB #6 C++ String Library(Cont’d) • char *substring(const char *s, int beginIdx, int endIdx); • Returns a new string that is a substring of s. • The substring begins at the specified beginIdx and extends to the character at index endIdx - 1. Thus the length of the substring is endIdx-beginIdx. • Examples: • substring("hamburger“, 4, 8) returns "urge" • substring("smiles“, 1, 5) returns "mile
Homework #3 • Write a program that prints out the union, intersection, except of two sets • Example> • A = {2, 3, 7, 10, 11, 18, 20, 21}, B = {3, 6, 10, 14, 17} • Intersection: 3 10, Union: 2 3 6 7 10 11 14 17 18 20 21, Except: 2 7 11 18 20 21 • Write a program which swaps pairs of complex numbers • Assumed the arbitrary size • Example> • A = {1+3i, 2-2i, 4+i}, B ={2+3i, 3+5i, 2-i} • After Swapping A with B -> A={2+3i, 3+5i, 2-i}, B={1+3i, 2-2i, 4+i} • Use structure for a complex number, which consist of two integers(an real number and an imaginary number). • Don’t use global variables. • Hint : Array, pointer and structure
Homework #3(Cont’d) • Write a demonstration program to show how your string functions work • 담당 조교 • 이름 : 오태우 • E-mail : twoh@mmc.kaist.ac.kr
The End Any Question?