Overview (1A) Young Won Lim 9/25/17

Similar documents
Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 12/26/17

Pointers (1A) Young Won Lim 3/5/18

Pointers (1A) Young Won Lim 1/14/18

Pointers (1A) Young Won Lim 12/4/17

Function Overview (1A) Young Won Lim 10/23/17

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

Applications of Pointers (1A) Young Won Lim 4/11/18

Applications of Pointers (1A) Young Won Lim 3/31/18

Pointers (1A) Young Won Lim 10/18/17

Applications of Pointers (1A) Young Won Lim 4/24/18

Applications of Pointers (1A) Young Won Lim 2/27/18

Pointers (1A) Young Won Lim 1/22/18

Pointers (1A) Young Won Lim 2/6/18

Applications of Structures (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 10/23/17

Applications of Pointers (1A) Young Won Lim 1/5/18

Applications of Structures (1A) Young Won Lim 12/8/17

Pointers (1A) Young Won Lim 2/10/18

Applications of Arrays (1A) Young Won Lim 2/11/17

Applications of Arrays (1A) Young Won Lim 3/15/17

Arrays (1A) Young Won Lim 12/4/17

Structure (1A) Young Won Lim 7/30/13

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Arrays (1A) Young Won Lim 1/27/17

File (1A) Young Won Lim 11/25/16

Example 1. Young Won Lim 11/17/17

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Type (1A) Young Won Lim 2/17/18

Example 2. Young Won Lim 11/24/17

Example 3 : using a structure array. Young Won Lim 11/25/17

Structures (1A) Young Won Lim 12/4/17

Structures (1A) Young Won Lim 11/8/16

Example 3. Young Won Lim 11/22/17

Memory (1A) Young Won Lim 9/7/17

Number System (1A) Young Won Lim 7/7/10

Arrays and Strings (2H) Young Won Lim 3/7/18

Variables (2D) Young Won Lim 3/28/18

ELF (1A) Young Won Lim 10/22/14

Applications of Array Pointers (1A) Young Won Lim 11/22/18

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Pointers (2G) Young Won Lim 3/7/18

Expressions (2E) Young Won Lim 4/9/18

Program Structure (2A) Young Won Lim 3/8/18

Accessibility (1A) Young Won Lim 8/22/13

Expressions (2E) Young Won Lim 3/10/18

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Program Structure (2A) Young Won Lim 5/28/18

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

ELF (1A) Young Won Lim 3/24/16

Class (1A) Young Won Lim 9/8/14

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

Functions (4A) Young Won Lim 3/16/18

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

AMCAT Automata Coding Sample Questions And Answers

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Pointers and scanf() Steven R. Bagley

Friday, February 10, Lab Notes

Structures (2I) Young Won Lim 4/17/18

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Functions (4A) Young Won Lim 5/8/17

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Arrays and Pointers (part 2) Be extra careful with pointers!

Structures (2I) Young Won Lim 3/7/18

Arrays and Pointers (part 2) Be extra careful with pointers!

Algorithms (7A) Young Won Lim 4/10/17

Monad Background (3A) Young Won Lim 11/20/17

Binghamton University. CS-211 Fall Functions. The Basis of C

Programming and Data Structure

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Course organization. Course introduction ( Week 1)

Algorithms (7A) Young Won Lim 4/18/17

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CSC258: Computer Organization. Functions and the Compiler Tool Chain

GHCi: Getting started (1A) Young Won Lim 5/26/17

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

PROGRAMMAZIONE I A.A. 2017/2018

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Polymorphism Overview (1A) Young Won Lim 2/20/18

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Dynamic memory allocation

Structured Programming. Functions and Structured Programming. Functions. Variables

Monad (1A) Young Won Lim 6/9/17

Worksheet 4 Basic Input functions and Mathematical Operators

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Side Effects (3B) Young Won Lim 11/20/17

Transcription:

Overview (1A)

Copyright (c) 2009-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Calculating the Mean The mean of 3 numbers m = a b c 3 40 + 50 + 60 3 = 150 3 = 50 Integer number fixed point number 45 53 63 3 = 161 3 = 53.6666666... Real number floating point number Overview 3

Calculating a mean in C int a, b, c; int mean; a = 40; b = 50; c = 60; mean = (a + b +c) / 3; int a, b, c; float mean; a = 45; b = 53; c = 63; mean = (a + b +c) / 3.0; * Variable * Type * Assignment * Operator Overview 4

Variables int Type int a, b, c; int mean; a = 40; b = 50; c = 60; mean = (a + b +c) / 3; a b c mean a b c mean 40 50 60 50 Overview 5

Variables float Type int a, b, c; float mean; a = 45; b = 53; c = 63; mean = (a + b +c) / 3.0; a b c mean a b c mean 45 53 63 53.6...67 float type Overview 6

C and assembly code view of variables variable name variable value address data a 40 b 50 4500 40 c 60 4510 50 mean 50 4520 60 c source code view 4530 50 Compiler assembly code view Overview 7

Memory : (Address, Data) a address address data a value b address 4500 40 b value c address mean address 4510 4520 50 60 c value mean value 4530 50 compiler determines the addresses Overview 8

Getting the addresses of variables &variable address Addresses determined by a compiler &a address of a &b address of b &c address of c &mean address of mean Overview 9

Example: a variable stored in memory &variable address int a; a = 40; &a a 4500 40 &a a :address (where) :data (what) 4500 40 Overview 10

Variables and the & operator int int a=40, b=50, c=60; mean=(a+b+c)/3; address data &a: 4500 a: 40 a b 40 50 &b: 4510 &c: 4520 b: 50 c: 60 c 60 &mean: 4530 mean: 50 mean 50 Overview 11

Graphical representation of address assignment a = 40; Value assignment to a int a ; &a: 4500 a = 40 integer variable an arrow a pointer p = &a; Address assignment to p int * p ; &p p= &a pointer variable 4500 Overview 12

Data and Address Operators Address operator Data operator &variable address *address data &a address of a *(&a) data at the address of a &b address of b *(&b) data at the address of b &c address of c *(&c) data at the address of c &mean address of mean *(&mean) data at the address of mean Overview 13

The & and * operators The address of a variable : Address of operator & the value at an address : Dereferencing operator * * & a a p *p & Overview 14

&a and *p &a *p a must be a variable p s value must be an address int a ; int * p ; integer variable a pointer variable p Overview 15

The address &a and the variable *p &a an address *p a variable a a variable p an address int a ; int * p ; integer variable a pointer variable p Overview 16

R/W Accessing &a and *p int a ; int * p ; integer variable a pointer variable p & a = (write) * p = (write)... = & a (read)... = * p (read) like a constant like a variable Overview 17

&a and *p examples * & a a p *p & The value of p is an address. The data at that address is *p &p p & int a ; int * p ; integer variable a pointer variable p Overview 18

The & and * operators [int a] * & a a & a a & *(& a) = a int a ; integer variable a int a ; integer variable a Overview 19

The & and * operators [int *p] * p *p p *p & * & p p & p p & int * p ; pointer variable p int * p ; pointer variable p Overview 20

The & and * operators [int *p] * p *p p *p & &(*p) = p *(p) = *p * & p p & p p & &(p) = &p *(&p) = p value of p ( an address) value of p ( an address) Overview 21

The & and * operators [int *p] The address of a variable : Address of operator & &(p) = &p &(*p) = p the value at an address : Dereferencing operator * *(&p) = p *(p) = *p int * p ; pointer variable p int * p ; pointer variable p Overview 22

The & and * cancel each other &(*p) = p *(& p) = p [2] * & p p & p p *(& p) = p [1] & [1] * p *p &(*p) = p p *p [2] & Overview 23

Pointer Type Declaration int a; &a a a holds integer value int *p; &p p p holds address *p holds integer value p *p value of p ( an address) Overview 24

Address assignment to a pointer variable int *p; p = &a; Address assignment where: the address of p what: the value of p &a (address of a) The value of p is the address &a p points to &a p points to where the variable a is stored Overview 25

Address assignment example int *p; address data p = &a; Address assignment &a: 4500 &b: 4510 a: 40 b: 50 &c: 4520 c: 60 &mean: 4530 mean: 50 &p: 4540 p: 4500 Overview 26

Value assignment to a pointer variable int *p; p = &a; *p = 55; Value assignment where: the location where the value of p points to (value of p address) what: the data at p 55 (an integer value) Overview 27

Value assignment to a pointer variable int *p; address data p = &a; *p = 55; Value assignment p: &a: 4500 &b: 4510 *p: a: 55 b: 50 &c: 4520 c: 60 &mean: 4530 mean: 50 &p: 4540 p: 4500 Overview 28

Function examples passing values function call int x=3, y=5;... S = vsum ( x,... y); &x &y x y int vsum (int a, int b) { } return (a + b); function definition &a &b a=x b=y Overview 29

Function examples passing addresses function call int x=3, y=5;... S = asum ( &x,... &y); &x &y x y int asum (int *a, int *b) { } return (*a + *b); function definition &a &b a=&x b=&y Overview 30

Comparison int x, y; int x, y;... S = vsum ( x, y); S = asum ( &x, &y);... int vsum (int a, int b) { int asum (int *a, int *b) { } return (a + b); } return (*a + *b); a = x; b = y; a + b = x + y a = &x; b = &y *a = *(&x); *b = *(&y) *a + *b = x + y Overview 31

1-way vs. 2-way int x, y; int x, y; S = vsum ( x, y); S = asum ( &x, &y); int vsum (int a, int b) { int val = a + b; int asum (int *a, int *b) { int val = *a + *b; } a = b = 0; return (val); } *a = *b = 0; return (val); *(&x) = *(&y) = x = y = 0 a = x; b = y; a = 0 b = 0 a = &x; b = &y x = *(&x) = *a = 0 y = *(&y) = *b = 0 Overview 32

Differences int x, y; int x, y; S = vsum ( x, y); S = asum ( &x, &y); int vsum (int a, int b) { int val = a + b; int asum (int *a, int *b) { int val = *a + *b; } a = b = 0; return (val); } *a = *b = 0; return (val); *(&x) = *(&y) = x = y = 0 x, y : no change x, y : changed to zeros Overview 33

printf() : the built-in function Expected Output The mean of three numbers a = 40 b = 50 c = 60 mean(40, 50, 60) => 50 printf( The mean of three numbers \n ); printf( a = %d \n, a); printf( b = %d \n, b); printf( c = %d \n, c); printf( mean (%d, %d, %d) => %d \n, a, b, c, mean); Overview 34

scanf() : another built-in function Input Example Enter three numbers! a = 40 b = 50 c = 60 address &a: 4500 value 40 &b: 4510 50 &c: 4520 60 printf( Enter three numbers! \n ); printf( a = ); scanf( %d, &a); printf( b = ); scanf( %d, &b); printf( c = ); scanf( %d, &c); Overview 35

The Main Function (1) main (void) { int a, b, c; int mean; } a = 40; b = 50; c = 60; mean = (a + b + c) / 3; printf( The mean of three numbers \n ); printf( a = %d \n, a); printf( b = %d \n, b); printf( c = %d \n, c); printf( mean (%d, %d, %d) => %d \n, a, b, c, mean); Overview 36

The Main Function (2) main (void) { int a, b, c; int mean; } printf( Enter three numbers! \n ); printf( a = ); scanf( %d, &a); printf( b = ); scanf( %d, &b); printf( c = ); scanf( %d, &c); mean = (a + b + c) / 3; printf( The mean of three numbers \n ); printf( a = %d \n b = %d \n c = %d \n, a, b, c); printf( mean (%d, %d, %d) => %d \n, a, b, c, mean ); Overview 37

The Main Function (3) main (void) { int a, b, c; int mean; get_numbers(? ); compute_mean(? ); print_numbers(? ); } Overview 38

Function compute_mean() int compute_mean (int x, int y, int z) ; function prototype main (void) { int mean; mean = compute_mean(40, 50, 60); * Call by Value } int values are copied int compute_mean (int x, int y, int z) { int avg; * Local Variable avg = (x + y + z) / 3; return( avg ); * Return Value } Overview 39

Function get_numbers() void get_numbers (int *x, int *y, int *z) ; function prototype main (void) { int a, b, c; get_numbers(&a, &b, &c); * Call by Reference } addresses are copied void get_numbers (int * x, int * y, int * z) { printf( Enter three numbers! \n ); printf( a = ); scanf( %d, x); printf( b = ); scanf( %d, y); printf( c = ); scanf( %d, z); } * No Return Value Overview 40

Call by Value & Call by Reference * Call by Value mean = compute_mean(40, 50, 60); 40 50 60 int compute_mean (int x, int y, int z) &x x=40 &y y=50 &z z=60 * Call by Reference get_numbers(&a, &b, &c); &a 40 &b 50 &c 60 void get_numbers (int * x, int * y, int * z) &x x=&a &y y=&b &z z=&c Overview 41

Change the caller s variables * Call by Reference address value &a 40 &b 50 &c 60 Caller s Variables &x x=&a &y y=&b &z z=&c Now, values of a, b, c are changed in the get_numbers() function *x= 100; *y= 200; *z= 300; a= 100; b= 200; c= 300; The callee can change the values of the caller s variables &a 100 &b 200 &c 300 Overview 42

Function print_numbers() void print_numbers (int x, int y, int z, int avg) { printf( The mean of three numbers \n ); printf( a = %d \n b = %d \n c = %d \n, x, y, z); printf( mean (%d, %d, %d) => %d \n, x, y, z, avg); } * Call by Value * No Return Value main (void) { int a, b, c; int mean; } print_numbers(a, b, c, mean); Overview 43

The Main Function (4) void get_numbers (int *x, int *y, int *z); int compute_mean (int x, int y, int z); void print_numbers (int x, int y, int z, int avg); Prototypes main (void) { int a, b, c; int mean; get_numbers(&a, &b, &c); mean = compute_mean(a, b, c); } print_numbers(a, b, c, mean); Overview 44

Pointers and Arrays p+0 p+1 p+2 *(p+0) *(p+1) *(p+2) a+0 a+1 a+2 *(a+0) *(a+1) *(a+2) a[0] a[1] a[2] & p p int * p ; int a[3] ; pointer variable p array name a Overview 45

The Main Parameters (1) #include <stdio.h> int main(int argc, char *argv[]) { int i; printf("argc= %d \n", argc); for (i=0; i<argc; ++i) { } printf("argv[%d] = %s \n", i, argv[i]); } Overview 46

The Main Parameters (2) young@usys ~ $./a.out argc= 1 argv[0] =./a.out young@usys ~ $./a.out one two three argc= 4 argv[0] =./a.out argv[1] = one argv[2] = two argv[3] = three young@usys ~ $./a.out one two three four argc= 5 argv[0] =./a.out argv[1] = one argv[2] = two argv[3] = three argv[4] = four Overview 47

The Main Parameters (3) young@usys ~ $./a.out one two argc= 3 argv[0] =./a.out argv[1] = one argv[2] = two. argv[0] / argv[1] argv[2] t w o o n e \0 a. o u t \0 \0 Overview 48

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun