Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
1 Pointers. Variable Memory Snapshot 2 int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient.
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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
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 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Chapter 9: Pointers.
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
An Introduction to Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
C Programming Lecture-8 Pointers and Memory Management
Data Structures and Algorithms Introduction to Pointers
Standard Version of Starting Out with C++, 4th Edition
Pointers.
Presentation transcript:

Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers

Addresses and Pointers

Address Operator A variable can be referenced using the address operator & example: scanf(“%f”, &x); This statement specifies that the value read is to be stored at the address of x

Pointer Assignment A pointer is a variable that holds the address of a memory location If a variable p holds the address of another variable q, then p is said to point to q If q is a variable at location 100 in memory, then p would have the value 100 (q’s address)

How to declare a pointer variable pointer variables are declared using an asterisk ( * ) The asterisk is called the indirection operator or the de-referencing operator). example: –int a, b, *ptr; ptr is a pointer to an integer when a pointer is defined, the type of variable to which it will point must be specified. (i.e. a pointer defined to point to an integer cannot also point to a floating point variable.)

Example int *iPtr; double* dPtr; the variable iPtr is declared to point to an int the variable dPtr is declared to point to a double neither variable has been initialized in the above example declaring a pointer creates a variable capable of holding an address

Example int a, *iPtr; char* s; double *dPtr; ? ? ? iPtr s dPtr - ? a

More about declaring pointers When using the form int* p, q; the * operator does not distribute. In the above example p is declared to be a pointer to int. q is declared to be an int.

Assigning values to a pointer the assignment operator (=) is defined for pointers the right operand can be any expression that evaluates to the same type as the left the operator & in front of an ordinary variable produces the address of that variable. The & operator is called to address of operator

Example example - int I=6, j; int *iPtr; iPtr = &I; j = *iPtr; 6 6 I j iPtr

Practice! Give a memory snapshot after each set of assignment statements int a=1, b=2, *ptr; ptr = &b; int a=1, b=2, *ptr=&b; a = *ptr;

NULL pointer A pointer can be assigned or compared to the integer zero, or, equivalently, to the symbolic constant NULL, which is defined in. A pointer variable whose value is NULL is not pointing to anything that can be accessed Is guaranteed to compare unequally with any pointer that is not a null pointer.

Example- int *iPtr=0; char *s=0; double *dPtr=NULL; iPtr s dPtr

Pointer Assignment A pointer can point to only one location at a time, but several pointers can point to the same location. Example /* Declare and initialize variables. */ int x=-5, y = 8, *ptr1, *ptr2; /* Assign both pointers to point to x. */ ptr1 = &x; ptr2 = ptr1; The memory snapshot after these statements are executed is -58 ptr1 ptr2 x y

Pointer Arithmetic Four arithmetic operations are supported –+, -, ++, -- –only integers may be used in these operations Arithmetic is performed relative to the variable type being pointed to Example:p++; –if p is defined as int *p, p will be incremented by 4 (system dependent) –if p is defined as double *p, p will be incremented by 8(system dependent –when applied to pointers, ++ means increment pointer to point to next value in memory

Comparing Pointers You may compare pointers using relational operators Common comparisons are: –check for null pointer (p == NULL) –check if two pointers are pointing to the same object (p == q) Is this equivalent to (*p == *q) –compare two pointers that are pointing to a common object such as an array.

Pointers and Arrays The name of an array is the address of the first elements (i.e. a pointer to the first element) The array name is a constant that always points to the first element of the array and its value can not be changed. Array names and pointers may often be used interchangeably. Example int num[4] = {1,2,3,4}, *p; p = num; /* above assignment is the same as p = &num[0]; */ printf(“%i”, *p); p++; printf(“%i”, *p);

More Pointers and Arrays You can also index a pointer using array notation Example: char string[] = “This is a string”; char *str; int i; str = string; for(i =0; str[i]; i++)//look for null printf(“%c”, str[i]);

Two-Dimensional Arrays A two-dimensional array is stored in sequential memory locations, in row order. Array definition:int s[2][3] = {{2,4,6}, {1,5,3}}, *sptr=&s[0][0]; Memory allocation: s[0][0]2 s[0][1]4 s[0][2]6 s[1][0]1 s[1][1]5 s[1][2]3 A pointer reference to s[0][1] would be *(sptr+1) A pointer reference to s[1][1] would be *(sptr+4) row offset * number of columns + column offset

Pointers in Function References In C, function references are call-by-value except when an array name is used as an argument. –An array name is the address of the first element –Values in an array can be modified by statements within a function To modify a function argument, a pointer to the argument must be passed The actual parameter that corresponds to a pointer argument must be an address or pointer.

switch Example void switch2(int *a, int *b) { /* Declare Variables. */ int temp; /* Switch values pointed to by a and b. */ temp = *a; *a=*b; *b=temp; /* Void return. */ return; }

Dynamic Memory Allocation Dynamically allocated memory is determined at runtime A program may create as many or as few variables as required, offering greater flexibility Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. Dynamic memory is finite Dynamically allocated memory may be freed during execution

Dynamic Memory Allocation Memory is allocated using the: –malloc function(memory allocation) –calloc function (cleared memory allocation) Memory is released using the: –free function The size of memory requested by malloc or calloc can be changed using the: –realloc function

malloc and calloc Both functions return a pointer to the newly allocated memory If memory can not be allocated, the value returned will be a NULL value The pointer returned by these functions is declared to be a void pointer A cast operator should be used with the returned pointer value to coerce it to the proper pointer type

Example of malloc and calloc int npts = 500; double *x; int *p; /* Allocate memory for 500 doubles. */ x = (double *)malloc(npts*sizeof(double)); /* Allocate memory for 500 integers. */ p = (int *)calloc(npts,sizeof(int)); Represents the number of bytes used to store a double value

#include int main() { int num_std,j; double ave=0.0,*grade; printf("How many students are there?\n"); scanf("%d",&num_std); //declare malloc grade=(double*)malloc(num_std*sizeof(double)); //declare calloc //grade=(double*)calloc(num_std,sizeof(double)); for(j=0;j<num_std;j++) printf("%lf\n",grade[j]); printf("Please enter the marks of %d students\n",num_std); for(int i=0;i<num_std;i++) { scanf("%lf",&grade[i]); ave+=grade[i]; } ave=ave/num_std; printf("The average is %.2lf \n",ave); free(grade); return 0; }