1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.

Slides:



Advertisements
Similar presentations
#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Cosc 2150 Arrays in assembly code. Variables and addresses Uncompiled ld [a], %r1 addcc %r1, 2, %r3 ARC has three addressing modes —immediate, direct,
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
1 C++ Syntax and Semantics The Development Process.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.
C Programming language
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Chapter 1 Introduction to C Programming
Chapter 9 Pointers Objectives
Programming Languages and Paradigms
Basic notes on pointers in C
Buy book Online -
Unit-2 Objects and Classes
Lecture 18 Arrays and Pointer Arithmetic
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to C++ Programming Language
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Chapter 9: Pointers and String
Pointer Arithmetic By Anand George.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Presentation transcript:

1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book

2 Basic Notes int *x; float *y; float j = 200.0; int i = 50; -means x is of type pointer using 4 bytes expected to point to integer value. -Means y is also type pointer using 4 bytes, expected to point to float value. &i - means address of i. -In this case it is A205 -x = &i; y = &j; xA205A193 A194 A195 A196 yA201A197 A198 A199 A200 j200.0A201 A202 A203 A204 i50A205 A206 *x - means go to that pointed by x. printf( “ %d ”, *x)  50 *y = 30.5; 30.5

3 Can Memorize for Functions: Variables When calling a function where variable’s value will change:  Function definition will have * before the variable name everywhere in the function.  Caller will have ‘&’ before the variable

4 Memory Values

5 Memorize for Functions: Arrays When calling a function where Array (Name[30]) is sent as parameter: 1. Function definition will have array appear as char Name[] which means char *Name 2. Caller will NOT have ‘&’ before Array name. Array Name alone means its address. TotalScore means &TotalScore[0] int *Score &TotalScore

int *Score * &TotalScore A

7 More Rules int *Score &TotalScore *Score + i which is *(Score + i) TotalScore[2] = 32 is viewed as *TotalScore + 2 = 32 which is *(&TotalScore +2) = 32 When C sees Score[i] or TotalScore[2] it converts the code to: *Score + i and *TotalScore + 2. Now, there is a rule about arrays that if the array name such as TotalScore is mentioned alone without an index, it means the address of TotalScore or &TotalScore. Thus, *TotalScore + 2 is same as *(&TotalScore +2). Score[i] is *Score + i or *(Score + i)

int *Score * &TotalScore A *Score + i which is *(Score + i) TotalScore[2] = 32 is viewed as *TotalScore + 2 = 32 which is *(&TotalScore +2) = 32 32

9 2-D Arrays In main: In GetInput: Name[3][2] is ‘T’ is *(&Name +3*40+2) is ‘T’. char *Name[40] – pointer to 40 characters at a time or 40 bytes Name[2] is *(Name + 2) is *(A *40) is *(A180), meaning “PAM” A100 why have &