More Pointers Week 11

Similar documents
A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Chapter 6 - Pointers

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Language comparison. C has pointers. Java has references. C++ has pointers and references

MYcsvtu Notes LECTURE 34. POINTERS

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Lecture 05 POINTERS 1

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Lecture 9 - Pointers 1

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Arrays and Pointers (part 1)

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Computer Programming Lecture 12 Pointers

Programming for Engineers Pointers

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Fundamental of Programming (C)

Functions. Arash Rafiey. September 26, 2017

Arrays, Pointers and Memory Management

Introduction to Scientific Computing and Problem Solving

Course organization. Course introduction ( Week 1)

Arrays and Pointers (part 1)

Content. In this chapter, you will learn:

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

C++ Programming Chapter 7 Pointers

Basic and Practice in Programming Lab7

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Pointer. Pointer variable

Pointers. 10/5/07 Pointers 1

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

C Pointers Pearson Education, Inc. All rights reserved.

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Principles of Programming. Chapter 6: Arrays

Lab 3. Pointers Programming Lab (Using C) XU Silei

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

BSM540 Basics of C Language

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Variation of Pointers

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

POINTER & REFERENCE VARIABLES

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

by Pearson Education, Inc. All Rights Reserved.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

CS 241 Data Organization Pointers and Arrays

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

AMCAT Automata Coding Sample Questions And Answers

Variables and Operators 2/20/01 Lecture #

Lecture 3. More About C

return return else return

Slides adopted from T. Ferguson Spring 2016

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Fundamentals of Programming Session 20

ESC101N Fundamentals of Computing

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

This is CS50. Harvard University Fall Quiz 0 Answer Key

Fundamentals of Programming Session 20

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Personal SE. Arrays Pointers Strings

CSC 2400: Computer Systems. Arrays and Strings in C

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Array Initialization

Introduction to C Language (M3-R )

Pointers, command line arguments, Process control, and functions

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

C programming basics T3-1 -

Dynamic memory allocation (malloc)

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

ET156 Introduction to C Programming

Subject: Fundamental of Computer Programming 2068

BİL200 TUTORIAL-EXERCISES Objective:

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

CSC 2400: Computer Systems. Arrays and Strings in C

Create a Program in C (Last Class)

... Lecture 12. Pointers

Introduction to C. Systems Programming Concepts

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS 61c: Great Ideas in Computer Architecture

Procedural Programming & Fundamentals of Programming

OBJECT ORIENTED PROGRAMMING

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C-LANGUAGE CURRICULAM

Multidimension array, array of strings

Computer Programming Unit 3

Transcription:

More Pointers Week 11

Pointer Variable Normal variables Contain a specific value (direct reference) Pointer variables Contain memory addresses as their values countptr count 7 count 7

Pointer Expressions and Arithmetic Arithmetic operations can be performed on pointers Increment/decrement pointer (++ or --) Add an integer to a pointer ( + or +=, - or -=) Pointers may be subtracted from each other Operations meaningless unless performed on an array

Pointer Expressions and Arithmetic operation p++, p-- p+i (p-i) p[i] p + n (integer) Description Increment (decrement) p to point to the next element, it is equivalent to p+=1 (p -=1) Point to i-th element beyond (in front of) p but value of p is fixed Equivalent to p + i n must be an integer, its meaning is offset p - q Offset between pointer p and pointer q p+q, p*q, p/q, p%q Relational operator of two pointers p, q invalid valid, including p > q, p < q, p == q, p >= q, p <= q, p!= q

Pointer Expressions and Arithmetic Pointer comparison ( <, ==, > ) See which pointer points to the higher numbered array element (index) Also, see if a pointer points to 0

Example 1 (strcmp with pointers) #include <stdio.h> int main() { char line[20]; char *part = "hello"; do { printf("\nenter new String: "); gets(line); if(strcmp(line, part) == 0) printf("the same string %s\n", line); } while(strlen(line)!= 0) ; return 0; }

Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer void mydouble( int *number ) { int x; x = *number ; *number = 2 * x; } In main(): mydouble (&y) ;

Example 2 (print characters) #include <stdio.h> void printchars (char *string) { int count; for(count = 0; count < strlen(string); count ++) } { printf ( \n char no: %d is %c, count, string [count]) ; string[count] = a + count ; } int main() { char Arr[]= This is a test ; puts (Arr); printchars ( Arr ); printf ( \n %s, Arr); return 0; }

