32
Network Programming in C C Fundamentals command line arguments, pointers, and structures 20 10 Fall semester Rodney Van Meter

Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Embed Size (px)

Citation preview

Page 1: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Network Programming in C第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」

20 10 Fall semesterRodney Van Meter

Page 2: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

授業 Web ページ SFC-SFShttps://vu.sfc.keio.ac.jp/sfc-sfs/

本講義を「 MY 時間割(仮)」へ登録してください

課題・授業資料などの情報を掲示します 課題は毎回こちらに提出!! Deadline

Due Tuesday, 10/19 10:59 a.m.

Page 3: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

今期の授業スケジュール(予定) 第1回 9/28 : Introduction / イントロダクション 第2回 10/5 : C Basics ~ Functions, Variables, Data

Types ・ Makefiles 第3回 10/12 : C Basics ~ Command Line Arguments

・ Structures ・ Pointers 第4回 10/19 : C Basics ~ Pointers & Arrays ・ Lists 第5回 10/26 : file I/O ・ Network Programming 第6回 11/2 : Network Programming (1) 第7回 11/9 : Network Programming (2) 第8回 11/16 : Network Programming ( 3 ) 第9回 11/30 : Applied Network Programming ( 1 ) 第10回 12/7 : Applied Network Programming (2) 第11回 12/14 : Work on Projects 第1 2 回 12/21 : Work on Projects 第13回 1/19 : Final Presentations!!!

Page 4: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

今日のお題 Review last week’s homework コマンドライン引数 ポインタの基礎 構造体

Page 5: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

課題 1:Homework Prob. 1 浮動小数点のレーンジ(fprange.c)

1. 最小: Write a program that starts withx = 1.0, and divides it by two repeatedly until it becomes zero

2. 最大: Do the same thing getting larger until something happens (what happens?)

3. Do for both “float” and “double”.

Page 6: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

課題 2

Makefile を作ってください 一行目は:

all: hello typesizes fortest cline numbers

Page 7: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

コマンドライン引数

Page 8: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Command Line Arguments :argc,argv

What’s a command line argument?% cat –n file% cat file1 file2 file3

Page 9: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Using Arguments main( int argc, char **argv) main( int argc, char *argv[])

argc is number of args argv[0] is command name argv[1] is first argument argv[2] is second argument

Page 10: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Exercise: Using the args#include <stdio.h>int main(int argc, char *argv[]){

int i;for (i=0; i < argc; i++){

printf(“\t%s\n”, argv[i]);}

return 0;

}

Page 11: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointers

Page 12: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

What’s a Pointer? A pointer is a variable

It points to another variable int i = 10; int j = 20; int *ptr

meaning of * in declaration, says this var

is a pointer to the named typeVariables & addresses

0

Address

ptr

j

i

100

104

108

Page 13: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

What’s a Pointer? A pointer is a variable

It points to another variable int i = 10; int j = 20; int *ptr

meaning of * in declaration, says this var

is a pointer to the named typeVariables & addresses

0

Address

ptr

j

i

100

104

108

Page 14: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointer Exercise int i = 10; int j = 20; int *ptr = &i;

printf(“&i=%d\n”, &i); printf(“ptr=%d\n”, ptr); printf(“i=%d\n”, i); printf(“*ptr=%d\n”,*ptr); ptr points to variable i

変数とアドレス0

アドレス

ptr

j

i

100

104

108 10

20

108

Page 15: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

int x=1, y=5; int z[10]; int *p; p=&x; /* p points to x */ y=*p; /* y gets 1 */ *p = 0; /* x becomes 0 */ p=&z[2]; /* p points to z[2] */

More on Pointers

Page 16: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointers in Functions (1)#include <stdio.h>void swap(int x, int y){

int temp;temp = x;x = y;y = temp;

}

int main(){int a = 5;int b = 3;swap (a,b);printf(“a=%d\n”, a);printf(“b=%d\n”,b);return 0;

}

int 型の変数を入れ替えるプログラム

Try this out: does it swap a & b?

Page 17: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointers in Functions (2)#include <stdio.h>void swap(int x, int y){

