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

Similar documents
by Pearson Education, Inc. All Rights Reserved.

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C++ Programming Chapter 7 Pointers

C Pointers Pearson Education, Inc. All rights reserved.

Chapter 6 - Pointers

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

Lecture 05 POINTERS 1

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

Fundamentals of Programming Session 20

MYcsvtu Notes LECTURE 34. POINTERS

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

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

Programming for Engineers Pointers

C++ for Java Programmers

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

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

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Content. In this chapter, you will learn:

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

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

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

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Fundamental of Programming (C)

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Pointers and Strings. Adhi Harmoko S, M.Komp

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

PROGRAMMAZIONE I A.A. 2017/2018

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

SYSC 2006 C Winter 2012

Exercise 1.1 Hello world

Chapter 7 C Pointers

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

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

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

Pointers. 10/5/07 Pointers 1

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

More Pointers Week 11

Week 3: Pointers (Part 2)

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

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

Introduction to Scientific Computing and Problem Solving

... Lecture 12. Pointers

Homework #3 CS2255 Fall 2012

First of all, it is a variable, just like other variables you studied

Procedural programming with C

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Functions. Introduction :

12. Pointers Address-of operator (&)

IS 0020 Program Design and Software Tools

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

POINTER & REFERENCE VARIABLES

Relationship between Pointers and Arrays

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Pointers as Arguments

VARIABLES & ASSIGNMENTS

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Work relative to other classes

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

BITG 1113: POINTER LECTURE 12

CSC 211 Intermediate Programming. Arrays & Pointers

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

Introduction to Computer Science Midterm 3 Fall, Points

A First Program - Greeting.cpp

Pointers. Variable Declaration. Chapter 10

OBJECT ORIENTED PROGRAMMING

CS201 Some Important Definitions

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

Lecture 9 - Pointers 1

CS2255 HOMEWORK #1 Fall 2012

Reference operator (&)

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Lecture 23: Pointer Arithmetic

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Objectives. In this chapter, you will:

Pointers, Dynamic Data, and Reference Types

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Pointers, Arrays and C-Strings

What is an algorithm?

DECLARAING AND INITIALIZING POINTERS

Variation of Pointers

Pointers. Reference operator (&) ted = &andy;

Pointers. Memory. void foo() { }//return

A Fast Review of C Essentials Part I

Lecture 04 Introduction to pointers

Lecture on pointers, references, and arrays and vectors

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Tokens, Expressions and Control Structures

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Transcription:

Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the type of the variable. The way to find the size of the various types on your system is to run the following code that gives the information. #include <iostream> using namespace std; int main(void) { cout <<"size of a short is <<sizeof(short); cout <<"size of a int is" <<sizeof(int); cout << "size of a double is" <<sizeof(double); } Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

When we declare a variable, we inform the compiler of two things the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing: int k; On seeing the "int" part of this statement the compiler: sets aside 4 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside. k 1000 Symbol Name Symbol Relative Address k 1000 If, we later write: k = 2;

at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. 1000 0000 0000 0000 0010 In C, we refer to a variable such as the integer k as an "object". There are two values associated with the object k: 1. the rvalue: the value of the integer stored there (2 in the above example) 2. the lvalue: the "value" of the memory location, or the address of k. Now consider: int j, k; k = 2; j = 7; line 1 k = j; line 2 In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue. That is, here the j refers to the value stored at the memory location set aside for j, in this case 7.

Therefore, the 7 is copied to the address designated by the lvalue of k. In all of these examples, we are using 4 byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). Such a variable is called a pointer variable. Pointers are variables that contain memory addresses as their values. A pointer usually contains an address of a variable that contains a specific value. In C, we define a pointer variable by preceding its name with an asterisk. Example *a, declares a pointer variable called a. In C, we also give our pointer a type that, refers to the type of data stored at the address stored in the pointer. For example int *a, will indicate that the variable pointer a points to a variable of type integer. -A variable usually contains a specific value, we say it directly references a value. -A pointer indirectly references a value, in that it contains the address of the variable, which directly references it.

