Programming Language B

Similar documents
Programming Language B

Programming Language B

Programming Language A

Programming Language A

Programming Language A

Programming Language B

Programming Language B

ESc101: Pointers and Arrays

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

C Programming Language

Programming Language B

Programming Language A

Fundamental of Programming (C)

Structured programming

Dynamic Allocation of Memory Space

POINTER & REFERENCE VARIABLES

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Example: Pointer Basics

Lesson 7. Reading and Writing a.k.a. Input and Output

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Basic and Practice in Programming Lab7

CSC 270 Survey of Programming Languages. What is a Pointer?

Class Information ANNOUCEMENTS

Programming Language B

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Dynamic memory allocation (malloc)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Variation of Pointers

PDS Class Test 2. Room Sections No of students

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Intermediate Programming, Spring 2017*

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Computer Programming Unit 3

BİL200 TUTORIAL-EXERCISES Objective:

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Memory Management. CSC215 Lecture

DECLARAING AND INITIALIZING POINTERS

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Computer Programming: Skills & Concepts (CP) Variables and ints

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Arrays and Pointers in C. Alan L. Cox

Summary of Lecture 4. Computer Programming: Skills & Concepts (INF-1-CP1) Variables; scanf; Conditional Execution. Tutorials.

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Language comparison. C has pointers. Java has references. C++ has pointers and references

Programming for Electrical and Computer Engineers. Pointers and Arrays

CS61, Fall 2012 Section 2 Notes

Dynamic memory allocation

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS 108 Computing Fundamentals. October/November Array Bootcamp

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Introduction to C. Systems Programming Concepts

Dynamic Memory Allocation

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture 04 Introduction to pointers

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Principles of C and Memory Management

Lecture06: Pointers 4/1/2013

ECE 15B COMPUTER ORGANIZATION

Memory Allocation. General Questions

Character Strings. String-copy Example

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

ESC101N: Fundamentals of Computing End-sem st semester

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

2.2 Pointers. Department of CSE

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

C Tutorial Pointers, Dynamic Memory allocation, Makefile

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Procedural Programming

int marks[10]; // fixed size and fixed address No change in Memory address.

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Arrays, Strings, and Pointers

M.CS201 Programming language

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

PROGRAMMAZIONE I A.A. 2017/2018

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Programming with Indexed Variables

Fundamentals of Programming Session 12

EM108 Software Development for Engineers

CS240: Programming in C

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Arrays, Strings, and Pointers

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

CS A331 Programming Language Concepts

Transcription:

Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13

Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Takako Nemoto (JAIST) 7 January 2 / 13

Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Variables declared with * is called pointer. In this example, isako and hiroko are pointers. Their type are called pointer type to int-type object, pointer type to int, or int * type. If you declare them as int *isako, hiroko then hiroko has int type. Takako Nemoto (JAIST) 7 January 2 / 13

Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Pointers has the value of addresses of the objects with declared type. When the address of x is assigned into a pointer p, we say p points x. In this example, isako points sato and hiroko points first masaki and changed to sanaka. Unary * operator is applied to pointers and takes the object the pointer points. If the pointer p points the object x, then *p is an alias (alternative name) for x. E.g., *hiroko = 180; has the same meaning masaki = 180;. Takako Nemoto (JAIST) 7 January 3 / 13

Pointers and arrays #include <stdio.h> //10-9.c int a[5] = {1,2,3,4,5; int *p = a; for(i = 0; i < 5; i++) printf("&a[%d] = %p p+%d = %p\n", i, &a[i], i, p + i); int *p = a; is same as int *p = &a[0];. p + i is the pointer to a[i]. (Quiz 12) Change the 5th line as *p = &a[2]; and check the value of p - 2, p - 1, p, p + 1, p + 2 using printf. Send the screen shot of the execution result. Takako Nemoto (JAIST) 7 January 4 / 13

