1 TDBA66, vt-04, Lecture Ch8 Pointers & pointer arithmetics All variables are bound to a certain address in memory Ex. int i; the address of i is &i the.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

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.
A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 TDBA66, vt-04, Lecture Ch9 Type declaration Possible to create user defined data types Used to give special names for a certain data type typedef data.
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 and Arrays C and Data Structures Baojian Hua
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
1 TDBA66, VT-04, Lecture Ch7 One dimensional Arrays A data structure (vector) with many elements of the same type A common name where individual elements.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Chapter 8 Arrays and Strings
Pointers Applications
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Chapter 8 Arrays and Strings
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
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”
WEEK 6 Class Activities Lecturer’s slides.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
System Programming Practical Session 7 C++ Memory Handling.
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;
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Stack and Heap Memory Stack resident variables include:
Characters and Strings
Command Line Arguments
Command Line Arguments
Representation of data types
Arrays and Pointers CSE 2031 Fall September 2018.
Command Line Arguments
C Programming Language
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
prepared by Senem Kumova Metin modified by İlker Korkmaz
Procedure Activations
Outline Defining and using Pointers Operations on pointers
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
CSCE 206 Lab Structured Programming in C
Characters and Strings
Presentation transcript:

1 TDBA66, vt-04, Lecture Ch8 Pointers & pointer arithmetics All variables are bound to a certain address in memory Ex. int i; the address of i is &i the value of a pointer variable is an address int *p = &i; /* pointer to an integer */ NULL as the value of a pointer variable indicates ”points to nothing” Possible to reach other memory cell using the pointer p p+1 is the next address. Note! Pointer arithmetic *(p+1) is the value of the ”next integer”

2 TDBA66, vt-04, Lecture Ch8 Pointers as parameters The value of an argument is copied to the formal parameter I.e. Let two variables exchange values void byt(int *a, int *b) { int t; /* local variable */ t = *a; *a = *b; *b = t; } int v1,v2; byt(&v1,&v2); Call

3 TDBA66, vt-04, Lecture Ch8 One-dimensional arrays & pointers The array name is a pointer to the first element in the array int list[25]; now list &list[0] and list[0] *list and list[3] *(list+3) …but the array name can’t change value

4 TDBA66, vt-04, Lecture Ch8 Arguments to function main Declaration as int main(int argc, char *argv[]) Makes it possible to supply command line arguments at run time Ex../a.out 100 filein.txt This command makes argc == 3 (number of ”words” in command) and argv[i] points to the strings given at run time argv[0] ”./a.out” argv[1] ”100” argv[2] ”filein.txt”

5 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat stat_slump.c #include int main(int argc, char *argv[]) { int i, n, die_1, die_2, two_sum; double prob; int diec_res[13]; srand(time(NULL)); /* intialize random number generator */ /* Simulate throwing two dice a number of times. */ /* How many throws? */ if (argc == 1){/* No arguments */ printf("How many throws: "); scanf("%d", &n); fflush(stdin); } else{ sscanf(argv[1],"%d", &n); printf("\nValue of string pointed to by argv[1] %s\n", argv[1]); }

6 TDBA66, vt-04, Lecture Ch8 printf("\nValue of n= %d\n", n); for (i=2; i<13; i++) /* zero all summation variables */ diec_res[i]=0; for (i = 0; i < n; ++i) {/* simulate n trows with two diec */ die_1= (int) floor(rand()/(double)RAND_MAX*6.0)+1; die_2= (int) floor(rand()/(double)RAND_MAX*6.0)+1; two_sum = die_1+ die_2; diec_res[two_sum]++; } printf("\n\n"); printf("Relative frequence Probability\n"); for (i=2; i<13; i++){ printf("%15.3f", diec_res[i]/(float)n); if (i < 8) prob = (i-1)/36.0; else prob = (13-i)/36.0; printf("%15.3f\n", prob); } return 0; }

7 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod>./a.out How many throws: Value of n= Relative frequence Probability peppar:~/c/Ckod>./a.out Value of string pointed to by argv[1] Value of n= Relative frequence Probability

8 TDBA66, vt-04, Lecture Ch8 bubbel again void bubbel(int list[],int n) { int i,last; int bytt; /* logical */ last = n-1; /* last index in list */ do {/* place largest element in last position */ bytt = 0; /* logical false */ for (i=0;i <= last-1; i++) { if (list[i] > list[i+1]) { byt(&list[i],&list[i+1]); bytt = 1; /* logical true */ } last = last - 1; }while (bytt && last!=1); } /* bubbel */

9 TDBA66, vt-04, Lecture Ch8 In bubble function Comparisons : if (list[i] > list[i+1]) if (*(list+i) > *(list+i+1)) Calls : byt(&list[i],&list[i+1]); byt(list+i,list+i+1);

10 TDBA66, vt-04, Lecture Ch8 Two-dimensional arrays Normally int a[4][3];/* memory for 12 integers is allocated */ and the base address is &a[0][0] which means a[i][j] *(a+i*3+j) #values/row row number

11 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat writeMatrix2.c #define MAXROW 10 #define MAXCOL 20 void writeMatrix2(double matrix[][MAXCOL], int m, int n) { int i,j; for (i=0;i<m;i++) { for (j=0;j<n;j++) printf("%7.1lf",matrix[i][j]); putchar('\n'); }; }/* writeMatrix2 */

12 TDBA66, vt-04, Lecture Ch8 Picture a a[0] a[1] a[2] [0][1][2] a[3] Possible to create structure like this

13 TDBA66, vt-04, Lecture Ch8 double **a; int i,j; a = calloc(4,sizeof(double *)); for (i=0;i<4;i++) a[i] = calloc(3,sizeof(double));

14 TDBA66, vt-04, Lecture Ch8 Matrix write void writeMatrix(double **matrix, int m, int n) { int i,j; for (i=0;i<m;i++)/* i control row no. */ { for (j=0;j<n;j++)/* j control column no. */ printf("%7.1lf",matrix[i][j]); putchar('\n');/* new line on screen */ }; }/* writeMatrix */

15 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod> cat testWriteMatrix.c #include #define MAXROW 10 #define MAXCOL 20 void writeMatrix(double **matrix, int, int); void writeMatrix2(double matrix[][MAXCOL], int, int); int main(void) { double **a; double B[MAXROW][MAXCOL]; int i,j; a = calloc(4,sizeof(double *)); for (i=0;i<4;i++) a[i] = calloc(3,sizeof(double)); srand(time(NULL)); /* slumptalsfrö */ for (i=0;i<4;i++) for (j=0;j<3;j++){ a[i][j] = rand()% ; B[i][j] = a[i][j]; } writeMatrix(a,4,3); putchar('\n'); writeMatrix2(B,4,3); return 0; }/* main */

16 TDBA66, vt-04, Lecture Ch8 peppar:~/c/Ckod>./a.out Run

17 TDBA66, vt-04, Lecture Ch8 Conway’s Game of Life Programming project no. 3 in Ch. 8 in text book (p. 397) is about how to simulate the Game of Life. See separate papers (used functions can be found on the homepage of the course, see the link ”C-code demos”)