K 7 K directly references the variable whose value is 7 ptr * 7 K Ptr indirectly references a variable whose value is 7. Consider the variable declaration: int *ptr; ptr is the name of the pointer variable (just as k was the name of the integer variable). The '*' informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. When a variable contains the address of another variable, the first variable is said to point to the second, hence the name pointer

The general form for declaring a pointer is: data_type *name; data_type: any valid C data type like int, float *: pointer operator name: Any valid C identifier to name the pointer variable Initializing a pointer: Pointers are initialized either when they are declared or in an assignment. A pointer may be initialized to 0, NULL, or an address. -A pointer with the value NULL points to nothing. -NULL is a Symbolic constant defined in the <iostream> header file. -Initializing a pointer to 0 is equivalent to initializing it to NULL, but NULL is preferred. For example: int *ptr; ptr=null; The declaration: ptr = NULL, guarantees that the pointer has become a null pointer.

We can test for a null pointer using if (ptr == NULL). Pointer Operators Suppose now that we want to store in ptr the address of the integer variable k. To do this we use the unary & or address operator and write: ptr = &k; & is a unary operator that returns the address of its operand. The & operator retrieves the lvalue (address) of k, and copies that to the rvalue (or content) of the pointer ptr. Now, the value of ptr is 1000 The operand of the address operator & must be a variable. The address operator cannot be applied to constants, to expressions or to variables declared with the storage class register. The second pointer operator, *, is the dereferencing operator, it is the complement of &. * is a unary operator that returns the value of the variable located at the address that follows It is used as follows:

*ptr = 7; this will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. The statement: int t = *ptr; will place the value of k into t. When we use the '*' this way we are referring to the value that ptr is pointing to, not the value of the pointer itself. Similarly, we could write: printf("%d\n",*ptr); to print to the screen the integer value stored at the address pointed to by ptr. #include <iostream> using namespace std; int main(void){ int j, k; int *ptr; j = 1; k = 2; ptr = &k; cout <<"j has the value "<<j <<" and is stored at" << &j <<endl;

cout << "k has the value "<< k <<" and is stored at" << &k <<endl; cout << "ptr has the value " << ptr << " and is stored at"<< &ptr <<endl; cout << "The value of the integer pointed to by ptr is" <<*ptr <<endl; return 0; } Sample program output:

To review: A variable is declared by giving it a type and a name (e.g. int k;) A pointer variable is declared by giving it a type and a name preceded by the operator * (e.g. int *ptr) where the asterisk tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler what type the pointer is to point to (integer in this case).

Once a variable is declared, we can get its address by preceding its name with the unary & operator, as in &k. We can "dereference" a pointer, i.e. refer to the value of that which it points to, by using the unary '*' operator as in *ptr. An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory. The "rvalue" of a variable is the value stored in that variable (at that address). Pointer Expressions: Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions. Pointer assignments: One reason we need to identify the type of variable that a pointer points to is that if a variable pointer *ptr points to something, and we have an expression like: *ptr = 2; the compiler will know how many bytes to copy into that memory location pointed to by ptr. There are only two arithmetic operations that we may use on pointers: Addition and subtraction. -A pointer may be incremented (++) or decremented (--) -An integer may be added to a pointer (+, or +=) -An integer may be subtracted from a pointer(- or -=)

-or one pointer may be subtracted from another. Pointer Arithmetic: int *ptr; Consider a block in memory consisting of 10 integers in a row. That is, 20 bytes of memory are set aside to hold 10 integers. Now, let's say we point our integer pointer ptr at the first of these integers, located at memory location 100. 100 102 104 106 108 int1 int2 int3 int4 ptr What happens when we write ptr + 1? Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds (2*1) to ptr instead of 1. So, the pointer "points to" the next integer, at memory location 104. When an integer is added to or subtracted from a pointer, the pointer is not simply incremented or decremented by that integer, but by that integer times the size of the object it points to. So the statement: ptr+=2; will produce 104 (100+ 2*2)