Arrays and Pointers

Arrays and Pointers Note a same &a[0] a is a pointer only to the first element not the whole array. Note The name of an array is a pointer constant; it cannot be used as an lvalue.

Arrays and Pointers

Arrays and Pointers

Arrays and Pointers Note To access an array, any pointer to the first element can be used instead of the name of the array.

Pointers and Arrays Compiler determine length of string and then size of amessage pmessage: string constant, cannot modified n o w i s t h e t i m e \0 Modifiable character array amessage: n o w i s t h e t i m e \0

Example 3 (Array and pointers) #include <stdio.h> int my_array[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i; } ptr = &my_array[0]; /* point our pointer to the first element of the array */ printf("\n\n"); for (i = 0; i < 6; i++) return 0; { printf("my_array[%d] = %d ", i, my_array[i] ); printf( \t ptr + %d = %d\n", i, *(ptr + i) ); }

Example 4 (count a character) #include <stdio.h> int countnchar (char *string, char ch) { char *p; int count = 0; for(p = string; *p!= '\0'; p++) { if(*p == ch) count++; } return count; } int main() { char f = A ; int x ; char Arr[]= This is A test for letter A ; x = countnchar ( Arr, f ); printf ( Letter A exist : %d times, x ); return 0; }

Example 5 (swap with array index) #include <stdio.h> # define N 5 void swap (int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int main() { int i, j; int Arr[N]={1,2,3,4,5}; for (i=0; i < N/2 ; i++) swap ( & Arr [ i ], & Arr [ (N-1) i ] ); return 0; }

Example 6 (swap with pointers) #include <stdio.h> # define N 5 void swap (int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int main() { int i, j; int Arr[N]={1,2,3,4,5}; for (i=0; i < N/2 ; i++) swap ( Arr + i, Arr + (N-1) i ); return 0; }

Example 7 (reverse with pointers) #include <stdio.h> void reverse(char *mysrt) { char * lp = mysrt; /* left pointer */ } char *rp = &mysrt[strlen(mysrt)-1]; /* right pointer */ char tmp; while(lp < rp) { tmp = *lp; *lp = *rp; *rp = tmp; lp++; rp--; } int main() { char Arr[]="This is a test"; puts(arr); reverse (Arr); printf("\n"); puts(arr); return 0; }

Arrays of Pointers Arrays can contain pointers to..

Array of Pointers int x = 4; int *y = &x; int *z[4] = {NULL, NULL, NULL, NULL}; int a[4] = {1, 2, 3, 4}; z[0] = a; // same as &a[0]; z[1] = a + 1; // same as &a[1]; z[2] = a + 2; // same as &a[2]; z[3] = a + 3; // same as &a[3]; for (x=0;x<4;x++) printf("\n %d --- %d ",a[x],*z[x]);

Arrays of Pointers Arrays can contain pointers to (array)

Array of Pointers For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; Strings are pointers to the first character Each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size

Arrays of Pointers char * each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size suit[0] H e a r t s \0 suit[1] D i a m o n d s \0 suit[2] C l u b s \0 suit[3] S p a d e s \0

Example 8 (Array of strings) char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; int main() { int x ; for (x = 0; x < 4 ; x++) printf("\n %s --- %d ", suit[x],strlen(suit[x])); return 0; }

Pointer to Structure We can use pointer to struct: struct MyPoint {int x, int y}; MyPoint point, *ptr; point.x = 0; point.y = 10; ptr = &point; ptr->x = 12; same as (*ptr).x ptr->y = 40; same as (*ptr).y

Example 9 (pointer to struct) #include <stdio.h> struct inven { char code; float cost; int pices ; } ; int main() { struct inven part; read (&part); write (part); return 0; }

Example 9 (pointer to struct) -cont void read (struct inven *in) { printf ("\n Enter Product Data. \n") ; printf (" Enter part code: "); scanf ("%c",&in->code); printf (" Enter part cost: "); scanf ("%f",&in->cost); printf (" Enter no of pices: "); scanf ("%d",&in->pices); } void write (struct inven out) { printf (" part code: %c \n", out.code); printf (" part cost: %f \n", out.cost); printf (" no of pices: %d \n", out.pices); }

Questions? Pointers can Pointers point to everywhere, anywhere,. in MEMORY!