(PART 2) 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

Lecture 20 Arrays and Strings
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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:
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Pointers Applications
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
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”
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
5.13 Recursion Recursive functions Functions that call themselves
C Primer.
Recursion.
Characters and Strings
Command Line Arguments
Command Line Arguments
Understand argc and argv
Command-Line Arguments
Programmazione I a.a. 2017/2018.
Pointers.
Arrays and Pointers CSE 2031 Fall September 2018.
Recursion.
Lecture 6 C++ Programming
14th September IIT Kanpur
Pointers.
Programming and Data Structures
C Programming Language
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Dynamic Memory Allocation
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Recursion.
Pointers.
prepared by Senem Kumova Metin modified by İlker Korkmaz
By Hector M Lugo-Cordero September 17, 2008
Outline Defining and using Pointers Operations on pointers
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7. Pointers, Dynamic Memory
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays, Pointers, and Strings
Characters and Strings
C++ Pointers and Strings
15213 C Primer 17 September 2002.
Pointers.
Presentation transcript:

(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz ARRAYS, POINTERS (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz

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); }

Array Sorting Example: BUBBLE SORT void swap(int *, int *);  /* swap was defined before */ void bubble(int a[ ], int n)  /* n is the size of a[] */ {   int  i, j;   for (i = 0; i < n - 1; ++i)        for (j = n -1; j > i; --j)             if (a[j-1] > a[j])                 swap(&a[j-1], &a[j]); } /* bubble sort algorithm has a worst-case complexity of O(n2) */

Arguments to main() (1) Two arguments, conventionally called argc and argv, can be used with main() to communicate with the operating system. The variable argc >= 1 provides a count to the number of command line arguments, including the program’s name itself. The array argv is an array of pointers, each pointer pointing to a string - a component of the command line. main( int argc, char *argv[])

Arguments to main() (2) /* echoing the command line arguments.*/ #include <stdio.h>  int main(int argc, char *argv[])  {     int i;     printf("argc = %d\n", argc);     for (i = 0; i < argc; ++i)         printf("argv[%d] = %s\n", i, argv[i]);     return 0;  } /* try to run the program with some arguments at the command line */

The Type Qualifiers const and volatile They restrict, or qualify, the way an identifier of a given type can be used. “const” comes after the storage class (if any), but before the type, means that the variable can be initialized, but thereafter the variable cannot be assigned to or modified. static const int k = 3; “volatile” object is one that can be modified in some unspecified way by the hardware. extern const volatile int real_time_clock; Since the storage class is “extern”, the system looks for “real_time_clock” either in this file or in some other file. The “volatile” qualifier indicates that the object may be acted on by the hardware; because it is “const”, it cannot be modified by the program, however the hardware can change the clock.