Similarly, if ptr had been declared as a pointer to a double, it would add (8*1) to it instead of (2*1). The same goes for other data types such as floats,, short. In C, this operation is referred to as addition using "pointer arithmetic". Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 incrementing a pointer using the unary ++ operator, either preor post-, increments the address it stores by the amount sizeof(type) where "type" is the data type of the object pointed to. (i.e. 4 for an integer, 2 for a character..). Pointer Comparisons: We can compare two pointers in a relational expression. For instance given two pointers p and q, the following statement is valid: if (p<q) printf( p points to lower memory than q\n ); Generally, pointer comparisons are used when two or more pointers point to a common object. Pointers and Arrays Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, we can use pointer to refer to elements in an array.

Consider the following: int my_array[] = {1,23,17,4,-5,100}; Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. Alternatively, we could access them via a pointer as follows: int *ptr; ptr = &my_array[0]; Using either of the statements ++ptr ; or ptr++; increments the pointer to point to the next location or element in the array. Similarly using --ptr or ptr-- decrements the pointer to point to the previous element in the array #include <iostream> using namespace std; int a[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i;

ptr = &a[0]; /* points to the first element of the array */ cout <<"\n\n"; for (i = 0; i < 6; i++){ cout<< "a["<<i<<"] ="<<a[i]<<endl;/*a*/ cout<<"ptr+"<<i<<"is "; cout <<*(ptr+i)<<endl; /*B */} return 0;} When we run the program, it prints out the same values in either case at lines A and B. Sample program output: a[0] = 1 ptr + 0 1 a[1] = 23 ptr+ 1 23 a [2] = 17 ptr+ 2 17 Notice how we dereferenced the pointer in line B, i.e. we first added i to it and then dereferenced the new pointer.

Also, we could have replaced line B with: cout<<"ptr+ <<i<< = << *ptr++<<endl; and gotten exactly the same results. Operator s precedence: Operators Associativity Type () [] Left to right Highest + - ++ --! * & (type) * / % + - < <= > >= ==!= &&?: = += -= *= /= %= Right to left Left to right Left to right Left to right Left to right Left to right Left to right Right to left Right to left Unary Binary Binary Relational Relational Logical AND Logical OR Conditional Assignment In C, wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:

ptr = &a[0]; ptr=a we can write: ptr = a; to achieve the same result. This means that the name of the array is the address of first element in the array. Note that while we can write ptr = a; we cannot write a = ptr; The reason is that while ptr is a variable, a is a constant. That is, the location at which the first element of a will be stored and cannot be changed once a[] has been declared. We refer to an array's name as a constant pointer. Since a is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of a[0] and on seeing: ptr = a; it simply uses this address as a constant in the code. So the array element a[3] can be referenced with the pointer expression : *(ptr+3) or *(a + 3)

The 3 in the expression above is the offset to the pointer. This is called the pointer/offset notation When the pointer points to the beginning of an array, the offset indicates which element of the array is referenced. The offset value is identical to the array subscript or index. The use of ( ) is necessary because * has higher precedence than the +. Pointers can also be indexed or subscripted exactly like arrays, so the expression: ptr[1] (ptr+ 1)refers to a [1]. This is called the pointer/subscript notation. So there are four ways one can use to reference an element in an array: - Array subscripting, - pointer/offset with the array name, - pointer/offset with the pointer name - and pointer subscripting. Calling Functions by Reference: When we pass an integer, for example, to a function, we make a copy of the integer, i.e. get its value and put it on the stack. Within the function, any manipulation of the value passed can in no way effect the original integer.

With arrays and pointers, we pass the address of the variable and hence manipulate the values of the original variables. Many functions require the capability to modify one or more variables in the caller, or to pass a pointer to a large data object. When a variable is to be modified by a function, a pointer to that variable is used as an argument and passed to the function. Pointers and the indirection operator are used to simulate a call by reference. #include <iostream> using namespace std; int cubeandsquare(int, int *); int main(void) { int number = 5, cube, square; int *ptr; ptr =&square; cout<< "The original value of number is "<< number<<endl; cube = cubeandsquare(number, ptr); cout<<"the square of number "<< number<<" is "<< *ptr <<endl;

