Fundamental of Programming (C)

Similar documents
Arrays and Pointers in C. Alan L. Cox

Dynamic memory allocation

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

Fundamentals of Programming Session 20

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

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

Content. In this chapter, you will learn:

Computer Programming Unit 3

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

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

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

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

Lecture 04 Introduction to pointers

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

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

Chapter 6 - Pointers

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

Dynamic memory allocation (malloc)

EM108 Software Development for Engineers

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

Arrays and Memory Management

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Procedural programming with C

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

Fundamentals of Programming Session 20

PROGRAMMAZIONE I A.A. 2017/2018

Fundamental of Programming (C)

POINTER & REFERENCE VARIABLES

Dynamic Allocation of Memory Space

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

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

Goals of this Lecture

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

Tutorial 10 Pointers in C. Shuyue Hu

Fundamental of Programming (C)

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

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

Introduction to C. Systems Programming Concepts

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

Memory Management. CSC215 Lecture

Understanding Pointers

Principles of C and Memory Management

Array Initialization

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Lecture 05 POINTERS 1

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

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

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

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

... Lecture 12. Pointers

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

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

Programming Language B

POINTER AND ARRAY SUNU WIBIRAMA

Memory Allocation. General Questions

211: Computer Architecture Summer 2016

Subject: Fundamental of Computer Programming 2068

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

APSC 160 Review Part 2

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

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

Pointers and File Handling

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

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

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Fundamentals of Programming

Lecture 3. More About C

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

Arrays, Pointers, and Strings

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

Character Strings. String-copy Example

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

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

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Programming for Electrical and Computer Engineers. Pointers and Arrays

MA 511: Computer Programming Lecture 10: Pointers (Contd..) Partha Sarathi Mandal

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Variation of Pointers

C programming basics T3-1 -

CS 0449 Sample Midterm

Quick review pointer basics (KR ch )

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

Programming for Engineers Pointers

Course organization. Course introduction ( Week 1)

Lectures 5-6: Introduction to C

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

FUNCTIONS POINTERS. Pointers. Functions

SYSC 2006 C Winter 2012

Arrays, Strings, and Pointers

Arrays, Pointers and Memory Management

Pointers. Pointer References

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

Transcription:

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46

Outline Defining and using Pointers Operations on pointers Arithmetic Logical Pointers and Arrays Memory Management for Pointers Department of Computer Engineering 2/46

Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable int x; // x = 22f54; x = 22f54; // Error When a value is assigned to a variable, the value is actually placed to the memory that was allocated x = 3; // (x) = 3; x = 3; // Error x 22F54 522F5 22F56 22F57 00000000 00000000 00000000 00000011 Department of Computer Engineering 3/46

Pointer Fundamentals When the value of a variable is used, the contents in the memory are used y = x; x 22F54 00000000 y = (x); 522F5 22F56 22F57 x can get the address of x (referencing operator ) The address can be passed to a function: scanf("%d", x); The address can also be stored in a variable 00000000 00000000 00000011 Department of Computer Engineering 4/46

Pointers To declare a pointer variable type PointerName; x 22F50 22F51 For example: int x; int p; //p is a int pointer // char p2; p 22F52 22F53 22F54 522F5 22F56 22F57 00 02 2F 50 p1 = x; / Initializing p1 / Department of Computer Engineering 5/46

Initializing Pointers Like other variables, always initialize pointers before using them!!! void main() { int x; int p; scanf("%d", p); / Don t / p = x; scanf("%d", p); / Correct / } x 22F50 22F51 22F52 22F53 Compiler p 22F54 522F5 22F56 22F57 00 02 2F 50 Developer Department of Computer Engineering 6/46

Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables To do this, use the operator (dereferencing operator) Depending on different context, has different meanings For example: int n, m = 3, p; p = m; // Initializing n = p; printf("%d\n", n); // 3 printf("%d\n", p); // 3 P = 10; printf("%d\n", n); // 3 printf("%d\n", p); // 10 n m p 3 n m p 3 n m p 3 3 n m p 3 10 Department of Computer Engineering 7/46

An Example int m = 3, n = 100, p, q; p = m; printf("m is %d\n", p); // 3 m++; printf("now m is %d\n", p); // 4 m n p q 3 100 m n p q 4 100 m n p q 4 100 p = n; printf("n is %d\n", p); // 100 p = 500; printf("now n is %d\n", n); // 500 m n 4 500 m n 4 500 m n 500 500 q = m; q = p; printf("now m is %d\n", m); // 500 p q p q p q Department of Computer Engineering 8/46

