Pointers. C and C++ Example. Manipulating pointers

Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

5 Arrays and Pointers

Informatica e Sistemi in Tempo Reale

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Sources: On the Web: Slides will be available on:

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

The C Programming Language course syllabus associate level

Tutorial on C Language Programming

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Example 1/24. Example

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Semantic Analysis: Types and Type Checking

How To Write Portable Programs In C

Comp151. Definitions & Declarations

El Dorado Union High School District Educational Services

arrays C Programming Language - Arrays

Lecture 22: C Programming 4 Embedded Systems

Introduction to Data Structures

1 Abstract Data Types Information Hiding

Project 2: Bejeweled

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

C++ Programming Language

Object Oriented Software Design II

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

DATA STRUCTURES USING C

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Object Oriented Software Design II

Programming languages C

C++ INTERVIEW QUESTIONS

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Lecture 12 Doubly Linked Lists (with Recursion)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

TivaWare Utilities Library

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Symbol Tables. Introduction

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Database Toolkit: Portable and Cost Effective Software

Chapter 13 Storage classes

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Compiler I: Syntax Analysis Human Thought

Base Conversion written by Cathy Saxton

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Chapter 4: Computer Codes

Keil C51 Cross Compiler

Chapter 5 Names, Bindings, Type Checking, and Scopes

The programming language C. sws1 1

Implementing a Data Logger with Spansion SPI Flash

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Specific Simple Network Management Tools

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Fundamentals of Programming

An Incomplete C++ Primer. University of Wyoming MA 5310

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

DNA Data and Program Representation. Alexandre David

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Member Functions of the istream Class

Pemrograman Dasar. Basic Elements Of Java

Compiler Construction

Java programming for C/C++ developers

CSC230 Getting Starting in C. Tyler Bletsch

IPC. Semaphores were chosen for synchronisation (out of several options).

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

