Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.

Slides:



Advertisements
Similar presentations
Pointers and Arrays Chapter 12
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Multiple-Subscripted Array
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Programming C/C++ on Eclipe C Training Trình bày : Ths HungNM.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
A First Book of ANSI C Fourth Edition
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Chapter 11 introduced pointers and showed how they're used as function arguments and as values returned by functions. This chapter covers another application.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Arrays.
Computer Programming for Engineers
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Engineering Computing I Chapter 5 Pointers and Arrays.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Using a Pointer as an Array Name C allows us to subscript a pointer as though it were an array name Here's an example: #define N 100 int a[N], i, sum =0,
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Arrays. Arrays are objects that help us organize large amounts of information.
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)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Chapter 7 Pointers and C-Strings
Hassan Khosravi / Geoffrey Tien
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
Pointers and Arrays Chapter 12
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
Pointers and Arrays Chapter 12
Outline Defining and using Pointers Operations on pointers
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Lecture-8 Pointers and Memory Management
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Presentation transcript:

Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training

Training C/C++EcoSoftware 2 Pointer and Pointer with Arrays o Introduction Pointer. o Declaring pointer variable. o Pointer with operator o Pointer Assignment o Pointer as arguments o Pointers to Constants o Array and pointer o Function pointer.

Training C/C++EcoSoftware 3 Introduction Pointer o Variables that can store addresses are called pointers. o The address that’s stored in a pointer is usually that of another variable o int number = 5; o int *P = &number;

Training C/C++EcoSoftware 4 Declaring pointer variable o Using statement:  Datatype *namePointer; o Example :  int *p ; // pointer to only integers  double *p ; // pointer to only doubles  char *p ; // pointer to only characters

Training C/C++EcoSoftware 5 The address and indirection Operators o The address operators  Declaring a pointer variable set aside space for pointer. int *p;  Must be init p before we use it. p = &i;

Training C/C++EcoSoftware 6 The address and indirection Operators o The indirection Operators.  Use the *(indirection) operator to access what’s stored in the object.  Example. int i = 5; int *p = &i; printf(“%d”,*p); printf will be display value of i, not address of i.  As long as p point to i.  *p is an alias for i.

Training C/C++EcoSoftware 7 Pointer Assignment o Use of the assignment operator to copy pointers.  int i, *p, *q;  p = &i; The address of i is copied into p;  q = p; This statement copies the content of p (address of i) into q *p = 1; *q = 2;

Training C/C++EcoSoftware 8 Pointer Assignment o Example 1  p = &i;  q = &j;  i = 1; o Example 2 o *p = *q;  Copy value that p pointers to (the value i) into the object that q point to (the variable j)

Training C/C++EcoSoftware 9 Using a pointer with scanf() o Example.  int value = 0;  int *pvalue = NULL;  pvalue = &value; /* Set pointer to refer to value */  printf ("Input an integer: ");  scanf(" %d", pvalue); /* Read into value via the pointer */  printf("\nYou entered %d\n", value);

Training C/C++EcoSoftware 10 Pointer as arguments o Using a pointer use to argument for function.  Example.  Prototype of decompose void decompose(double, long *, double *)  Call decompose decompose( , &i, &d);

Training C/C++EcoSoftware 11 Pointers to Constants o Use the const keyword when you declare a pointer to indicate that the value pointed to must not be changed.  long value = 9999L;  const long *pvalue = &value; /* Defines a pointer to a constant */  *pvalue = 8888L; /* Error - attempt to change const location */ o You have only asserted that what pvalue points to must not be changed. o You are quite free to do what you want with value:  value = 7777L;  long number = 8888L;  pvalue = &number; /* OK - changing the address in pvalue */

Training C/C++EcoSoftware 12 Constant Pointers o The address stored in a pointer cannot be changed. o using the const keyword slightly differently in the declaration of the pointer  int count = 43;  int *const pcount = &count; /* Defines a constant */ o Can’t changed address of a pcount.  int item = 34;  pcount = &item; /* Error - attempt to change a constant pointer */ o Can be change the value that pcount points  *pcount = 345; /* OK - changes the value of count */

Training C/C++EcoSoftware 13 Arrays and Pointers o Pointer Arithmetic  Use pointer can point to array element. int a[10], *p; p = &a[0];  Use can store 5 into a[0] by writing. *p = 5;

Training C/C++EcoSoftware 14 Arrays and Pointers o Adding an integer value to a pointer  Example :  int a[10], *p; *q;  p = &a[2];  q = p + 3;  p += 6;