Pointer Assignment int a = 2, b = 3; int p1, p2; p1 = a; p2 = b; printf("%p %p", p1,p2); p1 = p2; printf("%d %d", p1, p2); p2 = p1; printf("%p %p", p1, p2); printf("%p %p", p1, p2); b a p1 p2 3 2 b a p1 p2 3 3 Department of Computer Engineering 9/46

An Example int i = 25; int p; p = i; Flow of address is complier dependent p 22ff40 22ff41 22ff42 22ff43 i 22ff44 printf("%x %x", p, i); // 22ff40 22ff44 printf("%x %p", p, p); // 22ff44 0022ff44 printf("%d %d", i, p); // 25 25 Department of Computer Engineering 10/46

Constant Pointers A pointer to const data does not allow modification of the data through the pointer const int a = 10, b = 20; a = 5; // Error const int p; int q; p = a; p = 100; // Error : p is (const int ) p = b; q = a; q = 100; // OK!!! Department of Computer Engineering 11/46

Constant Pointers int x; / define x / int y; / define y / /ptr is a constant pointer to an integer that can be modified through ptr, but ptr always points to the same memory location / int const ptr = x; ptr = 7; / allowed: ptr is not const / ptr = y; / error: cannot assign new address / Department of Computer Engineering 12/46

Constant Pointers int x = 5; / initialize x / int y; / define y / /ptr is a constant pointer to a constant integer. ptr always points to the same location; the integer at that location cannot be modified / const int const ptr = x; ptr = 7; / error: cannot assign new value / ptr = y; / error: cannot assign new address / Department of Computer Engineering 13/46

Pointer to pointer int main(void) { int s = 1; int t = 1; s t 1 1 s t 2 1 s t 2 1 int ps = s; int pps = ps; int pt = t; pps = 2; ps pps pt ps pps pt ps pps pt pt = ps; pt = 3; return 0; } Department of Computer Engineering 14/46

Multiple indirection int a = 3; int b = a; int c = b; int d = c; int f = d; a b c d f 3 Department of Computer Engineering 15/46

NULL Pointer Special constant pointer NULL Points to no data Dereferencing illegal To define, include <stdlib.h> or <stdio.h> int q = NULL; Department of Computer Engineering 16/46

Generic Pointers: void void : a pointer to anything void p; int i; char c; p = i; p = c; putchar((char )p); type cast: tells the compiler to change an object s type (for type checking purposes does not modify the object in any way) Lose all information about what type of thing is pointed to Reduces effectiveness of compiler s type-checking Can t use pointer arithmetic Department of Computer Engineering 17/46

Arithmetic Operations A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another int a, b; int p = a, q = b; p = p + q ; // Error p = p q; // Error p = p / q; // Error p = p - q; // OK p = p + 3; p += 1.6; // Error p %= q; // Error Department of Computer Engineering 18/46

Arithmetic Operations When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to For example, if the pointer p contains the address of a double precision variable and that address is 234567870, then the statement: p = p + 2; // 234567870 + 2 sizeof(double) would change p to 234567886 Department of Computer Engineering 19/46

Arithmetic Operations pointer + number char p; char a; char b; p = a; p -= 1; In each, p now points to b!!! (complier dependent) pointer number int p; int a; int b; p = a; p -= 1; b a p subtracts 1sizeof(char) to the memory address subtracts 1sizeof(int) to the memory address Pointer arithmetic should be used cautiously Department of Computer Engineering 20/46

Logical Operations Pointers can be used in comparisons int a[10], p, q, i; p = a[2]; q = a[5]; i = q - p; / i is 3/ i = p - q; / i is -3 / a[2] = a[5] = 0; i = p - q; // i = a[2] a[5] if (p < q)...; / true / if (p == q)...; / false / if (p!= q)...; / true / p q [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Department of Computer Engineering 21/46

Pointers and Arrays the value of an array name is also an address In fact, pointers and array names can be used interchangeably in many (but not all) cases The major differences are: Array names come with valid spaces where they "point" to. And you cannot "point" the names to other places. Pointers do not point to valid space when they are created. You have to point them to some valid space (initialization) Department of Computer Engineering 22/46

