Senem KUMOVA METİN CS115 2008-2009 FALL 1 POINTERS && ARRAYS CHAPTER 6.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Chapter 40 - Physiology and Pathophysiology of Diuretic Action Copyright © 2013 Elsevier Inc. All rights reserved.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 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.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ZMQS ZMQS
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Chapter 7: Arrays In this chapter, you will learn about
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Chapter 17 Linked Lists.
LIST PROCESSING.
ABC Technology Project
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
© S Haughton more than 3?
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
Twenty Questions Subject: Twenty Questions
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Event 4: Mental Math 7th/8th grade Math Meet ‘11.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
Senem Kumova Metin // FALL CS115 Introduction to Programming Inst. Senem Kumova Metin Textbook : A Book on C, A. Kelly.
Pointers and Arrays Chapter 12
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
1 Unit 1 Kinematics Chapter 1 Day
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Using C++ 2E
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
Basic notes on pointers in C
prepared by Senem Kumova Metin modified by İlker Korkmaz
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Presentation transcript:

Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6

Senem KUMOVA METİN CS FALL 2 POINTERS POINTER is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address

Senem KUMOVA METİN CS FALL 3 POINTERS  int x= 5 is a variable and 5 is the value of it printf(“value of x : %d”, x); &x is the location or address of the x printf(“address of x in memory : %d”, &x);  Pointers are declared in programs and then used to take addresses !!! int x=5; // some stored value int * p; // a pointer that can hold the address of an integer p=&x; x= 5 &x …. ….. MEMORY

Senem KUMOVA METİN CS FALL 4 Pointers: & operator 5 0x x int x = 5; p int *p; p = &x; // adress of x is // stored in p 0x p is a pointer: it contains the address of a piece of data Memory Address &x

Senem KUMOVA METİN CS FALL 5 POINTERS int * p; // A pointer that can hold an address // of an integer  integer pointer int a =0; p= &a; double * q // A pointer that can hold an address // of a double  double pointer double b=1.2; q= &b; char * t // A char pointer char c=‘D’; t = &c;