Training C/C++EcoSoftware 15 Arrays and Pointers o Subtructing an integer from a pointer.  If p pointer to the array element a[i], then p-j pointer to a[i-j];  Example. int *p = &a[8]; q = p-3; p -= 6;

Training C/C++EcoSoftware 16 Example Pointer.

Training C/C++EcoSoftware 17 Arrays and Pointers o Pointer to compound literals  A pointer to point to an element with in an array created by compound literals.  Example. int *p = (int []){3,0,3,4,1}; p point to first element of five element array {3,0,3,4,1}.  Other way: int a[] = {3,0,3,4,1}; p = &a[0];

Training C/C++EcoSoftware 18 Using pointers for array processing o Combining the * and ++ Operators  Using combine the * (inderection) and ++ operators in statement in process array elements. a[i++] = j;  Using p is pointing to an array element. *(p++) = j;  Notes :

Training C/C++EcoSoftware 19 Using pointers for array processing o Combining the * and ++ Operators  Example : Total array with using pointer. –int a[] = {3,5,7,10,23}; –int *p; »Using for for(p=&a[0]; p<&a[N]; p++) { sum += *p; } Using while p = &a[0]; while (p<&a[N]) sum += *p++;

Training C/C++EcoSoftware 20 Using array name as a pointer o The name of an array can be use as a pointer to the first element in the array.  Example : int a[10]; *a = 7; /* store 7 in a[0] */. *(a+1) = 12 /* store 12 in a[1]*/; *(a+n) = k /* store k in a[n] */ o Using for, while to view data with array.  Example : for(p = &a[0]; p<&a[n]; p++) { sum +=*p; }

Training C/C++EcoSoftware 21 Example Using pointer with array o Reversing a series of numbers.

Training C/C++EcoSoftware 22 Pointer with multidimensional array. o Processing the elements of multidimension arrays. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int row, col; for(int row=0; row< NUM_ROWS ; row++) for(int col=0; col < NUM_ROWS ; col++) a[row][col] = 0;

Training C/C++EcoSoftware 23 Pointer with multidimensional array. o Using pointer with multidimensional array. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int *p; for(p=&a[0][0]; p< &a[NUM_ROWS-1][NUM_COLS]; p++) *p= 0;

Training C/C++EcoSoftware 24 Processing the rows of a multidimensional array o Example. o Using pointer with multidimensional array. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int *p, i; for(i = 0; i<NUM_ROWS; i++) for(p=a[i]; p<a[i] + NUM_COLS; p++) *p= 0;

Training C/C++EcoSoftware 25 Processing the colum of a multidimensional array o Example. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int (*p)[NUM_COLS], i; for(i = 0; NUM_COLS; i++) for(p=&a[0]; p<&a[NUM_ROWS]; p++) (*p)[i] = 0

Training C/C++EcoSoftware 26 Using the name of multidimension array as a pointer o Example.  Not using array name. for(p=&a[0]; p<&a[NUM_ROWS]; p++) (*p)[i] = 0;  Using array name. for(p = a; p<a + NUM_ROWS; p++) –(*p)[i] = 0;

Training C/C++EcoSoftware 27 Example Multidimentional array with pointer

Training C/C++EcoSoftware 28 Function Pointer o Declaration function Pointer.  Datatype (*name function)();  Example : int(*cmp)(); // function pointer has integer type float(*fcmp)(); // function pointer has float type.

Training C/C++EcoSoftware 29 Example function pointer. #include intadd( int a, int b) { return(a+b);} intsubtruct( int a, int b) { return(a-b);} intmultiable( int a, int b) { return(a*b);} intmodul( int a, int b) { return(a%b);} void main(void) { int (*cmp)(); /* function pointer has int type*/ int a, b; printf(“\n Input a=”); scanf(“%d”, &a); printf(“\n Input b=”); scanf(“%d”, &b); cmp=add; // Using function pointer printf(“\n Addition a + b =%d”, cmp(a,b));

Training C/C++EcoSoftware 30 Example function pointer. cmp = subtruct; printf(“\n Subtruction a - b =%d”, cmp(a,b)); cmp = multiable; printf(“\n Multiable a * b =%d”, cmp(a,b)); cmp = modul; printf(“\n modul(a, b) =%d”, cmp(a,b)); getch(); }

Training C/C++EcoSoftware 31 Thank You End