int temp;temp = x;x = y;y = temp;

}

int main(){int a = 5;int b = 3;swap (a,b);printf(“a=%d\n”, a);printf(“b=%d\n”,b);return 0;

}

変数とアドレス0

アドレス

b

a

104

108

5

3

x

y

94

90

交換

Page 18: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointers in Functions (3)#include <stdio.h>void swap(int *x, int *y){

int temp;temp = *x;*x = *y;*y = temp;

}

int main(){int a = 5;int b = 3;swap (&a,&b);printf(“a=%d\n”, a);printf(“b=%d\n”,b);return 0;

}

int 型の変数を入れ替えるプログラム

変数とアドレス0

b

a

104

108

108

104

x

y

94

90

*x

*y

Exchange

Page 19: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Data Structures

Page 20: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

構造体 (structure) A structure can hold more than one piece

of data, of the same or different types Ex: Holding ID & test score for a student

struct student{int id;int score;

}; Different element types:

struct student2{char name[32];int score;

};

Page 21: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

構造体の定義

struct 構造体名 { 型 メンバ名 1; 型 メンバ名 2;

::

};

使用例

struct student{int id;int score;

};

定義の方法

Page 22: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Using structs as variables

After defining struct, can use when defining variables Instead of type, use struct name

int a;struct student b;

Page 23: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Accessing struct members (1)

Simplest way is using . ( ドット ) 演算子 Ex: struct student’s score value:

struct student b = {70000000,70};printf(“b の点数は %d です \n”, b.score);

Page 24: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Accessing struct members (2)

Can also use a pointer to a struct: -> 演算子 Ex: score in struct student

struct student b = {70000000,70};struct student *c = &b;printf(“b の点数は %d です \n”, c->score);

In place of -> can also use(*pointer).member(*c).score

Page 25: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Using structures: example 1

int main(){

int i;struct student{

int id;int score;

} students[5] = { {70001234,80}, {70204578,70}, {70157384,60}, {70355678,75}, {70207658,49} };

for(i=0;i<5;i++){printf("student id:%d, score:%d\n",

students[i].id, students[i].score);}

}

Definition and initialization together

Page 26: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Structure definition separate from variable declaration & initialization

struct student{int id;int score;

};

int main(){

int i;struct student students[5] = { {70001234,80},

{70204578,70}, {70157384,60}, {70355678,75}, {70207658,49} };

for(i=0;i<5;i++){printf("student id:%d, score:%d\n", students[i].id,

students[i].score);}

}

Using structures: example 2

Page 27: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Initialize at run time instead of compile time

struct student{int id;int score;

};

int main(){

int i;struct student students[5];for(i=0; i<5; i++){

students[i].id = i;students[i].score = i;

}for(i=0;i<5;i++){

printf("student id:%d, score:%d\n", students[i].id, students[i].score);}

}

Using structures: example 3

Page 28: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

typedef

typedef struct student{int id;int score;

} STUDENT;STUDENT students[5];

Page 29: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Pointers to structs

struct student st;struct student *sp;sp = &st;sp->id = 7000123;(*sp).score = 23;

sp

7000123

st

23

id

score

printf(“%d\n”, sp->score);

Page 30: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Practice Problem 1 Write a program that accepts as input:

student number login name full name

%./a.out 80749423 kazuhisa “kazuhisa matsuzono”

Number: 7045678Login name: kazuhisaName: kazuhisa matsuzono

Page 31: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

Example (Not quite what we’re doing!)

#include <stdio.h>struct student{

char name[20];int math;int jlang;

};void clear(struct student *sei);

int main(){struct student seito;/* clearing data in seito */clear(&seito);

return 0;}

void clear(struct student *sei){strcpy (sei->name, “”);sei->math=0;sei->jlang=0;

}

Page 32: Network Programming in C 第3回「 C Fundamentals ~ command line arguments, pointers, and structures 」 20 10 Fall semester Rodney Van Meter

課題 / Homework Finish Practice #1 Determine size of struct student Determine size of array of struct

student Use memcpy() to copy struct Use assignment to copy struct Use memset() to clear memory