cout<<"the cube of number "<< number<< " is "<<cube<<endl; return 0;} int cubeandsquare(int n, int *square) { *square= n * n; return n * n * n; } Sample program Output: The original value of number is 5 The square of number 5 is 25 The cube of number 5 is 125 Another Program: We want to write a program that takes an array of 6 integers and returns the max and the min values in the array #include <iostream> using namespace std; int minmax(int[],int, int*); int a[] = {1,23,17,4,-5,100}; int *ptr, min, max;

int main(void){ ptr= &max; min = minmax(a, 6, ptr); cout<<"the max is <<max<< and the min is "<<min<<endl; return 0; } int minmax(int a[], int n, int *max) { int mx, mn, i; mn=a[0]; mx= a[0]; for(i=0; i<n; i++) { } if (mx < a[i]) mx= a[i]; if (mn > a[i]) *max=mx; return (mn); } mn= a[i];

Using the const Qualifier with pointers: The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified. If an argument is passed to a function with a call by value, a copy is made of the argument. If the copy is modified in the function, the original value remains unchanged. However, if we use a call by reference, the address in memory is passed, the function called can change the value of the argument. If a value does not or should not change in the body of a function to which it is passed, the value should be declared const to ensure that it is not accidentally modified. There are four ways to pass a pointer to a function: - A non-constant pointer to a non-constant data: This provides the highest level of access, in which the data and the pointer can be modified. - A non-constant pointer to a constant data: This a pointer that can be modified to point any data item, but the data to which it points cannot be modified.

- A constant pointer to a non-constant data is a pointer that points to the same memory location. An array name is a constant pointer to the beginning of the array. All data of the array can be accessed and modified. A constant pointer to non-constant data can be used to receive an array as an argument to a function. - A constant pointer to constant data. Such a pointer always points to the same memory location, and the data at that memory location cannot be modified. If a function is only to look at an array, it should be declared as: (const datatype *var) Our earlier function is better defined as: #include <stdio.h> int minmax(const int*,int, int*); int a[] = {1,23,17,4,-5,100}; int *ptr, min, max; int main(void){ ptr= &max; min = minmax(a, 6, ptr); printf ("The max is %2d and the min is %2d", max, min);

return 0; } int minmax(const int *ptr_a, int n, int *max) { int mx, mn, i; mn=*ptr_a (mn=a[0]); mx= *ptr_a; for(i=0; i<n; i++) { if (mx < *ptr_a) mx= *ptr_a; if (mn > *ptr_a) mn= *ptr_a; ++ptr_a; } *max=mx; return (mn); } Here the const modifier is used to assure the user that the function will not modify the contents pointed to by the ptr_a pointer.

To check the effect of const within the function, we will add a statement, which attempts to change the contents of that which is pointed to by source, such as: *ptr_a = 1; which would normally change the first element of the array to the value 1 The const modifier should cause the compiler to catch this as an error. Effectively, we get an error message: Cannot modify a const object in function minmax Let s look at the bubble sort program re-written using pointers: #include <stdio.h> #define SIZE 10 void bubblesort(int *, int); int main(void) { int i, a[size] = {2, 5, 3, 8, 86, 10, 12, 43}; printf("data items in original order \n"); for(i=0; i< SIZE; i++) printf("%4d", a[i]);

printf("\n"); bubblesort(a, SIZE); printf("data items in ascending order \n"); for(i=0; i<size; i++) printf("%4d", a[i]); printf("\n"); return 0; } void bubblesort(int *array, int size) { int pass, j; void swap(int *a, int *b); for (pass = 1; pass <= size-1; pass++) for (j= 0; j <= size-2 ; j++) if (array[j]> array[j+1]) swap (&array[j], &array[j+1]); } void swap(int *element1ptr, int *element2ptr) { int temp; temp = *element2ptr;

*element2ptr= *element1ptr; *element1ptr= temp; }

