6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall 20081.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Programming and Data Structure
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.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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:
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Applications
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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;
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
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.
Stack and Heap Memory Stack resident variables include:
Hassan Khosravi / Geoffrey Tien
ENEE150 Discussion 07 Section 0101 Adam Wang.
Programming Languages and Paradigms
14th September IIT Kanpur
5. Arrays, Pointers and Strings
Pointers.
CS111 Computer Programming
Pointers.
prepared by Senem Kumova Metin modified by İlker Korkmaz
Outline Defining and using Pointers Operations on pointers
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Pointers.
Presentation transcript:

6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall 20081

Pointers and arrays Pointers and arrays are tightly coupled. char a[] = “Hello World”; char *p = &a[0]; C Course, Programming club, Fall 20082

Pointers and arrays contd.. Name of the array is synonymous with the address of the first element of the array. 3C Course, Programming club, Fall 2008 int *p; int sample[10]; p = sample; // same as p = &sample[0]; int *p; int sample[10]; p = sample; p[5] = 100;// Both these statements *(p+5) = 100;// do the same thing

Pointers and function arguments Functions only receive copies of the variables passed to them. {program: swap_attempt_1.c} A function needs to know the address of a variable if it is to affect the original variable {program: swap_attempt_2.c} Large items like strings or arrays cannot be passed to functions either. What is passed is the address of “hello world\n” in the memory. C Course, Programming club, Fall printf(“hello world\n”);

Passing single dimension arrays to functions In C, you cannot pass the entire data of the array as an argument to a function. – How to pass array then? Pass a pointer to the array. 5C Course, Programming club, Fall 2008 int main() { int sample[10]; func1(sample); … } void func1(int *x) { … } void func1(int x[10]) { … } void func1(int x[]) { … }

2-Dimensional Arrays (Array of arrays) Access the point 1, 2 of the array: d[1][2] Initialize (without loops): C Course, Programming club, Fall int d[3][2]; int d[3][2] = {{1, 2}, {4, 5}, {7, 8}};

More about 2-Dimensional arrays d[0][0]d[0][1]d[0][2]d[0][3] d[1][0]d[1][1]d[1][2]d[1][3] d[2][0]d[2][1]d[2][2]d[2][3] A Multidimensional array is stored in a row major format. A two dimensional case:  next memory element to d[0][3] is d[1][0] What about memory addresses sequence of a three dimensional array?  next memory element to t[0][0][0] is t[0][0][1] C Course, Programming club, Fall 20087

Multidimensional Arrays Syntax type array_name[size1][size2]…[sizeN]; e.g size of array = 3 x 6 x 4 x 8 x 4 bytes 8C Course, Programming club, Fall 2008 int a[3][6][4][8];

Arrays of Pointers Declares an array of int pointers. Array has 10 pointers. Assign address to a pointer in array To find the value of var, 9C Course, Programming club, Fall 2008 int *x[10]; x[2] = &var; int i =*x[2];

Pointer to Pointer Declaration – Place an additional asterisk newbalance is a pointer to a double pointer. 10C Course, Programming club, Fall 2008 double **newbalance;

Pointer to Pointer contd.. {program: pointers.c} 11C Course, Programming club, Fall 2008 #include int main() { int x, *p, **q; x = 10; p = &x; q = &p; printf(“%d %d %d\n”, x, *p, **q); return 0; }

Dynamic Memory Allocation To allocate memory at run time. malloc(), calloc() – both return a void* you’ll need to typecast each time. 12C Course, Programming club, Fall 2008 char *p; p = (char *)malloc(1000); /*get 1000 byte space */ int *i; i = (int *)malloc(1000*sizeof(int));

Dynamic Memory Allocation contd.. To free memory free() – free(ptr) frees the space allocated to the pointer ptr 13C Course, Programming club, Fall 2008 int *i; i = (int *)malloc(1000*sizeof(int));. free(i);

Pointers to functions A function pointer stores the address of the function. Function pointers allow: – call the function using a pointer – functions to be passed as arguments to other functions return_type (*function_name)(type arg1, type arg2…) {program: function_pointer.c} 14C Course, Programming club, Fall 2008