Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Similar documents
Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Fundamentals of Programming Session 24

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Programming for Engineers Structures, Unions

MYcsvtu Notes LECTURE 34. POINTERS

... Lecture 12. Pointers

Chapter 6 - Pointers

Procedural programming with C

Goals of this Lecture

Lecture 05 POINTERS 1

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

SYSC 2006 C Winter 2012

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Variation of Pointers

Programming for Engineers Pointers

Arrays and Pointers (part 1)

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C++ Programming Chapter 7 Pointers

C Pointers Pearson Education, Inc. All rights reserved.

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Slides adopted from T. Ferguson Spring 2016

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Fundamentals of Programming Session 20

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Introduction to Scientific Computing and Problem Solving

Arrays and Pointers (part 1)

PROGRAMMAZIONE I A.A. 2017/2018

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Arrays, Pointers and Memory Management

Character Strings. String-copy Example

OBJECT ORIENTED PROGRAMMING USING C++

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Lectures 5-6: Introduction to C

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Chapter 7 C Pointers

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

typedef int Array[10]; String name; Array ages;

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

BITG 1113: POINTER LECTURE 12

Chapter 10 C Structures, Unions, Bit Manipulations

Computers Programming Course 6. Iulian Năstac

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Lectures 5-6: Introduction to C

Arrays and Pointers. CSE 2031 Fall November 11, 2013

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

[0569] p 0318 garbage

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Fundamental of Programming (C)

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Fundamental of Programming (C)

Pointers. 10/5/07 Pointers 1

Pointers. Pointer References

Operators and Expressions:

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Unit IV & V Previous Papers 1 mark Answers

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Basics of Programming

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Lab 3. Pointers Programming Lab (Using C) XU Silei

Lecture 3. More About C

Chapter 8 STRUCTURES IN C

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Computer Programming Lecture 12 Pointers

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Fundamentals of Programming Session 20

Programming Language B

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Principles of C and Memory Management

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Language comparison. C has pointers. Java has references. C++ has pointers and references

At the end of this module, the student should be able to:

Transcription:

Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1

Pointer Variables Each variable in a C program occupies space in memory during its lifetime: The address of a variable is the address of its first byte in memory. A pointer variable is a variable capable of storing the address of ( pointing to ) another variable. 2

Pointer Variables When a pointer variable is declared, its name is preceded by an asterisk: int *p; The * indicates that p is not an integer variable, but rather a variable capable of pointing to an integer. Each pointer variable can point only to objects of a particular type: int *p; /* points only to integers */ float *q; /* points only to float values */ char *r; /* points only to characters */ Before it has been assigned a value, a pointer points to nothing in particular. 3

The Address Operator A pointer can be given a value by assigning it the address of a variable. To determine the address of a variable, use the address operator (represented by the & symbol): int i, *p; p = &i; After this assignment, p points to i: 4

Pointer Representation in Memory The following figure shows the representation of the pointer in memory, assuming that integer variable y is stored at location 600,000, and pointer variable yptr is stored at location 500,000. The operand of the address operator (&) must be a variable; the address operator (&) cannot be applied to constants or expressions. 5

The Indirection Operator To determine what a pointer currently points to, use the indirection operator (represented by the * symbol): int i, *p; p = &i; printf("%d\n", *p); printf will display the value of i. 6

The Indirection Operator The unary * operator, commonly referred to as the indirection operator or dereferencing operator, returns the value of the object to which its operand (i.e., a pointer) points. For example, the statement printf("%d", *yptr); prints the value of variable y, namely 5. Using * in this manner is called dereferencing a pointer. 7

The Indirection Operator If p points to i, then *p is just another name for i: int i, *p; p = &i; i = 1; printf("%d\n", i); /* prints 1 */ printf("%d\n", *p); /* prints 1 */ *p = 2; printf("%d\n", i); /* prints 2 */ printf("%d\n", *p); /* prints 2 */ 8

& and * Operators #include<stdio.h> int main() { int a = 7; int *aptr = &a; //set aptr to the address of a printf("the address of a is %p\n The value of aptr is %p\n\n", &a, aptr); printf("the value of a is %d\n The value of *aptr is %d\n\n", a, *aptr); printf("showing that * and & are complements of each other \n &*aptr = %p\n *&aptr = %p\n", &*aptr, *&aptr); } 9

Pointer Assignment One pointer may be assigned to another, provided that both have the same type: int i, *p, *q; p = &i; q = p; After the assignment, both p and q point to i: Any number of pointers may be pointing to the same variable. 10

Pointer Assignment Don t confuse pointer assignment with assignment using the indirection operator: int i, j, *p, *q; p = &i; q = &j; i = 1; *q = *p; 11