Pointers and Strings: In C, strings are arrays of characters. We have seen that pointers and arrays are interchangeable and that we can use pointers to subscript an array and access its elements. We can initialize an array as: char my_string[40] = "Programming"; -When this is done, the null character ( '\0' ) is automatically appended to the end of the string.. - When this declaration is read, the compiler sets aside a contiguous block of memory 40 bytes long to hold characters and initializes it such that the first 12 characters are Programming\0. We will show a program handling characters using the pointer notation: /*Convert lowercase letters to uppercase letters*/ /*using a non-constant pointer to non-constant data*/ #include <iostream> using namespace std; void converttouppercase(char *); int main(void) { char string[]= "characters";

cout<<"the string before conversion is: "<< string<<endl; converttouppercase(string); cout<<"the string before conversion is: "<< string<<endl; return 0; } void converttouppercase(char *s) { while( *s!= '\0') { if (*s >= 'a' && *s <= 'z') *s -=32; ++s;}} Sample Program Output: The string before conversion is: characters The string after conversion is: CHARACTERS Let s consider the following program that defines two string copying functions: #include <iostream> using namespace std; void copy1(char *, const char *);

void copy2(char *, const char *); int main(void) { char string1[10], *string2= Hello ; char string3[10], string4[] = Good Bye ; copy1(string1, string2); cout<< string1= << string1<<endl; copy2(string3, string4); cout<< string3 = <<string3<<endl; return 0; } void copy1(char *s1, const char *s2) { int j; for (j=0; s1[j] = s2[j]; j++); } void copy2(char *s1, const char *s2) { for ( ; *s1 = *s2; s1++, s2++) ; }

Sample Program Output: string1 = Hello string3 = Good Bye Arrays of pointers: Like any other data type pointer may be arrayed. The declaration for an int pointer array of size 10 is: int *x[10]; To assign the address of an integer variable var to the third element of the array we use: x[2] =&var; To find the value of var, we use: *x[2]; A common use of arrays of pointers is to form an array of strings, or a string array. Each element of the array is a string, which in C is a pointer to its first character. The string is an array of characters. char *suit[4] ={ Hearts, Diamonds, Clubs, Spades }; *suit[4] declares an array of 4 elements, char * indicates that each element is of type pointer to char.

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 Each pointer points to the first character of its corresponding string. Thus, even though the suit array is fixed in size, it provides access to character strings of any length. Build a table of error messages called error_table, that would include these messages: 1. Read error \n 2. Write error \n 3. Cannot open file \n 4. Cannot find file \n char *error_table[4] = { Read error \n, Write error \n, Cannot open file \n, Cannot find file \n }; Write a program that looks up a given name in an array of names. Use the function strcmp to find the name. Answer:

#include <iostream> #include <string.h> using namespace std; #define SIZE 50 int main(void) { char *name[7]= { "Mickey", "Minnie", "Donald", "Felix", "Stumpy", "Dilbert", "Garfield"}; char name1[size]; cout<< "Please enter the name to be searched: \n"; cin >> name1; int t; for (t=0; t<7; t++) if (strcmp(name1, name[t]) == 0) { cout<< "The name was found\n"; if (t == 7) break; }; cout<<"the name was not found\n"; return 0;}

Pointers to Functions: A function has a physical location in memory or an address that can be assigned to a pointer. A pointer to a function contains the address of the function in memory. A function s address is the entry point of the function. A function name is really the starting address in memory of the code that performs the function name. For example int (*functptr)(int, int); This expression declares a pointer to a function that takes two input arguments of type integer and returns an integer result. Pointers to functions can be passed to functions, returned from functions, stored in arrays. #include <iostream> #include <string.h> using namespace std; void check(char *a, char *b, int(*)(const char *, const char *)); void main(void) {

char s1[80], s2[80]; int (*p)(const char *, const char *); p = strcmp; cin>>s1>>s2; check(s1, s2, p); return 0;} void check(char *a, char *b, int (*cmp)(const char *, const char *)){ printf("testing for equality\n"); if (!(*cmp)(a, b)) } printf("equal"); else printf( "not equal"); Sample Program Output 1: This This testing for equality equal Sample Program Output 2:

This this testing for equality not equal