Senem KUMOVA METİN CS FALL 6 POINTERS main() { int * p; //  integer pointer int a =0; p= &a;//  a equals to *p printf(“%d \n”, a); printf(“%d \n”, *p); *p=6; printf(“%d \n”, a); printf(“%d \n”, *p); a++; printf(“%d \n”, a); printf(“%d \n”, *p);} a=0 p=&a *p

Senem KUMOVA METİN CS FALL 7 POINTERS int a=1, b=2, *p; p=&a; &a&b &p a=1 b=2 ?? will point somewhere in memory &p 1 2 p=&a &a&b

Senem KUMOVA METİN CS FALL 8 POINTERS b=*p; // equivalent to b=a &a &b&p a=1 b=1 ( the value of a ) p=&a THE VALUE POINTED BY A POINTER CAN BE CHANGED BY * name_pointer *p

Senem KUMOVA METİN CS FALL 9 Pointers : EXAMPLE 1 #include void main(void) { int i = 7, j=3, *k, *m; k = &i; // k ??? *k ??? j=*k; // k ??? *k ??? m=&i; // i ??? *i ??? (*m) ++; // m ??? *m ??? printf(“ %d %d %d %d”, i, j, *k, *m); }

Senem KUMOVA METİN CS FALL 10 Pointers : EXAMPLE 2 #include void main(void) { char a = ‘D’; char *p = &a; *p=*p+2; printf(“%c %c”, a, *p); a=a+1; printf(“%c %c”, a, *p);}

Senem KUMOVA METİN CS FALL 11 CALL BY VALUE /* Whenever variables are passed as arguments to a function, their values are copied to the function parameters */ void main() {int a=20; incr1 (a); printf(“%d: “, a); incr2 (a); printf(“%d: “, a);} void incr1(int x) { x++; } void incr2(int a) { a++; } R E M E M B E R !

Senem KUMOVA METİN CS FALL 12 CALL BY REFERENCE /* Pointers are passed as arguments to a function, their addresses are assigned to the function parameters defined as pointers:*/ void main() {int a=20; incr (&a); printf(“%d: “, a);} void incr(int * x) { (*x)++; }

Senem KUMOVA METİN CS FALL 13 CALL BY REFERENCE : Example 1 void addi(int *a, int b); void main() {int a=20, int b=30; addi (&a, b) printf(“%d %d :“, a, b);} void addi(int *a, int b) {int tmp = *a +b; *a= tmp; b= b++;} OUTPUT??

Senem KUMOVA METİN CS FALL 14 void main() {int a=20; int b=30; swap1 (a, b); swap2 (&a, &b); printf(“%d %d: “, a, b);} void swap1(int x, int y) {int tmp; tmp=x; x=y; y=tmp; } void swap2(int *x, int *y) {int tmp; tmp = *x; // get value pointed by x. *x = *y; // assign value pointed by y to x *y = tmp; } CALL BY REFERENCE Example 2 : SWAP

Senem KUMOVA METİN CS FALL 15 What is an Array?  int grade0, grade1, grade2;  int grade [3];  Arrays make it possible to represent large number of homogenous values grade0grade1grade2 grade[0]grade[1]grade[2]

Senem KUMOVA METİN CS FALL 16 One Dimensional Arrays  type name_of_array[size] EXAMPLE int grade[5]; data grade[1] data grade[2] data grade[0] grade[3] grade[4] MEMORY first element in array fift ( the last) element in array starts from 0 ends at size - 1

Senem KUMOVA METİN CS FALL 17 One Dimensional Arrays int grade[5], x=9, y; grade[0]= 0; grade[3]= 4; grade[2]= -1; grade[1]=grade[3]+grade[2]; grade[4]= x; x= grade[2]; y= grade[grade[3]]; 0 3 grade[1] grade[2] 4 9 grade[0] grade[3] grade[4] MEMORY x ? y ?

Senem KUMOVA METİN CS FALL 18 INITIALIZATION  float f1[]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration without the size */  float f2[4]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration with size */  char z[] =“abc”; // char arrays  char z[] = {‘a’, ‘b’, ‘c’, ‘\0’}  int a[80]={0};

Senem KUMOVA METİN CS FALL 19 EXAMPLE1: /* array1.c*/ main() { int x[10], k; for(k=0; k<10; k++) { x[k]=0; printf(“x[%d] = %d\n”, k, x[k]); //what will be the output ??? }

Senem KUMOVA METİN CS FALL 20 EXAMPLE2: /* array2.c*/ main() { int x[10], k; for(k=0; k<10; k++) { x[k]=k; printf(“x[%d] = %d\n”, k, x[k]); //what will be the output ??? }

Senem KUMOVA METİN CS FALL 21 EXAMPLE3: /* array3.c*/ main() { int x[10], k, sum=0; for(k=0; k<10; k++) scanf(“%d”, &x[k]); for(k=0; k<10; k++) { printf(“%d”, x[k]); sum=sum + x[k]; } printf(“sum is %d”, sum); }

Senem KUMOVA METİN CS FALL 22 Relationship between Pointers and Arrays /*An array name by itself is an adress or a pointer value */ int a[5], *p; p=&a[0]; // Equals to p=a;

Senem KUMOVA METİN CS FALL 23 Relationship between Pointers and Arrays &a[0] equals to a then a[0] equals to *a &a[1] equals to a+1 then a[1] equals to *(a+1) &a[2] equals to a+2 then a[2] equals to *(a+2) ……………………………………………………………………………… &a[i] equals to a+i then a[i] equals to *(a+i)  EXAMPLE: int a [5]={1,2,3,4,5}; int * p; printf(“%d”,a[0]); printf(“%d”,*a); printf(“%d”,a[2]); printf(“%d”,*(a+2)); p=&a[4]; P=a+4;

Senem KUMOVA METİN CS FALL 24 Relationship between Pointers and Arrays : Example1 main() { int x[10], k; for(k=0; k<10; k++) {x[k]=k; printf(“%d\n”, x[k]); } main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=k; printf(“%d\n”, *(x+k)); }

Senem KUMOVA METİN CS FALL 25 Relationship between Pointers and Arrays : Example2 main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=0; printf(“%d\n”, *x+k); } main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=0; printf(“%d\n”, *(x+k)); }

Senem KUMOVA METİN CS FALL 26 Pointer Arithmetic EXAMPLE double a[2],*p,*q; p=a; // p=&a[0] q=p+1; // q=&a[0]+1= &a[1] printf(“%d\n”, q-p); /* difference in terms of array elements */ printf(“%d\n”, (int) q-(int) p); /* difference in memory locations */

Senem KUMOVA METİN CS FALL 27 Arrays as Function Arguments main() { int x[9], r; r=sum(x,9); //sum(&x[0],9) } double sum( int a[], int n) { int i; double result =0.0; for (i=0; i<n; i++) result+=a[i]; return result; } main() { int x[9], r; r=sum(x,9); // sum(&x[0],9) } double sum( int * a, int n) { int i; double result =0.0; for (i=0; i<n; i++) result+=*(a+i); return result; }

Senem KUMOVA METİN CS FALL 28 Dynamic Memory Allocation  Calloc : contiguous memory allocation  Malloc : memory allocation

Senem KUMOVA METİN CS FALL 29 CALLOC  calloc(n, el_size)  an array of n elements, each element having el_size bytes int main() { int *a; //will be used as an array int n; // size of array.... a=calloc(n, sizeof(int)); /* get space for a, and initialize each bit to zero */ free(a); /* each space opened dynamically must be returned */ }

Senem KUMOVA METİN CS FALL 30 MALLOC /* malloc does not initialize memory */ int main() { int *a; //will be used as an array int n; // size of array.... a=malloc(n*sizeof(int)); /* get space for a */.... free(a); }

Senem KUMOVA METİN CS FALL 31 DYNAMIC ARRAY : Example main() { int * x, k,s; printf(“give array size”); scanf(“%d”,&s); x=malloc(s*sizeof(int)); for(k=0; k<s; k++)x[k]=k; for(k=0; k<s; k++)printf(“%d ”,x[k]); }

Senem KUMOVA METİN CS FALL 32 NEXT WEEK  MULTIDIMENSIONAL ARRAYS  SORTING ALGORITHMS : BUBBLE SORT MERGE SORT  STRINGS && STRING_HANDLING FUNCTIONS  ARRAYS OF POINTERS  ARGUMENTS TO MAIN