Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 22, 2002.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

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.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
Structure.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointers Applications
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers CSE 2451 Rong Shi.
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.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
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.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
CPS120: Introduction to Computer Science Lecture 15A Structures.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Ahmet Sacan - Ceng C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS Ahmet Sacan.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Windows Programming Lecture 03. Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
EPSII 59:006 Spring 2004.
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Basic notes on pointers in C
Pointers Lecture 1 Thu, Jan 15, 2004.
C Programming Pointers
Introduction to Pointers
Presentation transcript:

Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 22, 2002

Pointers A pointer is variable which points to a place in computer's memory. pointer = address pointer variable: a variable dedicated to hold addresses.

& and * unary operators &, when applied to a variable, yields its address (pointer to the variable) *, when applied to an address (pointer), fetches the value at that address.

contents:.... x address:.... x103x104x105x106x variable name: apa ip*ipa&a x10733

Pointer declaration * ; int iVar;/* declares an int. */ int * iVarPtr;/* declares a variable that will POINT to an integer */ int * ptr2; float f; float * myFloatPointer;

Pointer Assignment & /* address of */ iVarPtr = &iVar; ptr2 = iVarPtr; myFloatPointer = &f; iVarPtr = iVar;/* BAD... */ myFloatPointer = iVarPtr; /* INVALID */

NULL a pointer value of 0 is known as NULL pointer. int * x; x = NULL;

Dereferencing pointers & /* value inside the memory pointed at */ iVar = * iVarPtr; f = * iVarPtr; f = * myFloatPtr;

int x=3, y=4, *p1=NULL; int *p2=0; p1 = &x; printf("%d %xd %xd %d", x, &x, p1, *p1); x=5; printf("%d %xd %xd %d", x, &x, p1, *p1); *p1 = 6; printf("%d %xd %xd %d", x, &x, p1, *p1); p2 = p1; printf("%xd %xd %d %xd %xd %d", &p1, p1, *p1, &p2, p2, *p2 );

Functions & Pointers - Call by reference - int square(int x) { return x*x; } void main() {... a = square(a); void square(int * x) { *x = (*x)*(*x); } void main() {... square(&a); int a; void square( ) { a = a*a; } void main() {... square();

int Add(int x, int y){ return x+y; } void main(){... c = Add(a, b);... void Add(int x, int y, int *z) { *z = x+y; } void main(){... Add(a, b, &c);...

void OrderSwap ( int *x, int *y ){ int t; if(*x > *y){ t = *x; *x = *y; *y = t; } } void main(){ int a, b, c; scanf("%d %d %d", &a, &b, &c); OrderSwap(&a,&b); OrderSwap(&b,&c); OrderSwap(&a,&b); printf("%d %d %d", a, b, c); }

float *maxPtr(float *xp, float *yp){ return *xp > *yp ? xp : yp; } void main(){ float a, b, *c; c = maxPtr(&a, &b);...

Goal First read 10 numbers. Then read numbers 1<= x <= 10 and tell what the xth number was.

int a, b, c, d, e, f, g, h, i, j; int x; scanf("%d %d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j); scanf("%d", &x); if(x==1) printf("%d", a); else if(x==2) printf("%d", b);....

Arrays int arr [10]; int i, x; for(i=0; i<10; i++) scanf("%d", &arr[i]); scanf("%d", &x); printf("%d", arr[x]);

Arrays array : a finite, ordered collection of the same-typed data items. element: individual data items in an array subscript (index): Only one name is assigned to an entire array. Individual elements are referenced by specifying an index. Subscripts start at ZERO.

Array Declaration [expr-1]...[expr-n] –expr-i must be a constant integral type. int midtermScores[50]; #define NUM_EXAMS 3 float exams[3]; int x = 10; float prices[x]; /* INVALID! array-expr must be constant */