prepared by Senem Kumova Metin modified by İlker Korkmaz

Slides:



Advertisements
Similar presentations
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Advertisements

Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Arrays and pointers Lone Leth Thomsen. March 2006Basis-C-5/LL2 What is an array An array is a consecutive series of variables that share one variable.
Kernighan/Ritchie: Kelley/Pohl:
Pointer A variable that represent the location (rather than value) of a data item, such as variable or an array element It is used to pass information.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Introduction to C Programming CE
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers CSE 2451 Rong Shi.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
Arrays and Pointers.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Pointers verses Variables
CSE 220 – C Programming Pointers.
Recursion.
Java Array Object Chuen-Liang Chen Department of Computer Science
EPSII 59:006 Spring 2004.
INC 161 , CPE 100 Computer Programming
Arrays and Pointers CSE 2031 Fall September 2018.
Recursion.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Pointers.
14th September IIT Kanpur
C Passing arrays to a Function
Pointers.
Programming and Data Structures
C Programming Language
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Dynamic Memory Allocation
CS111 Computer Programming
Pointers Problem Solving & Program Design in C Eighth Edition
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Programming with Pointers
INC 161 , CPE 100 Computer Programming
Recursion.
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
Initializing variables
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Arrays, Pointers, and Strings
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Pointers.
Presentation transcript:

prepared by Senem Kumova Metin modified by İlker Korkmaz ARRAYS, POINTERS prepared by Senem Kumova Metin modified by İlker Korkmaz

What is an Array? int grade0, grade1, grade2; int grade [3]; Arrays represent a group of homogenous typed values grade0 grade1 grade2 grade[0] grade[1] grade[2]

One Dimensional Arrays (1) type name_of_array[size]; int grade[5]; MEMORY starts from 0 grade[0] data first element in array grade[1] data grade[2] data grade[3] data ends at size - 1 grade[4] data fifth ( the last) element in array

One Dimensional Arrays (2) int grade[5], x=9, y=0; grade[0]=0; grade[3]=4; grade[2]=-1; grade[1]=grade[3]+grade[2]; grade[4]= x; x= grade[2]; //the value of x ? y= grade[grade[3]]; //the value of y ?? MEMORY grade[0] grade[1] 3 grade[2] -1 grade[3] 4 grade[4] 9 ? x ?? y

Array Initialization float f1[]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration without size */ float f2[4]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration with size */ char z[] =“abc”; // special char arrays: strings char z[] = {‘a’, ‘b’, ‘c’, ‘\0’} int a[80]={0}; // initializes all the elements to zero

Example: One Dimensional Arrays /* array1.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 ??? }

Two Dimensional Arrays (1) #define R 4 #define C 5 int x[R][C]; column 0 column 1 column 2 …. column C-1 row 0 x[0][0] x[0][1] x[0][2] x[0][C-1] row 1 x[1][0] x[1][1] x[1][2] x[1][C-1] row 2 x[2][0] x[2][1] x[2][2] x[2][C-1] row 3 x[3][0] x[3][1] x[3][2] x[3][C-1] row R-1 x[R-1][0] x[R-1][1] x[R-1][2] x[R-1][C-1]

Two Dimensional Arrays (2) x[0][0] data 1. row , 1. column x[0][1] 1.row , 2. column x[0][2] 1.row , 3. column x[0][3] 1.row , 4. column x[1][0] x[1][1] How can x be declared ? x[1][2]  int x[?][?]; x[1][3] x[2][0] 3. row , 1. column x[2][1] 3.row , 2. column x[2][2] 3.row , 3. column x[2][3] 3.row , 4. column

Two Dimensional Array Initialization int y[2][3] = {1,2,3,4,5,6}; int y[2][3] = {{1,2,3},{4,5,6}}; int y[][3] = {{1,2,3},{4,5,6}};

POINTERS (1) int v = 5; v is the identifier of an integer variable 5 is the value of v &v is the location or address of the v inside the memory & means “ the address of ” Pointers are used in programs to access memory. int * p; // p is the identifier of a “pointer to an integer” p=&v; // p is assigned with the address of v p=0; // OR p = NULL;

POINTERS (2) int a=1, b=2, *p; p=&a; p a b 1 2 ?? p a b 1 2 &a will point somewhere in memory p a b 1 2 &a

POINTERS (3) b=*p; // equivalent to b=a a b p 1 1 &a ( the value of a ) &a

Example: Pointers EXAMPLE: #include <stdio.h> int main(void) { int i = 7, j , *k; k = &i; printf("%s%d\n%s%p\n", " Value of i: ", *k, "Location of i: ", k); j=*k; return 0; }

CALL BY VALUE /* Whenever variables are passed as arguments to a function, their values are copied to the function parameters , and the variables themselves are not changed in the calling environment.*/ int main() { int a=20; int b=30; swap (a, b) printf(“%d %d: “, a, b); return 0; } void swap(int x, int y) { int tmp; tmp=x; x=y; y=tmp; return;

CALL BY REFERENCE /* Whenever addresses of variables are passed as arguments to a function, their values shall be changed in the calling environment.*/ void main() { int a=20; int b=30; swap (&a, &b) printf(“%d %d: “, a, b); } void swap(int *x, int *y) { int tmp; tmp = *x; // get value pointed by x. *x = *y; // assign value pointed by y to x *y = tmp; return; }

Relationship between “pointers” and “arrays” (1) /* The name of an array is the adress or the pointer to the first element of the array. */ int a[5] , *p; p=a; // OR p=&a[0];

Relationship between “pointers” and “arrays” (2) &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;

Storage mapping the mapping b/w pointer values and array indices EXAMPLE: int d [3]; d[i]  *(&d[0]+i) int a [3] [4]; a[i][j]  *(&a[0][0]+4*i+j)

Arrays as Function Arguments double sum(int [] , int); main() { int x[9]; double 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=result+a[i]; return result; double sum(int* , int); main() { int x[9]; double 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=result + *(a+i); return result;

Dynamic Memory Allocation calloc : Contiguous memory ALLOCation malloc : Memory ALLOCation

calloc calloc(n, el_size) void main() an array of n elements, each element having el_size bytes void 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 allocated dynamically should be returned */ }

malloc /* malloc does not initialize memory */ void main() { int *a; //will be used as an array int n; // size of array printf(“give a value for n:”); scanf(“%d”,&n); a=malloc(n*sizeof(int)); /* get space for a (allocate a)*/ .... free(a); }