1 More on pointers. Example 1 – pass by value/reference int main(){ int p, q, r; p = 5; q = 6; r = Sum_1(p, q + 1); return 0; } void Sum_1(int a, 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

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
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.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
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:
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’;
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Built into qsort is a function that can swap two given array elements.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Computer Science 210 Computer Organization Pointers.
Dynamic Memory Allocation. One Dimensional Dynamic Memory #define SIZE1 25 #define SIZE2 36 int *p; long double *q; p = (int *)malloc(SIZE1 * sizeof(int));
…WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.
Outline Midterm results Static variables Memory model
Functions, Pointers, Structures Keerthi Nelaturu.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functions & Pointers in C Jordan Erenrich
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
/* example program to demonstrate the passing of an array */ #include int maximum( int [] ); /* ANSI function prototype */ int maximum(
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Introduction to Programming
Day 04 Introduction to C.
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Dynamic Memory Allocation
Dynamic memory allocation and Intraprogram Communication
Pointers and dynamic memory
Dynamic Memory Allocation
prepared by Senem Kumova Metin modified by İlker Korkmaz
Introduction to Computer Organization & Systems
7. Pointers, Dynamic Memory
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Program Compilation and Execution
Presentation transcript:

1 More on pointers

Example 1 – pass by value/reference int main(){ int p, q, r; p = 5; q = 6; r = Sum_1(p, q + 1); return 0; } void Sum_1(int a, int b){ int c; c = a + b; return c; } pqr abc

Example 2 – pass by value/reference int main(){ int *x,*y; int p = 5; int q = 6; x = &p; y = &q; Sum_1(p, y); return 0; } void Sum_1(int a, int *b){ int input; printf(“Enter an integer:”); scanf("%d", &input); *b = a + input; } xypq abinput

Example: Array and pointer int a[5]; int *p; int i = 0; for(i=0; i < 5; i++) a[i] = 3 + i; p= a; p = (int *)malloc(3 * sizeof(int)); a[0]a[1]a[2]a[3]a[4]p p[0]p[1]p[2] Heap (space for dynamically allocated data) Statically allocated data

Example 3: pointer to a struct int main(){ Student john; Student * john_ptr = &john; john.age = 20; john.gpa = 3.5; print_student_age_1(john); printf("gpa is %.1f\n", john.gpa); print_student_age_2(john_ptr); printf("gpa is %.1f\n", john.gpa); return 0; } void print_student_age_1(Student any){ printf("Age is %d\n", any.age); any.gpa = 2.0; } void print_student_age_2(Student * any){ printf("Age is %d\n", any->age); any->gpa = 2.0; } void print_student_age_3(const Student * any){ printf("Age is %d\n", any->age); any->gpa = 2.0; } john.agejohn.gpajohn_ptr any.ageany.gpa any

Example 4: malloc and structs Student * many = (Student *)malloc(3 * sizeof(Student)); many[0].age = 20; many[0].gpa = 3.5; //void print_student_info( Student class [ ], int count ); OR //void print_student_info( Student * class, int count ); print_student_info(___________________, 3 ); many[0].agemany[0].gpamany[1].agemany[1].gpamany[2].agemany[2].gpa many