Passing Arguments to Functions by Value #include<stdio.h> int cubebyvalue(int n); int main() { int number = 5; // initialize number printf("the original value of number is %d\n", number); number = cubebyvalue(number); //pass number by value to cubebyvalue printf("the new value of number is %d\n", number); } // calculate and return cube of integer argument int cubebyvalue(int n) { return n * n * n; //cube local variable n and return result } 12

Passing Arguments to Functions by Reference #include<stdio.h> void cubebyvalue(int *nptr); int main() { int number = 5; // initialize number printf("the original value of number is %d\n", number); //pass address of number to cubebyvalue cubebyvalue(&number); printf("the new value of number is %d\n", number); } // calculate cube of *nptr; actually modifies number in main void cubebyvalue(int *nptr) { *nptr = (*nptr) * (*nptr) * (*nptr); //cube *nptr } 13

Pass-By-Value 14

Pass-By-Value 15

Pass-By-Value 16

Pass-By-Reference 17

Pass-By-Reference 18

Swap with Pass-By-Value How to write a function that swaps the values stored in two variables? swap(a, b); void swap(int x, int y) { int temp; At assembly time, this function call will be expanded to create three variables in the stack area. The stack pointer will be increased as the result. } temp = x; x = y; y = temp; The return statement is hidden. At assembly time, the return statement will be expanded to delete three variables in the stack area. The stack pointer will be decreased as the result. Is the above function correct? If not, why? 19

Swap with Pass-By-Value swap(a, b); void swap(int x, int y) { int temp; } temp = x; x = y; y = temp; At this moment Varible name value address a 1 A b 2 B x 1 C y 2 D temp? E 20

Swap with Pass-By-Value swap(a, b); void swap(int x, int y) { int temp; } temp = x; x = y; y = temp; At this moment Varible name value address a 1 A b 2 B x 2 C y 1 D temp 1 E 21

Swap with Pass-By-Value swap(a, b); void swap(int x, int y) { int temp; } temp = x; x = y; y = temp; At this moment Varible name value address a 1 A b 2 B 22

Pointers as Arguments For a function to be able to modify a variable, it must be given a pointer to the variable: void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } swap(&x, &y); scanf is an example of a function whose arguments must be pointers: scanf("%d%d", &i, &j); 23

Swap with Pass-By-Reference How to write a function that swaps the values stored in two variables? swap(&a, &b); void swap(int *px, int* py) { int temp; } temp =???;???;??? = temp; At this moment Varible name value address a 1 A b 2 B px A C py B D tmp? E 24

Swap with Pass-By-Reference swap(&a, &b); void swap(int *px, int* py) { int temp; } temp = *px; *px = *py; *py = temp; Varible name value address a 1 A b 2 B px A C py B D tmp 1 E 25

Swap with Pass-By-Reference swap(&a, &b); void swap(int *px, int* py) { int temp; } temp = *px; *px = *py; *py = temp; Varible name value address a 2 A b 2 B px A C py B D tmp 1 E 26

Swap with Pass-By-Reference swap(&a, &b); void swap(int *px, int* py) { int temp; } temp = *px; *px = *py; *py = temp; Varible name value address a 2 A b 1 B px A C py B D tmp 1 E 27

Swap with Pass-By-Reference swap(&a, &b); void swap(int *px, int* py) { int temp; } temp = *px; *px = *py; *py = temp; Varible name value address a 2 A b 1 B 28

Using Pointers for Array Processing Pointers to Array Elements A pointer variable may point to an element of an array: int a[10], *p; p = &a[0]; *p = 5; /* stores 5 into a[0] */ Use this ability for scanf: scanf("%d", &a[i]);

Pointer Arithmetic Three arithmetic operations are defined for pointers: Adding an integer to a pointer Subtracting an integer from a pointer Subtracting two pointers

Adding an Integer to a Pointer Adding an integer j to a pointer p yields a pointer to the element that is j places after the one that p points to. int a[10], *p, *q; p = &a[2]; q = p + 3;

Adding an Integer to a Pointer p += 6;

Subtracting an Integer from a Pointer If p points to the array element a[i], then p - j points to a[i-j]. p = &a[8]; q = p - 3; p -= 6;

Subtracting Pointers When two pointers are subtracted, the result is the distance (measured in array elements) between the pointers. p = &a[5]; q = &a[1]; i = p - q; /* i is 4 */ i = q - p; /* i is -4 */ Note: Performing arithmetic on a pointer p gives a meaningful result only when p points to an array element. Subtracting two pointers is meaningful only when both point to elements of the same array

Pointer Comparison Any two pointers of the same type may be compared using the equality operators (== and!=). Pointers may also be compared using the relational operators, provided that they point to elements of the same array. Example: int a[10], *p, *q; p = &a[5]; q = &a[3]; if (p < q) /* false */

Using a Pointer to Step Through an Array A pointer can be used to step through an array: #define N 10 int a[n], sum, *p; sum = 0; for (p = &a[0]; p < &a[n]; p++) sum += *p; At the end of the first iteration:

