14th September IIT Kanpur

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
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.
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.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
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.
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
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.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers verses Variables
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Hassan Khosravi / Geoffrey Tien
ENEE150 Discussion 07 Section 0101 Adam Wang.
Day 03 Introduction to C.
Pointers.
Hassan Khosravi / Geoffrey Tien
Arrays and Pointers CSE 2031 Fall September 2018.
POINTERS.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Dynamic Memory Allocation
5. Arrays, Pointers and Strings
Pointers.
Object Oriented Programming COP3330 / CGS5409
Pointers  Week 10.
Dynamic Memory Allocation
CS111 Computer Programming
Pointers.
Programming with Pointers
Pointers.
Multidimensional Arrays
prepared by Senem Kumova Metin modified by İlker Korkmaz
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
C (and C++) Pointers April 4, 2019.
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
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 10-1: Dynamic Memory Allocation
Arrays.
Chapter 9: Pointers and String
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Arrays and Pointers CSE 2031 Fall May 2019.
EECE.2160 ECE Application Programming
Dynamic Memory – A Review
Arrays and Pointers CSE 2031 Fall July 2019.
Pointers.
Pointer Syntax Review *ptrName Declaring Pointers
Presentation transcript:

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

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

Pointers and arrays contd.. Name of the array is synonymous with the address of the first element of the array. 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 C Course, Programming club, Fall 2008

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. printf(“hello world\n”); C Course, Programming club, Fall 2008

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. int main() { int sample[10]; func1(sample); … } void func1(int *x) { void func1(int x[10]) { void func1(int x[]) { C Course, Programming club, Fall 2008

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

More about 2-Dimensional arrays 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] 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] 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 2008

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

C Course, Programming club, Fall 2008 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, int *x[10]; x[2] = &var; int i =*x[2]; C Course, Programming club, Fall 2008

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

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

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

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

C Course, Programming club, Fall 2008 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} C Course, Programming club, Fall 2008