Basic and Practice in Programming Lab7

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

Principles of C and Memory Management

Arrays and Pointers (part 1)

Functions. Arash Rafiey. September 26, 2017

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

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

Arrays and Pointers (part 1)

Lab 3. Pointers Programming Lab (Using C) XU Silei

Principles of Programming. Chapter 6: Arrays

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Programming Language B

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?

ECE264 Fall 2013 Exam 1, September 24, 2013

Fundamental of Programming (C)

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

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

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

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

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Example: Pointer Basics

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

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

[0569] p 0318 garbage

Programming. Pointers, Multi-dimensional Arrays and Memory Management

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Lecture 04 Introduction to pointers

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

C Programming Language

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

High Performance Programming Programming in C part 1

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

More Pointers Week 11

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Arrays and Strings. Arash Rafiey. September 12, 2017

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C programming basics T3-1 -

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Practice Sheet #07 with Solutions

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

CSE2301. Arrays. Arrays. Arrays and Pointers

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Subject: Fundamental of Computer Programming 2068

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Multidimension array, array of strings

Content. In this chapter, you will learn:

AMCAT Automata Coding Sample Questions And Answers

Course organization. Course introduction ( Week 1)

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

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Programming Studio #9 ECE 190

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

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

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

POINTER & REFERENCE VARIABLES

CSC231 C Tutorial Fall 2018 Introduction to C

BSM540 Basics of C Language

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

ECE264 Summer 2013 Exam 1, June 20, 2013

Exercise Session 2 Simon Gerber

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

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

CS 61c: Great Ideas in Computer Architecture

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

today cs3157-fall2002-sklar-lect05 1

Introduction to C. Systems Programming Concepts

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

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

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

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

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

BSM540 Basics of C Language

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

Introduction to Scientific Computing and Problem Solving

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

CS240: Programming in C

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

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Basic and Practice in Programming Lab 10

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

C Tutorial Pointers, Dynamic Memory allocation, Makefile

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