Using a Pointer to Step Through an Array At the end of the second iteration: At the end of the third iteration:

Using an Array Name as a Pointer Arrays and pointers are related in the following way: The name of an array can be used as a pointer to the first element in the array. The first element of an array a can be accessed by using a as a pointer: int a[10]; *a = 0; /* stores 0 in a[0] */ The second element of a can be accessed through a + 1: *(a+1) = 1; /* stores 1 in a[1] */

Using an Array Name as a Pointer In general, the expression a+i is another way to write &a[i]. Using this knowledge, we can simplify our arraysumming loop: #define N 100 int a[n], sum, *p; sum = 0; for (p = a; p < a + N; p++) sum += *p;

Array Arguments Pass array to the function int find_largest(const int a[], int n) { } Because of the relationship between arrays and pointers, an array parameter may be declared as a pointer instead: void store_zeroes(int *a, int n) { }

Using a Pointer as an Array Name When a pointer points to an array, it can be used in place of the array s name: #define N 100 int a[n], i, sum, *p; p = a; sum = 0; for (i = 0; i < N; i++) sum += p[i];

Declaring Structures A structure (aggregate) is a collection of one or more components (members), which may be of different types. A structure is a derived data type. Members of the same structure type must have unique names, but two different structure types may contain members of the same name without conflict. Each structure definition must end with a semicolon. Declaring structure variables can be done as follows: struct { char name[25]; int id, age; char sex; } s1, s2;

Declaring Structures Member names don t conflict with any other names in a program. Structure variables may be initialized: struct { char name[25]; int id, age; char sex; } s1 = { "Smith, John", 2813, 25, 'M'}, s2 = { "Smith, Mary", 4692, 23, 'F'};

Structure Tags A structure can be given a name (a structure tag) for later use: struct student { char name[25]; int id, age; char sex; }; struct student s1, s2; The two declarations can be combined if desired, with the same effect: struct student { char name[25]; int id, age; char sex; } s1, s2;

Declaring Structures A structure cannot contain an instance of itself. A pointer to struct employee, however, may be included. struct employee { char firstname[20]; char lastname[20]; unsigned int age; char gender; double hourlysalary; struct employee person; // ERROR struct employee *eptr; // pointer }; self-referential structure

Structure Tags Structure definitions do not reserve any space in memory; rather, each definition creates a new data type that s used to define variables. Structure variables are defined like variables of other types. struct card acard, deck[52], *cardptr; struct card { char *face; char *suit; } acard, deck[52], *cardptr;

Structure Tags The structure tag name is optional. If a structure definition does not contain a structure tag name, variables of the structure type may be declared only in the structure definition not in a separate declaration. struct { char name[25]; int id, age; char sex; } s1, s2;

Initializing Structures Structures can be initialized using initializer lists as with arrays. To initialize a structure, follow the variable name in the definition with an equals sign and a braceenclosed, comma-separated list of initializers. struct card { char *face; char *suit; }; struct card acard = {"Three", "Hearts"};

Accessing Structure Members struct student { char name[25]; int id, age; char sex; } s; strcpy(s.name, "Doe, John"); s.id = 18193; s.age = 18; s.sex = 'M'; struct student class[30]; strcpy(class[12].name, "Doe, John"); class[12].id = 18193; class[12].age = 18; class[12].sex = 'M';

Accessing Structure Members Two operators are used to access members of structures: the structure member operator (.) also called the dot operator and the structure pointer operator (->) also called the arrow operator. The structure member operator accesses a structure member via the structure variable name. For example, to print member suit of structure variable acard, use the statement printf("%s", acard.suit); // displays Hearts

Accessing Structure Members The expression cardptr->suit is equivalent to (*cardptr).suit, which dereferences the pointer and accesses the member suit using the structure member operator. The parentheses are needed here because the structure member operator (.) has a higher precedence than the pointer dereferencing operator (*). The structure pointer operator and structure member operator, along with parentheses (for calling functions) and brackets ([]) used for array subscripting, have the highest operator precedence and associate from left to right.

An Example //structure member operator and pointer operator #include <stdio.h>//card structure definition struct card { char *face; // define pointer face char *suit; // define pointer suit }; int main(){ struct card acard; //define one struct card variable // place strings into acard acard.face = "Ace"; acard.suit = "Spades"; printf("%s%s%s\n",acard.face, " of ", acard.suit); struct card *cardptr = &acard; // assign address of acard to cardptr printf("%s%s%s\n",cardptr->face, " of ", cardptr->suit); printf("%s%s%s\n",(*cardptr).face, " of ", (*cardptr).suit); }

typedef The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. Names for structure types are often defined with typedef to create shorter type names. C programmers often use typedef to define a structure type, so a structure tag is not required. typedef struct { char name[25]; int id, age; char sex; } Student; Student s1, s2; typedef struct card Card; or typedef struct { char *face; char *suit; } Card; Card deck[52];