Pointers and arrays #include <stdio.h> //10-9.c int a[5] = {1,2,3,4,5; int *p = a; for(i = 0; i < 5; i++) printf("&a[%d] = %p p+%d = %p\n", i, &a[i], i, p + i); An array is recognized as a pointer to its first element, except for the following two cases: sizeof(a) is the size of array a itself, not the size of the pointer to a s first element. & a is not the pointer to the first element of a, but the pointer to array a in total. Recall the following does not work: int a[5] = {1,2,3,4,5; int b[5]; a = b; but we can copy them as in the example. Takako Nemoto (JAIST) 7 January 5 / 13

Pointers and arrays #include <stdio.h> int a[5] = {1,2,3,4,5; int *p = a; Recall that if the address of var is assigned to a pointer p, then *p is an alias of var. Similarly, *(p+i) is an alias of a[i] in the example. for(i = 0; i < 5; i++) printf("a[%d] = %d *(a+%d) = %d p[%d] = %d *(p+%d) = %d\n", i, a[i], i, *(a+i), i, p[i], i, *(p + i)); for(i = 0; i < 5; i++) printf("&a[%d] = %p a+%d = %p &p[%d] = %p p+%d = %p\n", i, &a[i], i, (a+i), i, &p[i], i, (p + i)); Takako Nemoto (JAIST) 7 January 6 / 13

Functions and pointer #include <stdio.h> void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; The Parameter v[] in ary_set is recognized as *v. Even v[] is replaced by v[5], the length 5 is ignored. This is why we have to set another parameter n for the length of v. ary_set(a, 5, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 7 / 13

Some more examples #include <stdio.h> //ex10-5.c void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; ary_set(&a[2], 2, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 8 / 13

Some more examples #include <stdio.h> //ex10-5.c void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; In the function call ary_set(&a[2], 2, 99) the pointer to a[2] and the length 2 are given as arguments. Hence only the value of a[2] and a[3] are changed. ary_set(&a[2], 2, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 9 / 13

Some more examples #include <stdio.h> //10c-1.c int i, a[4]; 0[a] = a[1] = *(a + 2) = *(a + 3) = 7; for (i = 0; i < 4; i++) printf("a[%d] = %d\n", i, a[i]); [] is a commutative operator! Takako Nemoto (JAIST) 7 January 10 / 13

Advanced topic #include <stdio.h> //malloc-1.c int n; puts("input an integer: "); scanf("%d", &n); int v[n]; for (i = 0; i < n; i++) v[i] = i; for (i=0;i < n; i++) printf("%d ", v[i]); printf("\n"); This example might not work, depending on compilers. The reason is that static allocations are used for arrays. Takako Nemoto (JAIST) 7 January 11 / 13

Advanced topic #include <stdio.h> //malloc-2.c #include <stdlib.h> int main(void) { int *v; int i, n; printf("input a number : "); scanf("%d", &n); v = (int *)malloc(n * sizeof(int)); if (v == NULL) exit(0); for (i=0; i<n; i++) v[i] = i; for (i=0;i < n; i++) printf("%d ", v[i]); printf("\n"); free(v); malloc is a function which keeps dynamic memory of designated size, contained in stdlib.h. If it fails to take memory allocations, it returns NULL and the program does not work. Then you can set the size of arrays as in the example. To use malloc, you should check the return value of malloc like if (v == NULL) exit(0); and HAVE TO clear the memory allocation in the end by free(v); Takako Nemoto (JAIST) 7 January 12 / 13

Today s homework 1 Do Exercise 10-2 ( 10-2): Make function to renew the date to the previous one and to the next one. Do not forget bissextile years: decrement_date(int *y, int *m, int *d){... increment_date(int *y, int *m, int *d){... 2 Do Exercise 10-4 ( 10-4): Make a function set_idx(int *v, int n){... such that, for the input of an array with n int-type elements, it assigns the value same as the index to each element of the array. 3 Read sections 10 and 11. As usual, send the programs and the text file as attached files to me via e-mail, with the title Homework Lecture 12 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 7 January 13 / 13