1 More on Pointers. 2 Reminder 3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7.

Slides:



Advertisements
Similar presentations
Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Chapter 10.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Programming Review: Functions, pointers and strings.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Exercise 10 Review: pointers, strings and recursion.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Introduction to C programming
While Loop Structure while (condition) { …. // This line will process when while condition is true }
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
 Recursion  Pointers and Arrays #include void print3(int n){ if (n==0)return; printf ("%d\n",n); print3(n-1); printf ("%d\n",n); } void main(){ print3(5);
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
Chapter 8 Arrays, Strings and Pointers
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings (מחרוזות).
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
INC 161 , CPE 100 Computer Programming
Strings A string is a sequence of characters treated as a group
Arrays in C.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Chapter 16 Pointers and Arrays
C++ Pointers and Strings
Exercise Arrays.
Programming Strings.
CS1100 Computational Engineering
C++ Pointers and Strings
Presentation transcript:

1 More on Pointers

2 Reminder

3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7 34…… …… P

4 Brief Summary &x – address (pointer) of variable x *y – content in address y (common) usage: int z = *y; (common) usage: printf(“%d”,*y); int */double */char * - define pointer to the corresponding primitive (common) usage: int * p = &x; int x = 8; int *y = &x; *y = 10; printf(“%d”,*(&x));

5 Common errors It is impossible to define pointers to constants or expressions. i = &3; j = &(k+5); It is also impossible to change a variable’s address (because it is not for us to determine!). &a = &b; &a = 150;

6 Exercise (from last week) Write a function that accepts a double parameter and returns its integer and fraction parts Write a program that accepts a number from the user and prints out its integer and fraction parts, using this function

7 dbl_split.c void split(double num, int *int_part, double *frac_part) { *int_part = (int)num; *frac_part = num - *int_part; }

8 dbl_split.c int main(void) { double num, fraction; int integer; printf("Please enter a double number: "); scanf("%lf", &num); split(num, &integer, &fraction); printf("The integer part is %d\n", integer); printf("The fraction part is %g\n", fraction); return 0; }

9 Pointers and Arrays Recall that an array S holds the address of its first element S[0] S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0]

10 Pointer versus Array Array is a pointer Array declaration and memory allocation A pointer is uninitialized when declared (like a primitive variable) The value of an array variable (the address it is pointing) cannot be changed – unlike pointers!

11 Pointer arithmetic Pointers can be incremented and decremented If p is a pointer to a particular type, p+1 yields the correct address of the next variable of the same type How? p++, p+i, and p += i also make sense

12 Pointer arithmetic - example If p and q point to elements in an array, q-p yields the number of elements between p and q. int a[] = {0,1,2,3}; int * b = a; b += 4; printf("last element: %d, diff: %d\n",*(b-1),b-a);

13 array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }

14 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 0 i ‘!’

15 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 0 i ‘!’

16 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘!’

17 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘!’

18 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘e’‘!’

19 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘!’

20 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘!’

21 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘s’‘!’

22 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘!’

23 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘!’

24 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘\0’‘!’

25 Pointer arithmetic strcpy void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; }

26 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘!’

27 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

28 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

29 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

30 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’

31 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

32 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

33 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

34 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’

35 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’

36 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’

37 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’

38 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’

39 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘\0’‘!’

40 An additional use A function that accepts an array and searches for something within it (a char, a number, etc.) What should the function return? The index where the ‘something’ was found A pointer to the place where it was found

41 Functions that return pointers Like any other data type, functions can return pointers For example, a prototype for a function returning a pointer to char will be – char *func(…); But how would we indicate failure Suppose we searched the array and didn’t find anything – what should we return?

42 The NULL pointer The NULL pointer is an ‘empty’ pointer that points to nothing. It is the address number 0. If a pointer p is set to NULL, trying to access *p results in a run-time (not compilation) error. Often used to indicate failure

43 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

44 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

45 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

46 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

47 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

48 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

49 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

50 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

51 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

52 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search

53 Example strchr.c

54 Mystery – what does this do? int main(void) { char s[LENGTH+1]; char *p; scanf("%100s", s); p = strchr(s, ','); while(p!=NULL) { *p = ' '; p = strchr(p+1, ','); } printf("The result is - %s\n", s); return 0; }

55 Exercise Implement the string.h function strrchr that returns a pointer to the last occurrence of a character inside a string. Write a program that accepts a string and char from the user, displays what remains of the string after the last occurrence of that char.

56 strrchr.c char* my_strrchr(char str[], char c) { char *search = str + strlen(str) - 1; /* search now points to the beginning of str */ while(search >= str) { if(*search == c) return search; search--; /* advance pointer by one */ } /* if we're here, we didn't find */ return NULL; }

57 strncmp Declared in string.h, with prototype int strncmp(char *s1, char *s2, int n); Returns 0 if the first n letters of s1 are equal to those of s2 Returns non-zero if first n letters are different

58 Exercise Using strncmp, implement the following function – Input – two strings str1, str2 Output – a pointer to the first instance of str2 in str1, or NULL Write a program that accepts two strings from the user and reports whether the first contains the second

59 strstr.c char* my_strstr(char str1[], char str2[]) { char *search = haystack; /*search now points to the beginning of str*/ int len1 = strlen(str1), len2 = strlen(str2); int i; for(i = 0; i <= len1-len2; i++) { /* strncmp(s1, s2, n) compares the first n letters of s1 and s2 */ if(strncmp(search, str2, len2) == 0) return search; search++; } /* if we're here, we didn't find */ return NULL; }

60 home Implement the following function – Input – two strings str1, str2 Output – pointer to the first instance in str1 of any of the characters contained in str2 Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces

61 solution strcspn.c

62 Some functions of string.h Implementations of functions from string.h : strcpy.c – copies one string into the other strchr.c – returns a pointer to the first occurrence of a character within a string strstr.c – returns a pointer to the first occurrence of one string within another strcspn.c – returns a pointer to the first occurrence of a single char from one string in the second