/* File: blkcopy.c. size_t n

CS 241 Data Organization Coding Standards

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Stack Allocation. Run-Time Data Structures. Static Structures

Habanero Extreme Scale Software Research Project

Stacks. Linear data structures

Java Interview Questions and Answers

Glossary of Object Oriented Terms

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Lecture 9. Semantic Analysis Scoping and Symbol Table

C Interview Questions

Cyclone: A Type-Safe Dialect of C

Module 816. File Management in C. M. Campbell 1993 Deakin University

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

BCS2B02: OOP Concepts and Data Structures Using C++

Embedded C Programming

An Introduction to the C Programming Language and Software Design. Tim Bailey

Database access for illiterate programmers. K.B.Swiatlowski

Passing 1D arrays to functions.

Code Refactoring and Defects

Transcription:

Pointers C and C++ 3. Pointers Structures Alastair R. Beresford University of Cambridge Lent Term 28 Computer memory is often abstracted as a sequence of bytes, grouped into words Each byte has a unique address or index into this sequence The size of a word (and byte!) determines the size of addressable memory in the machine A pointer in C is a variable which contains the memory address of another variable (this can, itself, be a pointer) Pointers are declared or defined using an asterisk(*); for example: char *pc; or int **ppi; The asterisk binds to the variable name, not the type definition; for example char *pc,c; A pointer does not necessarily take the same amount of storage space as the type it points to 1 / 25 2 / 25 Manipulating pointers int **ppi int *pi int i char *pc char c 52...... 1c 41 Little... 42 41 Big 38 4c 05 62 The value pointed to by a pointer can be retrieved or dereferenced by using the unary * operator; for example: int *p =... int x = *p; The memory address of a variable is returned with the unary ampersand (&) operator; for example int *p = &x; 0x2c 0x30 0x34 0x38 0x4c 0x50 0x60 Dereferenced pointer values can be used in normal expressions; for example: *pi += 5; or (*pi)++ 3 / 25 4 / 25

Pointers and arrays 1 #include <stdio.h> 2 3 int main(void) { 4 int x=1,y=2; 5 int *pi; 6 int **ppi; 7 8 pi = &x; ppi = π 9 printf("%p, %p, %d=%d=%d\n",ppi,pi,x,*pi,**ppi); 10 pi = &y; 11 printf("%p, %p, %d=%d=%d\n",ppi,pi,y,*pi,**ppi); 12 13 return 0; 14 } A C array uses consecutive memory addresses without padding to store data An array name (without an index) represents the memory address of the beginning of the array; for example: char c[10]; char *pc = c; Pointers can be used to index into any element of an array; for example: int i[10]; int *pi = &i[5]; 5 / 25 6 / 25 Pointer arithmetic Pointer arithmetic can be used to adjust where a pointer points; for example, if pc points to the first element of an array, after executing pc+=3; then pc points to the fourth element A pointer can even be dereferenced using array notation; for example pc[2] represents the value of the array element which is two elements beyond the array element currently pointed to by pc In summary, for an array c, *(c+i) c[i] and c+i &c[i] A pointer is a variable, but an array name is not; therefore pc=c and pc++ are valid, but c=pc and c++ are not 1 #include <stdio.h> 2 3 int main(void) { 4 char str[] = "A string."; 5 char *pc = str; 6 7 printf("%c %c %c\n",str[0],*pc,pc[3]); 8 pc += 2; 9 printf("%c %c %c\n",*pc, pc[2], pc[5]); 10 11 return 0; 12 } 7 / 25 8 / 25

Pointers as function arguments Recall that all arguments to a function are copied, i.e. passed-by-value; modification of the local value does not affect the original In the second lecture we defined functions which took an array as an argument; for example void reverse(char s[]) Why, then, does reverse affect the values of the array after the function returns (i.e. the array values haven t been copied)? because s is a pointer to the start of the array Pointers of any type can be passed as parameters and return types of functions Pointers allow a function to alter parameters passed to it Compare swp1(a,b) with swp2(&a,&b): 1 void swp1(int x,int y) 2 { 3 int temp = x; 4 x = y; 5 y = temp; 6 } 1 void swp2(int *px,int *py) 2 { 3 int temp = *px; 4 *px = *py; 5 *py = temp; 6 } 9 / 25 10 / 25 Arrays of pointers C allows the creation of arrays of pointers; for example int *a[5]; Arrays of pointers are particularly useful with strings An example is C support of command line arguments: int main(int argc, char *argv[]) {... } In this case argv is an array of character pointers, and argc tells the programmer the length of the array argv: argc: 3 argv[0] argv[1] argv[2] argv[3] NULL progname\0 firstarg\0 secondarg\0 11 / 25 12 / 25

Multi-dimensional arrays Pointers to functions Multi-dimensional arrays can be declared in C; for example: int i[5][10]; Values of the array can be accessed using square brackets; for example: i[3][2] When passing a two dimensional array to a function, the first dimension is not needed; for example, the following are equivalent: void f(int i[5][10]) {... } void f(int i[][10]) {... } void f(int (*i)[10]) {... } In arrays with higher dimensionality, all but the first dimension must be specified C allows the programmer to use pointers to functions This allows functions to be passed as arguments to functions For example, we may wish to parameterise a sort algorithm on different comparison operators (e.g. lexicographically or numerically) If the sort routine accepts a pointer to a function, the sort routine can call this function when deciding how to order values 13 / 25 14 / 25 1 void sort(int a[], const int len, 2 int (*compare)(int, int)) 3 { 4 int i,j,tmp; 5 for(i=0;i<len-1;i++) 6 for(j=0;j<len-1-i;j++) 7 if ((*compare)(a[j],a[j+1])) 8 tmp=a[j], a[j]=a[j+1], a[j+1]=tmp; 9 } 10 11 int inc(int a, int b) { 12 return a > b? 1 : 0; 13 } 1 #include <stdio.h> 2 #include "example8.h" 3 4 int main(void) { 5 int a[] = {1,4,3,2,5}; 6 unsigned int len = 5; 7 sort(a,len,inc); //or sort(a,len,&inc); 8 9 int *pa = a; //C99 10 printf("["); 11 while (len--) 12 printf("%d%s",*pa++,len?" ":""); 13 printf("]\n"); 14 15 return 0; 16 } 15 / 25 16 / 25

The void * pointer Structure declaration C has a typeless or generic pointer: void *p This can be a pointer to anything This can be useful when dealing with dynamic memory Enables polymorphic code; for example: 1 sort(void *p, const unsigned int len, 2 int (*comp)(void *,void *)); However this is also a big hole in the type system Therefore void * pointers should only be used where necessary A structure is a collection of one or more variables It provides a simple method of abstraction and grouping A structure may itself contain structures A structure can be assigned to, as well as passed to, and returned from functions We declare a structure using the keyword struct For example, to declare a structure circle we write struct circle {int x; int y; unsigned int r;}; Once declared, a structure creates a new type 17 / 25 18 / 25 Structure definition Member access To define an instance of the structure circle we write struct circle c; A structure can also be initialised with values: struct circle c = {12, 23, 5}; An automatic, or local, structure variable can be initialised by function call: struct circle c = circle_init(); A structure can declared, and several instances defined in one go: struct circle {int x; int y; unsigned int r;} a, b; A structure member can be accessed using. notation: structname.member; for example: pt.x Comparison (e.g. pt1 > pt2) is undefined Pointers to structures may be defined; for example: struct circle *pc When using a pointer to a struct, member access can be achieved with the. operator, but can look clumsy; for example: (*pc).x Alternatively, the -> operator can be used; for example: pc->x 19 / 25 20 / 25

Self-referential structures Unions A structure declaration can contain a member which is a pointer whose type is the structure declaration itself This means we can build recursive data structures; for example: 1 struct tree { 2 int val; 3 struct tree *left; 4 struct tree *right; 5 } 1 struct link { 2 int val; 3 struct link *next; 4 } A union variable is a single variable which can hold one of a number of different types A union variable is declared using a notation similar to structures; for example: union u { int i; float f; char c;}; The size of a union variable is the size of its largest member The type held can change during program execution The type retrieved must be the type most recently stored Member access to unions is the same as for structures (. and -> ) Unions can be nested inside structures, and vice versa 21 / 25 22 / 25 Bit fields (adapted from K&R) Bit fields allow low-level access to individual bits of a word Useful when memory is limited, or to interact with hardware A bit field is specified inside a struct by appending a declaration with a colon (:) and number of bits; for example: struct fields { int f1 : 2; int f2 : 3;}; Members are accessed in the same way as for structs and unions A bit field member does not have an address (no & operator) Lots of details about bit fields are implementation specific: word boundary overlap & alignment, assignment direction, etc. 1 struct { /* a compiler symbol table */ 2 char *name; 3 struct { 4 unsigned int is_keyword : 1; 5 unsigned int is_extern : 1; 6 unsigned int is_static : 1; 7... 8 } flags; 9 int utype; 10 union { 11 int ival; /* accessed as symtab[i].u.ival */ 12 float fval; 13 char *sval; 14 } u; 15 } symtab[nsym]; 23 / 25 24 / 25

Exercises 1. If p is a pointer, what does p[-2] mean? When is this legal? 2. Write a string search function with a declaration of char *strfind(const char *s, const char *f); which returns a pointer to first occurrence of s in f (and NULL otherwise) 3. If p is a pointer to a structure, write some C code which uses all the following code snippets: ++p->i, p++->i, *p->i, *p->i++, (*p->i)++ and *p++->i ; describe the action of each code snippet 4. Write a program calc which evaluates a reverse Polish expression given on the command line; for example $ calc 2 3 4 + * should print 14 (K&R Exercise 5-10) 25 / 25