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

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

Overview (1A) Young Won Lim 9/25/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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 2. Young Won Lim 11/24/17

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

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

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

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

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

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

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

Friday, February 10, Lab Notes

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

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

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

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

Slides adopted from T. Ferguson Spring 2016

RAM (1A) Young Won Lim 11/12/13

Computer Programming Lecture 12 Pointers

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

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

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

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

CSC258: Computer Organization. Functions and the Compiler Tool Chain

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

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

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

PROGRAMMAZIONE I A.A. 2017/2018

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

Worksheet 4 Basic Input functions and Mathematical Operators

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

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CSE 333 Autumn 2014 Midterm Key

Fundamental of Programming (C)

Structure (1A) Component

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

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

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

Programming and Data Structure

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

CORDIC Fixed Point Simulation. Young Won Lim 2/28/12

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 value &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

The & and * operators * & a a p *p & &p p & int a ; int * p ; integer variable a pointer variable p Overview 15

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

The & and * operators [int *p] value of p ( an address) value of p ( an address) &(p) = &p * *(&p) = p & p p & p p & * p *p p *p & &(*p) = p *(p) = *p int * p ; pointer variable p int * p ; pointer variable p Overview 17

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 18

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 19

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

Address assignment to a pointer variable int *p; address value p = &a; where: addr of p what: value of p : &a (addr of a) &a: 4500 &b: 4510 &c: 4520 a: 40 b: 50 c: 60 &mean: 4530 mean: 50 &p: 4540 p: 4500 Overview 21

Value assignment to a pointer variable int *p; address value p = &a; p: &a: 4500 a: 55 &b: 4510 b: 50 *p = 55; where: p points to ( value of p ) what: value 55 &c: 4520 c: 60 &mean: 4530 mean: 50 &p: 4540 p: 4500 Overview 22

Passing values and addresses to a function 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; x + y = a + b a = &x; b = &y *a = *(&x); *b = *(&y) x + y = *a + *b Overview 23

Passing values and addresses to a function int x, y; int x, y; S = vsum ( x, y); S = asum ( &x, &y); int vsum (int a, int b) { int val = a + b; a = b = 0; return (val); int asum (int *a, int *b) { int val = *a + *b; *a = *b = 0; return (val); } } *(&x) = *(&y) = x = y = 0 x, y : no change x, y : changed to zeros Overview 24

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 25

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 26

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 27

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 28

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

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

Function get_numbers() void get_numbers (int *x, int *y, int *z) ; main (void) { int a, b, c; * Call by Reference get_numbers(&a, &b, &c); } 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 31

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 32

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 33

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 34

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 35

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 36

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 37

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 o u t \0 \0 Overview 38

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