Pointers.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
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:
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Dynamic Allocation in C
CS1010 Programming Methodology
CS1010 Programming Methodology
Computer Organization and Design Pointers, Arrays and Strings in C
Lesson #8 Structures Linked Lists Command Line Arguments.
Computer Science 210 Computer Organization
“Studying C programming excluding pointers is meaningless.” d0m3z
UNIT 5 C Pointers.
C Interview Questions Prepared By:.
Strings (Continued) Chapter 13
CS1010 Programming Methodology
Lecture 7: Repeating a Known Number of Times
Testing and Debugging.
Quiz 11/15/16 – C functions, arrays and strings
A First Book of ANSI C Fourth Edition
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Arrays in C.
Computer Science 210 Computer Organization
Object Oriented Programming COP3330 / CGS5409
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
C Arrays.
Programming in C Pointer Basics.
Format String.
Pointers.
IFS410 Advanced Analysis and Design
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Outline Defining and using Pointers Operations on pointers
Pointers (continued) Chapter 11
Programming in C Pointer Basics.
Conversion Check your class notes and given examples at class.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Overloading functions
Pointers Pointers point to memory locations
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
ETE 132 Lecture 6 By Munirul Haque.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Pointers

Pointers Pointers permit a program to work with addresses of variables rather than just their values. Powerful feature of the C language Also treacherous. Easy to make mistakes Mistakes can be very difficult to track down. You have to be very careful with pointers!

Why would we want to do this? Necessary in system programming Operating systems, device drivers, etc. Permit a function to modify caller’s non-array data Like we are able to do with arrays Permit a function to return more than one value to the caller. Can be of various types. Can have meaningful names.

Why would we want to do this? Required for complex data structures Permit a function to access complex data structures in caller’s address space.

Pointer variables are specific to a type int *pCount; // better: int* pCount, but … The * means that pCount is a pointer to an int. i.e., it can contain the address of an int *pCount represents the value that pCount points to. The * in *pCount is the dereferencing operator. *pCount sometimes called the target of the pointer We use *pCount like a normal int variable. But we have to be sure that pCount is set to point to an actual integer variable. This does not happen automatically!

Setting Pointer Variables Pointer variables have to be set to the address of a something before being used. To get the address of a variable, put & in front of the name. As for scanf Example int value; int *pValue; .... pValue = &value;

Using a Pointer int main () { int value = 100; int *pValue; printf ("Before increment, value = %d\n", value ); pValue = &value; *pValue += 1; printf ("After increment, value = %d\n", value ); return 0; } Set pointer pValue to address of value Increment the int variable that pValue points to

Dereferencing a Pointer Effectively replaces the pointer by the variable to which it points. If pValue is a pointer to an int, *pValue is an int Equivalent to using the name of the variable whose address is in pValue. * has very high precedence like all unary operators.

Using a Pointer

Using a Pointer We could have used *pValue in the printf #include <stdio.h> int main () { int value = 100; int * pValue; pValue = &value; // Set pointer before using it printf ("Before increment, value = %d\n", *pValue); *pValue += 1; printf ("After increment, value = %d\n", *pValue); return 0; } Same effect as value.

Using a Pointer Same result.

Pointers to other types work the same Using a Pointer Pointers to other types work the same double *pRadius; char *pSomeChar;

*pValue works like a normal int variable Using a Pointer Summary If pValue is a pointer to int *pValue works like a normal int variable Use on either side of an assignment *pValue = some_other_int; some_other_int = *pValue; Use in function calls printf (“Value is %d\n”, *pValue);

Pointers as Function Arguments

Passing Pointers to Functions Functions can be defined with addresses as parameters. void Get_Sides (int *width, int *length) Caller passes addresses of variables that the function can then access. Address &value Pointer pValue

Function with Address Parameters void Get_Sides (int *width, int *length) { do *width = -1; printf ("Please enter Width as positive integer: "); scanf ("%d", width); /* Clear input buffer. Only do this if user */ /* is expected to input only one value per line*/ while (getchar() != '\n'); if (*width <= 0) printf ("Received invalid value %d\n", *width); } } while (*width <= 0); /* Same for length */ /* ... */ No & (width is an address) Check value entered by user No “return” statement. Values are put directly into variables whose addresses are passed as arguments.

Passing Addresses as Arguments int main (void) { int width = 0; int length = 0; int area = 0; printf ("This program computes the area of a rectangle\n"); Get_Sides (&width, &length); area = width * length; printf ("Area is %d\n", area); return 0; } Must pass in the location of the arguments,i.e., their addresses

Program Running

Program Running

Passing Addresses as Arguments We could also have passed pointers as arguments to Get_Sides.

Passing Pointers as Arguments int main (void) { int width = 0; int length = 0; int *pWidth = &width; int *pLength = &length; int area; printf ("This program computes the area of a rectangle\n"); Get_Sides (pWidth, pLength); area = width * length; printf ("Area is %d\n", area); return 0; } No & this time! Effect is identical to previous example.

A Good Test Question What’s wrong with this program? int main (void) { int *pWidth; int *pLength; int area; printf ("This program computes the area of a rectangle\n"); Get_Sides (pWidth, pLength); area = *pWidth * *pLength; printf ("Area is %d\n", area); return 0; } What’s wrong with this program?

Try It! Compiles without error Runtime error (Good!)