Pointers and Arrays Array pointer to the initial (0th) array element a a[0] a[i] (a+i) a[i] a + i Example: int a, p; p=a; p = 1; p[0] = 1; p int a[ 10 ], p; p = a[2]; p[0] = 10; p[1] = 10; printf("%d", p[3]); int a[ 10 ], p; a[2] = 10; a[3] = 10; printf("%d", a[5]); p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] a [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Department of Computer Engineering 23/46

Pointers and Arrays Array pointer to the initial (0th) array element a a[0] a[i] (a+i) a[i] a + i 0 a 1 a + 1 2 a + 2 3 a + 3 int i; int array[10]; int p; int array[10]; for (i = 0; i < 10; i++) { array[i] = ; } for (p = array; p < array[10]; p++) { p = ; } These two blocks of code are functionally equivalent Department of Computer Engineering 24/46

An Array Name is Like a Constant Pointer Array name is like a constant pointer which points to the first element of the array int const a int a[10], p, q; p = a; / p = a[0] / q = a + 3; / q = a[0] + 3 / a ++; / Error!!! / Department of Computer Engineering 25/46

Example int a[10], i; int p = a; // int p = a[0]; for (i = 0; i < 10; i++) scanf("%d", a + i); // scanf("%d", a[i]); for (i = 9; i >= 0; --i) printf("%d", (p + i)); // printf("%d", a[i]); //printf("%d", p[i]); for (p = a; p < a[10]; p++) printf("%d", p); Department of Computer Engineering 26/46

An example p int a[10], p, q; q q [0] [0] p = a[2]; [1] [1] q = p + 3; [2] [2] p = q 1; p++; [3] [4] [3] [4] [5] [5] q--; [6] [6] p = 123; [7] [7] q = p; [8] [8] q = p; [9] [9] printf("%d", q); / printf("%d", a[5]) / p 123 123 Department of Computer Engineering 27/46

An Example int a[10], p; a++; //Error a--; // Error a += 3; //Error p = a; // p = a[0]; p ++; //OK p--; // Ok P +=3; // Ok Department of Computer Engineering 28/46

Strings In C, strings are just an array of characters Terminated with \0 character Arrays for bounded-length strings Pointer for constant strings (or unknown length) char str1[15] = "Hello, world! ; H e l l o, w o r l d! \0 char str1[] char str2 = "Hello, world!"; = "Hello, world!"; H e l l o, w o r l d! \0 Department of Computer Engineering 29/46

An Example char str, s[] = "ALIREZA"; printf("%s", s); // ALIREZA printf(s); // ALIREZA printf("%s", s + 3); // REZA scanf("%s", s); scanf("%s", s[0]); str = s; while( str) putchar(str++); // s++ : Error Department of Computer Engineering 30/46

An Example void copy1(char const s1, const char const s2) { int i; / counter / / loop through strings / for ( i = 0; ( s1[ i ] = s2[ i ] )!= '\0'; i++ ); / do nothing in body / } void copy2(char s1, const char s2) { / loop through strings / for ( ; ( s1 = s2 )!= '\0'; s1++, s2++ ); / do nothing in body / } Department of Computer Engineering 31/46

Multi-Dimensional Arrays int a[row][col]; a[row][col] ((a + row) + col) a[row][col] (a[row] + col) a[row][col] a[row] + col a a[0][0] a[0] a[0] + 2 scanf(" %d ", a[0][0]) scanf(" %d ", a[0]) printf (" %d ", a[0][0]) printf(" %d ", a[0]) scanf(" %d ", a[2][2]) scanf(" %d ", a[2]+ 2) printf (" %d ", a[2][2]) printf(" %d ", (a[2] + 2)) a[0] [0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6] [0][7] [0][8] [0][9] a[1] [1][0] [1][1] [1][2] [1][3] [1][4] [1][5] [1][6] [1][7] [1][8] [1][9] a[2] [2][0] [2][1] [2][2] [2][3] [2][4] [2][5] [2][6] [2][7] [2][8] [2][9] a[3] [3][0] [3][1] [3][2] [3][3] [3][4] [3][5] [3][6] [3][7] [3][8] [3][9] a[4] [4][0] [4][1] [4][2] [4][3] [4][4] [4][5] [4][6] [4][7] [4][8] [4][9] Department of Computer Engineering 32/46

Array of Pointers char suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] H e a r t s \0 suit[1] D i a m o n d s \0 suit[2] C l u b s \0 suit[3] S p a d e s \0 Department of Computer Engineering 33/46

Array of Pointers int a=1, b=2, c=3, d=4; int k[4] = {a, b, c, d}; k[0] a 1 k[1] b 2 k[2] c 3 k[3] d 4 k[0] k[1] k[2] k[3] a b c d 1 2 3 4 printf("%d %d %d %d", k[0], k[1],k[2],k[3]); Department of Computer Engineering 34/46

