ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2

Slides:



Advertisements
Similar presentations
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Advertisements

Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
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.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
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.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
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: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 8 Arrays, Strings and Pointers
Data Transfers, Addressing, and Arithmetic
Pointers.
Motivation and Overview
Pointers.
Lecture-5 Arrays.
Student Book An Introduction
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Arrays in C.
Programming Languages and Paradigms
Pointers and Arrays Chapter 12
Pointers and References
Chapter 10: Pointers 1.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Pointers.
C Operators, Operands, Expressions & Statements
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
Introduction to Problem Solving and Programming
ECE 103 Engineering Programming Chapter 32 Array Parameters
Chapter 16 Pointers and Arrays
Initializing variables
Objectives You should be able to describe: Addresses and Pointers
Homework Starting K&R Chapter 5 Good tutorial on pointers
ECE 103 Engineering Programming Chapter 56 Runtime Errors
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Data Structures and Algorithms Introduction to Pointers
Herbert G. Mayer, PSU CS Status 7/19/2015
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 9: Pointers and String
C++ Pointers and Strings
Pointers.
Introduction to Pointers
Presentation transcript:

ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2 Herbert G. Mayer, PSU CS Status 8/4/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Array Variables Pointer Variables and 1-D Arrays Pointer Arithmetic Pointers and Strings Examples

Array Variables Declaration: datatype var_name[SIZE]; Example: int x[3]; x is the address of the first element in the array. x is an array. It is not a pointer. The address value cannot be changed. Example: x = 3278; /* Error! */ x[k], where k is a non-negative integer index, is the value stored at index k in the array. 2

x[0] x[1] x[2] x[0] x[1] x[2] x[0] x[1] x[2] int x[3]; x[0] = 3; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x 3 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x 3 3

Pointer Variables and 1-D Arrays Declaration: datatype * pointer_name; Example: int *p; p contains the address of a location in memory. The address stored in p can be changed. & p is the memory address where the pointer variable itself is stored. 4

If a pointer p contains the address of an array, then either pointer notation or array notation can be used to access the array. Pointer notation Uses the * symbol, pointer name, integer offset: *(p+k) where k is an integer (neg/zero/pos) Array notation Uses pointer name, brackets, integer index: p[k] where k is an integer (neg/zero/pos) 5

Example: int *p; /* Pointer to data array */ int x[3]; /* Data array */ p = x; /* p now points to the first element of array x */ p = &x[0]; /* This does the same thing */ x[0] = 3; /* Store a 3 in the first element of array x */ *(p+0) = 3; /* This does the same thing */ p[0] = 3; /* This does the same thing */ x[1] = 5; /* Store a 5 in the second element of array x */ *(p+1) = 5; /* This does the same thing */ p[1] = 5; /* This does the same thing */ p = &x[2]; /* p now points to the third element of array x */ x[0] = 7; /* Store a 7 in the first element of array x */ *(p+0) = 9; /* Store a 9 in the third element of array x */ 6

x[0] x[1] x[2] p x[0] x[1] x[2] p int *p; int x[3]; p = x; p = &x[0]; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 7

x[0] x[1] x[2] p x[0] x[1] x[2] p *(p+0) = 3; /* Same as *p = 3; */ p[0] = 3; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 3 A x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 3 8

x[0] x[1] x[2] p x[0] x[1] x[2] p *(p+1) = 5; /* p+1 is a 4 byte offset */ p[1] = 5; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 5 A A+4 3 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 3 5 9

p = &x[2]; x[0] x[1] p x[2] Array x 3 5 Address A Address A+4 Address Ap A+8 3 5 10

x[0] x[1] x[2] p x[0] x[1] x[2] p Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 9 A+8 7 3 5 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A+8 7 5 9 11

Pointer Arithmetic The address value stored in a pointer can be incremented or decremented. Final address depends on the declared datatype. Integer offsets can be added to or subtracted from a pointer variable. Example: p = p + 1; /* Increment address by one offset */ p = p + 3; /* Increment address by three offsets */ p = p - 1; /* Decrement address by one offset */ p = p - 3; /* Decrement address by three offsets */ 12

The increment/decrement operators advance a pointer by one offset (++) or back up the pointer by one offset (--). Example: ++p; /* Increment address by one offset (pre) */ p++; /* Increment address by one offset (post)*/ --p; /* Decrement address by one offset (pre) */ p--; /* Decrement address by one offset (post)*/ Two pointers can be compared. What is actually being compared are the memory addresses. It is not legal to add or multiply two pointers together, or to divide one pointer by another. 13

Combining Dereferencing and ++/-- * and ++/-- are often combined in a single expression. The rules of precedence and associativity provide guidance. Precedence Description Operator Associativity 1 (Highest) Increment (post) expr++ left-to-right Decrement (post) expr-- 2 Indirection * right-to-left Reference (addr) & Increment (pre) ++expr Decrement (pre) --expr 14

*p++ or *(p++) and *p-- or *(p--) Current address A in p is retrieved. Address in p is incremented or decremented. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=*p++; 2004 15

Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; (*p)++ and (*p)-- Current address A in p is retrieved. Value stored at address A is returned. Value at address A is incremented or decremented. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=(*p)++; 101 16

Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; ++*p and --*p Current address A in p is retrieved. Value at address A is incremented or decremented. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=++*p; 101 17

Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; *++p and *--p Address in p is incremented or decremented. Current address A in p is retrieved. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=*++p; 2004 18

Pointers and Strings An array of type char is used to a hold string: char s[10]; A pointer of type char is used to point to a string: char *p; Declaring a pointer to a string does not cause memory to be reserved for holding the string. The correct way to allocate memory for the string is to declare an array of characters. 19

A pointer can serve as an alias for the actual string. Example: char s1[10] = "ECE103", s2[10]; /* Arrays to hold the strings */ char *src, *dst; /* Pointers to source and destination strings */ strcpy(&s2[0],&s1[0]); /* Works fine */ strcpy(s2, s1); /* Works fine */ strcpy(dst, src); /* Valid syntax, but fails since uninitialized */ /* pointers to char are not real strings */ src = s1; dst = s2; /* Initialize pointers to the actual arrays */ strcpy(dst, src); /* Now this will work */ strcpy(dst, s1); /* OK */ strcpy(s2, src); /* OK */ 20

Example: 21 #include <stdio.h> #include <string.h> int main (void) { char s[] = "Hello\n"; /* Store a string in the character array */ char *p = s; /* p is a work pointer */ int k, len = strlen(s); for (k = 0; k < len; k++) /* Display the string using array notation */ printf("%c", p[k]); for (k = 0; k < len; k++) /* Display the string using pointer offsets */ printf("%c", *(p+k)); while (*p != '\0') /* Display the string by incrementing the pointer itself */ printf("%c", *p); p++; } p = s; /* Need to make p point to the start of s again */ while (*p) /* More compact way to display the string */ printf("%c", *p++); return 0; 21

Example: #include <stdio.h> #include <string.h> int main (void) { char s[] = "ECE103"; /* Store a string in the character array */ char *p; /* p is a work pointer */ p = s + strlen(s) - 1; /* p points to the end of the string */ /* Display the string in reverse */ while (p >= s) printf("%c", *p); p--; } printf("\n"); return 0; 22