C - Func(on, Pointer, Array. Zhaoguo Wang

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Dynamic Data Structures. CSCI 112: Programming in C

First of all, it is a variable, just like other variables you studied

Solutions to Assessment

DECLARAING AND INITIALIZING POINTERS

Transcription:

Basic and Practice in Programming Lab7

Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10; value 0x00000004 0x00000000

Variable and Its Address (2/2) /* Practice 1 : Variable and Address */ #include <stdio.h> int main(void) int a = 10; printf( value: %d address: %p\n, a, &a); return 0;

A Consideration Let s consider follow function form int add (int x, int y) x += y; return x;

A Consideration Let s consider follow function form int add (int x, int y) x += y; return x; int add (_x, _y) int x, y; x = _x; y = _y; x += y; return x; Realistic executable function form The function uses arguments as form of local variables

A Consideration Let s consider follow function form int add (int x, int y) x += y; return x; int main (void) int x = 10, y = 10, sum; sum = add(x, y); return 0; What is the value of x? int add (_x, _y) int x, y; x = _x; y = _y; x += y; return x; Realistic executable function form The function uses arguments as form of local variables

Call by Value /* Practice 2 : Call by value */ #include <stdio.h> int add (int x, int y) x += y; return x; int main(void) printf( %d + %d = %d\n, 10, 20, add(10, 20)); return 0;

Call by Value /* Practice 3 : Call by value 2 */ #include <stdio.h> int add (int x, int y) x += y; printf ( x: %p y: %p, &x, &y); return x; int main(void) int x = 10, y = 20; printf( x: %p y: %p, &x, &y); printf( %d + %d = %d\n, x, y, add(x, y)); return 0;

A Question How to use same variable Across two functions The answers are Using global variable but, Using global variable is NOT recommended Security, consistency, reliability problems Using variable s address instead of variable itself Pointer

Pointer It is not a concept Just a method There is no serious philosophy in the pointer Pointer is just variable for containing address int a = 10; int *p = &a; //declaration form of pointer //p == &a, *p == a

Pointer Memory address 10 0x00000010 int a = 10; int *p; p = &a; value 0x00000010 value 0x00000004 *p 0x00000000

Functions Two types of function call Call by value Call by reference void func1(int a) Call by value Call by value Argument is just value Call by reference Argument is given by variable s address void func2(int *a) Call by reference

Call by Reference (1/3) Swap function Exchange two variable s contents int a = 10, b = 20; a 10 b 20 swap(a, b); a 20 b 10

Call by Reference (2/3) /* Practice 4 : Call by reference example1*/ #include <stdio.h> int main(void) int a = 10, b = 20; void swap (int x, int y) int temp; temp = y; y = x; x = temp; printf( a : %d b : %d, a, b); swap(a, b); printf( a : %d b : %d, a, b); return 0;

Call by Reference (3/3) /* Practice 5 : Call by reference example2*/ #include <stdio.h> int main(void) int a = 10, b = 20; void swap (int *x, int *y) int temp; temp = *y; *y = *x; *x = temp; printf( a : %d b : %d, a, b); swap(&a, &b); printf( a : %d b : %d, a, b); return 0;

Reference? swap Call by value x 10 main y 20 a b 10 0x1000 20 0x1004

Reference? swap Call by value x 20 main y 10 a b 10 0x1000 20 0x1004

Reference? swap Call by value x 20 main y 10 a b 10 0x1000 20 0x1004 swap *x 0x1000 Call by reference *y 0x1004

Reference? swap Call by value x 20 main y 10 a 10 0x1000 b 20 0x1004 swap *x 0x1000 0x1000 10 Call by reference *y 0x1004 0x1004 20

Reference? swap Call by value x 20 main y 10 a b 20 0x1000 10 0x1004 Reference swap *x 0x1000 0x1000 20 Call by reference *y 0x1004 0x1004 10

Type Pointer has type Integer, character, floating point Pointer must be used to point to same type variable ip = &int_val int *ip int int_val = 10; char *cp cp = &char_val char char_val = a ; fp = &f_val float *fp float f_val = 3.14;

Pointer of Pointer Pointer is just a variable Of course, it has its address /* Practice 4 : Pointer of pointer */ #include <stdio.h> int main (void) int x = 10, *p, **pp; p = &x; pp = &p; printf ( %d %d %d\n, x, *p, **pp); printf ( %p %p %p\n, &x, p, *pp); printf ( %p %p\n, &p, pp); return 0;

Exercise 1 Left shifter Input is given as form of sequence of 5 distinct charac ters Last input is the number of shift Skeleton code is given Check the homepage You must not print last input A B C D E 3 D E A B C

Array (1/8) Pointer Container of memory address Accessing variable s value by using * int *p; p = &a; Array Reference of continuous memory addresses Not a variable int arr[10] arr address of arr[0] arr[2] arr[4] arr[6] arr[8] arr[1] arr[3] arr[5] arr[7] arr[9] arr == &arr[0] *arr?

Array (2/8) /* Practice 1 : Array 1*/ #include <stdio.h> int main (void) int arr[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; printf( %p %p\n, &arr[0], arr); printf( %d %d\n, arr[0], *arr); return 0;

Array (3/8) Array in memory Address is linearly increasing By size of type int arr[5] arr[0] arr[1] arr[2] arr[3] arr[4] 0x1000 0x1004 0x1008 0x100c 0x1010 sizeof(int) Memory

Array (4/8) /* Practice 2 : Array 2*/ #include <stdio.h> int main (void) int arr[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; int i; for (i=0;i<10;i++) printf( %p %p\n, &arr[i], arr+i); arr[i] == arr+i == &arr[0] + i*sizeof(int) for (i=0;i<10;i++) printf( %d %d\n, arr[i], *(arr+i)); return 0;

Array (5/8) /* Practice 3 : Array 3*/ #include <stdio.h> int main (void) char arr[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; int i; for (i=0;i<10;i++) printf( %p %p\n, &arr[i], arr+i); arr[i] == arr+i == &arr[0] + i*sizeof(char) for (i=0;i<10;i++) printf( %d %d\n, arr[i], *(arr+i)); return 0;

Array (6/8) 2D array int arr[2][5] arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[0][4] arr[1][0] arr[1][1] 0x1000 0x1004 0x1008 0x100c 0x1010 0x1014 0x1018 sizeof(int) sizeof(int) * 5 sizeof(int) Memory

Array (7/8) /* Practice 4 : 2D Array 1*/ #include <stdio.h> int main (void) int arr[2][5] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; printf( %p %p %p\n, arr, arr[0], &arr[0][0]); printf( %p %p %p\n, arr+1, arr[1], &arr[1][0]); printf( %p %p %p\n, *(arr+1)+2, arr[1]+2, &arr[1][2]); return 0;

Array (8/8) /* Practice 5 : 2D Array 2*/ #include <stdio.h> int main (void) int arr[2][5] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; printf( %p %p %p\n, arr, arr[0], &arr[0][0]); printf( %p %p %p\n, arr+1, arr[1], &arr[1][0]); printf( %p %p %p\n, *(arr+1)+2, arr[1]+2, &arr[1][2]); printf("%d %d %d\n", **arr, *arr[0], arr[0][0]); printf("%d %d %d\n", **(arr+1), *arr[1], arr[1][0]); printf("%d %d %d\n", *(*(arr+1)+2), *(arr[1]+2), arr[1][2]); return 0;

Pointer (1/6) Pointer can be used as array int arr[5]; int *p; p = &arr[0]; //same as p = arr; *p 0x1000 arr[0] arr[1] arr[2] arr[3] arr[4] 0x1000 Memory

Pointer (2/6) /* Practice 6: Pointer 1*/ #include <stdio.h> int main (void) int arr[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; int i; int *p; p = &arr[0]; //same as p = arr for (i=0;i<10;i++) printf( %p %p %p\n, &p[i], p+i, &arr[i]); for (i=0;i<10;i++) printf( %d %d %d\n, p[i], *(p+i), arr[i]); return 0;

Pointer (3/6) Pointer must be carefully used int arr[5]; int *p; printf( %d\n, p[1]); *p p[1] == *(p + sizeof(int)) garbage arr[0] arr[1] arr[2] arr[3] arr[4] 0x1000 Memory?

Pointer (4/6) /* Practice 7 : Pointer misuse*/ #include <stdio.h> int main (void) int arr[10] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; int i; int *p; //p = &arr[0]; for (i=0;i<10;i++) printf( %p %p %p\n, &p[i], p+i, &arr[i]); for (i=0;i<10;i++) printf( %d %d %d\n, p[i], *(p+i), arr[i]); return 0;

Pointer (5/6) /* Practice 8 : Pointer 2*/ #include <stdio.h> int string_length(char *s) int n; for (n = 0;*s!= \0 ;s++) n++; return n; int main (void) char str[10]; scanf( %s, &str[0]); printf( %s\nlength : %d\n, str, string_lenth(str)); return 0;

Pointer (6/6) String copy strcpy() is defined in standard C library

Pointer Example /* Practice 9 : String copy 2*/ #include <stdio.h> void mystrcpy(char s[], char d[]) int i; for (i = 0; s[i]!= \0 ; i++) d[i] = s[i]; d[i] = s[i]; int main (void) char str[10], dest[10]; scanf( %s, &str[0]); printf( %s\nlength : %d\n, str, string_lenth(str)); return 0;

Pointer Array Pointer can be declared as array Example: string array char *str_array[5] str_array[0] str_array[1] str_array[2] str_array[3] str_array[4] All work and no play makes Jack a dull boy

Pointer Array /* Practice 10 : Pointer array */ int main (void) char *str_array[5] = All work, and, no play, makes, Jack a dull boy ; printf( %s %s %s %s %s\n, str_array[0], str_array[1], str_array[2], str_array[3], str_array[4]); return 0;

Pointer Array /* Practice 11 : Pointer array 2 */ int main (void) char *str_array[5] = All work, and, no play, makes, Jack a dull boy ; printf( %s %s %s %s %s\n, str_array[0], str_array[1], str_array[2], str_array[3], str_array[4]); str_swap(&str_array[0], &str_array[2]); printf( %s %s %s %s %s\n, str_array[0], str_array[1], str_array[2], str_array[3], str_array[4]); return 0; void str_swap (char **s, char **d) char *temp; temp = *s; *s = *d; *d = *s;

Exercise 1 Modifying strcpy() Two arguments Source string Destination string Don t use local variable Using pointer operations Modifying practice 9 s code

Exercise 2 Word changer Input is composed of three strings A sentence not including white space Target word Changing word You find target words in a sentence, and change by c hanging word Two words have same length AllworkandnoplaymakesJackadullboy work play AllplayandnoplaymakesJackadullboy