Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Pointers in C Rohit Khokher
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Exam Example Questions
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
. Plab – Tirgul 5 pointers to pointers, libraries.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers to pointers & multi-dimensional arrays
Pointers verses Variables
Memory allocation & parameter passing
2016.
Programming Languages and Paradigms
Lecture 6 C++ Programming
14th September IIT Kanpur
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Pointers and Arrays Beyond Chapter 16
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

Plab 2003 Exercise 4. Pointers to pointers

Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int *ip1 = &i; (5)int *ip2 = &j; ipp: (6)int **ipp = &ip1;

Plab 2003 Exercise 4 3 Pointers to pointers (1)int i=3; (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2;

Plab 2003 Exercise 4 4 Pointers to pointers (1)int i=3; (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2; (8)*ipp = &k;

Plab 2003 Exercise 4 5 pointers to pointers: example //put pointer to an allocated string in pp (1)int allocString(int len, char ** pp) { (2)char *str = malloc(len + 1); (3)if (str==NULL) (4)return -1; (5)*pp = str; (6)return 0; (7)} // copy a string using “allocString” (8)void main() { (9)char *s = “example”; (10)char *copy = NULL; (11)allocString( strlen(s), &copy ); (12)strcpy( copy, string); (13)}

Plab 2003 Exercise 4 6 Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7];  5 rows, 7 columns  continuous memory (divided to 5 blocks)  access: arr[row][col] = 0; u When sending ‘arr’ as an argument to a function, only the 1 st index can be omitted:  void func( int x[5][7] ) //ok  void func( int x[][7] ) //ok  void func( int x[][] ) //error (always)  void func( int * x[] ) //error  void func( int ** x ) //error

Plab 2003 Exercise 4 7 Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5];// allocates memory for 5 // pointers. for (i=0; i<5; i++) { pa[i] = (int*) malloc( 7*sizeof(int) ); // pa[i] now points to a memory for 7 // ints }

Plab 2003 Exercise 4 8 Multi-dimensional arrays 3. Dynamically: (1)int ** array; (2)array = (int**) malloc( 5*sizeof(int*) ); (3)for (i=0; i<5; i++) { (4) arr[i] = malloc( 7*sizeof(int) ); (5)}

Plab 2003 Exercise 4 9 Multi-dimensional arrays Dynamically allocated multi-dimensional array: u Memory not continuous u Each pointer can be with different size u Access: arr[ i ][ j ] u Don’t forget to free all the memory for (i=0; i<nrows; i++ ) { free( array[i] ); array[i] = NULL; } free( array );

Plab 2003 Exercise 4 10 pointers to pointers to … We also have pointers to pointers to pointers, etc. double ** mat1 = getMatrix(); double ** mat2 = getMatrix(); // allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat1; matrices[1] = mat2;

Plab 2003 Exercise 4. Test your understanding

Plab 2003 Exercise 4 12 Pointers void func( int * ptr ) { ptr++; ptr += 1; } int main () { int arr[] = {0,0}; int * p = arr; func(p); p++; *p += 1; printf("%d %d\n",arr[1],arr[0]); }

Plab 2003 Exercise 4 13 Pointers Cont (1) int i; (2) int * const px = &i; (3) int j = 5; (4) px = &j; (5) px[1] = j; 1. Line 2 will generate compilation errors/warnings 2. Line 4 will generate compilation errors/warnings 3. Line 5 will generate compilation errors/warnings 4. The code will not generate compilation errors/warnings

Plab 2003 Exercise 4 14 Macro #define MAX(a,b) \ (((a) > (b)) ? (a) : (b) ) int i=2; int j=3; int x = MAX(i++,j); printf("%d %d\n",i,x);

Plab 2003 Exercise 4 15 Macro #define BAR(x) x ) #define FOO(x) ( x++ int a = 2; int b = 3; int c = FOO(a) + BAR(b); 1. This code will generate a compilation error. 2. This code will generate a run-time error. 3. The code will compile without problems. 4. This code will generate a pre-processing error.

Plab 2003 Exercise 4 16 Memory Management #include void f(int* pa[]) { for (int i=0; i<5; i++) pa[i] = (int*)malloc(7*sizeof(int)); } int main() { int* pa[5]; for( int i=0; i<5; i++ ) pa[i] = (int*)malloc(7*sizeof(int)); f(pa); for (int i=0; i<5; i++) free(pa[i]); }

Plab 2003 Exercise 4 17 Memory Management Cont int main() { (1) int *y = (int*)malloc( 3*sizeof(int) ); (2) int x[] = {1,2,3}; (3) int ** pp = NULL; (4) int *z = y; } In which of the following lines a space was allocated on the memory heap?

Plab 2003 Exercise 4 18 Strings struct Flight { char source[20]; char * destination; } ticket1, ticket2; ticket1.destination = (char*)malloc( 20*sizeof(char) ); strcpy(ticket1.source, "Florence"); strcpy(ticket1.destination,“London"); ticket2 = ticket1; *(ticket2.destination + 2) = 'k'; ticket2.source[1] = 'm'; printf("%s %s",ticket1.source,ticket2.destination);

Plab 2003 Exercise 4 19 Strings Cont char s[] = {'p','l','a','b'}; What will be returned by calling strcmp(s,"plab")? 1. a negative number 2. a positive number We cannot know for sure

Plab 2003 Exercise 4 20 Strings Cont (1) char s[] = "plab"; (2) (2) (*s)++; (3) (3) *(s+2) = '\0'; 1. line 2 will generate a compilation error 2. line 3 will generate a compilation error 3. after runnung this code the string s is "ql" 4. after runnung this code the string s is "pl" 5. after running this code the string s is "plab"

Plab 2003 Exercise 4 21 Union Suppose that sizeof(char) = 1, sizeof(int) = 4, sizeof(double)=8; union ID { char x; double y; }; sizeof( ID ) is ?