Pointer to Function #include <stdio.h> void f1(float a){ printf("f1 %g", a);} void f2(float a){ printf("f2 %g", a);} int main(){ void (ptrf)(float a); ptrf = f1; ptrf(12.5); ptrf = f2; ptrf(12.5); getch(); return 0; } A function pointer is defined in the same way as a function prototype, but the function name is replaced by the pointer name prefixed with an asterisk and encapsulated with parenthesis Example: int (fptr)(int, char) fptr = some_function; (ftpr)(3,'a'); some_function(3,'a'); Department of Computer Engineering 35/46

Array of Functions #include<stdio.h> void func1() { printf("function 1 Called\n"); } void func2() { printf("function 2 Called\n"); } void func3() { printf("function 3 Called\n"); } int main(int argc, char argv[]) { void (ptr[3]) () = {func1, func2, func3}; int k = 0; for(k = 0; k < 3; k++) ptr[k](); getch(); return 0; } Department of Computer Engineering 36/46

Passing Arrays to Functions #include <stdio.h> void display(int a) { printf("%d",a); } int main() { int c[] = {2,3,4}; display(c[2]); //Passing array element c[2] only return 0; } Department of Computer Engineering 37/46

Passing Arrays to Functions #include <stdio.h> float average(float a[]); // float average(float a) int main(){ } float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c); / Only name of array is passed as argument / printf("average age=%.2f", avg); return 0; float average(float a[]){ // float average(float a) } int I; float avg, sum=0.0; for(i = 0; I < 6; ++i) avg = (sum / 6); return avg; sum += a[i]; Department of Computer Engineering 38/46 void func (int x); / this is a pointer / void func (int x[]); / this is a pointer / void func (int x[10]); / this is a pointer /

Passing Arrays to Functions #include <stdio.h> float average(float a[], int count); // float average(float a, int count) int main(){ } float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c, 6); / Only name of array is passed as argument / printf("average age=%.2f", avg); return 0; float average(float a[], int count){ // float average(float a) } int I; float avg, sum = 0.0; for(i = 0;I < count; ++i) sum += a[i]; avg = (sum / 6); return avg; Department of Computer Engineering 39/46 void func (int x); / this is a pointer / void func (int x[]); / this is a pointer / void func (int x[10]); / this is a pointer /

Passing Arrays to Functions #include <stdio.h> void f1(float a) { a[1] = 100;} void f2(float a[]){ a[2] = 200;} void printarrat(float a[]) { } int i = 0; for(; i < 6; i++) printf("%g ", a[i]); int main(){ } float c[]={23.4, 55, 22.6, 3, 40.5, 18}; f1(c); printarrat(c); puts(""); f2(c); printarrat(c); getch(); return 0; Passing Array By Reference 23.4 55 22.6 3 40.5 18 23.4 100 22.6 3 40.5 18 23.4 55 200 3 40.5 18 Department of Computer Engineering 40/46

Passing 2D, 3D, Array to Functions Only the first dimension may be omitted int m[5][7]; func(m); void func(int a[5][7]) {... } void func(int a[][7]) {... } Department of Computer Engineering 41/46

Allocating Memory for a Pointer // The following program is wrong! #include <stdio.h> int main() { int p; scanf("%d", p); return 0; } Don t // This one is correct: #include <stdio.h> int main() { int p; int a; p = a; scanf("%d", p); return 0; } Department of Computer Engineering 42/46

malloc Prototype: void malloc(size_t size); function returns the address of the first byte programmers responsibility to not lose the pointer Example: Memory int ptr; ptr = (int )malloc(sizeof(int)); // new allocation 10 ptr #include <stdlib.h> Key previously allocated new allocation 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Department of Computer Engineering 43/46

free Prototype: void free(void ptr) releases the area pointed to by ptr ptr must not be null 2 p2 trying to free the same area twice will generate an error 5 p1 initial memory #include <stdlib.h> 2 0 1 2 3 4 5 6 7 free(p1); NULL p2 p1 Key allocated memory after free free memory 0 1 2 3 4 5 6 7 Department of Computer Engineering 44/46

Allocating Memory for a Pointer There is another way to allocate memory so the pointer can point to something: #include <stdio.h> #include <stdlib.h> int main(){ int p; p = (int ) malloc( sizeof(int) ); / Allocate 4 bytes / scanf("%d", p); printf("%d", p); } //... free(p); / This returns the memory to the system/ / Important!!! / Department of Computer Engineering 45/46

Allocating Memory for a Pointer You can use malloc and free to dynamically allocate and release the memory int p; p = (int ) malloc(1000 sizeof(int) ); for(i=0; i<1000; i++) p[i] = i; p[999]=3; p[1000]=3; / Wrong! / free(p); p[0]=5; / Wrong! / Department of Computer Engineering 46/46