ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 5: Pointers.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Programming and Data Structure
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 in C Rohit Khokher
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
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.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 4: Arrays and Strings.
Pointers Applications
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
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.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
UNIT 5 C Pointers.
CNG 140 C Programming (Lecture set 10)
Lecture 6 C++ Programming
Pointers and References
Lecture 18 Arrays and Pointer Arithmetic
Data Structures and Algorithms Introduction to Pointers
Chapter 9: Pointers and String
Presentation transcript:

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 5: Pointers

ספטמבר 04Copyright Meir Kalech2 What is a Pointer? A pointer is a variable that contains the address of a variable. A Pointer (to type) can be associated with a type. We are familiar with: int, float, char, … types. Each type has a comparable pointer (to) type: int *, float *, char *, etc. The difference: while primitive types store values, the pointer types store addresses. For example: int * stores an address of int. char * stores an address of char. float * stores an address of float. Pointer size is 4 bytes (independently of its type).

ספטמבר 04Copyright Meir Kalech3 Operators * and & C defines two operators: & (address of) and * (indirection) for use with pointers: Indirection Operator * has 2 different meanings: Upon declaration – “ I am a pointer ”. After declaration – access the variable whose address is held by the pointer. Address Operator & provides the address of a variable.

ספטמבר 04Copyright Meir Kalech4 Pointers Example FEDCBA float1: 76.45int1: 24 1F1E1D1C1B1A db1: F2E2D2C2B2A pfloat: cchar1: ‘ a ’ pint: 4 3F3E3D3C3B3A pchar: 29pdb: 12 4F4E4D4C4B4A void main() { int int1=24; float float1=76.45; double db1= ; char char1=‘a’; double *pdb=&db1; char *pchar=&char1; int *pint; float *pfloat; pint = &int1; pfloat = &float1; }

ספטמבר 04Copyright Meir Kalech5 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } Swap two integer values through use of pointers

ספטמבר 04Copyright Meir Kalech6 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } temp: y: x: 200 pint2: 350pint1: 250

ספטמבר 04Copyright Meir Kalech7 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } temp: y: x: pint2: pint1: 250

ספטמבר 04Copyright Meir Kalech8 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } 4 temp: y: x: pint2: pint1: 250 *pint1

ספטמבר 04Copyright Meir Kalech9 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } 4 temp: y: x: pint2: pint1: 250 *pint1 *pint2

ספטמבר 04Copyright Meir Kalech10 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; *pint1 = *pint2; *pint2 = temp; } 4 temp: y: x: pint2: pint1: 250 *pint2

ספטמבר 04Copyright Meir Kalech11 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; pint1 = pint2; *pint2 = temp; } 4 temp: y: x: pint2: pint1: 250 What happens in this case?

ספטמבר 04Copyright Meir Kalech12 Example void main() { int x=4, y=8, temp; int *pint1,*pint2; pint1 = &x; pint2 = &y; temp = *pint1; pint1 = pint2; *pint2 = temp; } 4 temp: y: x: pint2: pint1: 250 This is a pointer assignment!

ספטמבר 04Copyright Meir Kalech13 Be careful int x = 4; int *px; px = x; Error compilation: Cannot convert from int to int * int x = 4; int *px; float *pf; pf = px; BUG! Cannot convert from int * to float * int *px; *px = 9; BUG!!! px points to garbage - trying to assign a value to memory not allocated

ספטמבר 04Copyright Meir Kalech14 Call by Address Passing argument by value: the argument is copied to the local parameter of the function. Passing argument by address: only the address of the argument is passed (a local parameter stores the address). So the operator * enables access to the value of the argument.

ספטמבר 04Copyright Meir Kalech15 Call by Value Example temp num2 num1 Return address y=5 x=3 void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_value main swap gets x and y and swaps between them void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } Address: 100 Address: 200 Address: 500 Address: 400 Address: 300

ספטמבר 04Copyright Meir Kalech16 Call by Value Example temp num2=5 num1= y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_value main Address: 100 Address: 200 Address: 500 Address: 400 Address: 300 Arguments passing void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech17 Call by Value Example temp=3 num2=5 num1= y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_value main Address: 100 Address: 200 Address: 500 Address: 400 Address: 300 temp = num1; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech18 Call by Value Example temp=3 num2=5 num1= y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_value main Address: 100 Address: 200 Address: 500 Address: 400 Address: 300 num1 = num2; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech19 Call by Value Example temp=3 num2=3 num1= y=5 X=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_value main Address: 100 Address: 200 Address: 500 Address: 400 Address: 300 num2 = temp; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech20 Call by Value Example y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } main Address: 100 Address: 200 After swap_by_value has returned Note that x and y weren ’ t changed void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech21 Call by Address Example temp num2 num1 Return address y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_address main swap gets x and y and swaps between them Address: 100 Address: 200 Address: 600 void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } Address: 500 Address: 300

ספטמבר 04Copyright Meir Kalech22 Call by Address Example temp num2=100 num1= y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_address main Address: 100 Address: 200 Address: 300 Arguments passing void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } Address: 600 Address: 500

ספטמבר 04Copyright Meir Kalech23 Call by Address Example temp=3 num2=100 num1= y=5 x=3 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_address main Address: 100 Address: 200 Address: 300 temp = *num1; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } Address: 600 Address: 500

ספטמבר 04Copyright Meir Kalech24 Call by Address Example temp=3 num2=100 num1= y=5 x=5 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_address main Address: 100 Address: 200 Address: 300 *num1 = *num2; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } Address: 600 Address: 500

ספטמבר 04Copyright Meir Kalech25 Call by Address Example temp=3 num2=100 num1= y=3 x=5 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } swap_by_address main Address: 100 Address: 200 Address: 300 *num2 = temp; void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); } Address: 600 Address: 500

ספטמבר 04Copyright Meir Kalech26 Call by Address Example y=3 x=5 void swap_by_value(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; } main Address: 100 Address: 200 After swap_by_value has returned Note that x and y were changed void swap_by_address(int *num1, int *num2) { int temp = *num1; *num1 = *num2; *num2 = temp; } void main() { int x=3, y=5; swap_by_value(x, y); swap_by_address(&x, &y); }

ספטמבר 04Copyright Meir Kalech27 Passing Arguments - Summary Table By valueBy address (of array) Declarationfunc(type name) func(type name[]) func(type *name) Scope In function Lifetime In function Address: lifetime only in function Array ’ s elements: original lifetime Argument changeable No Address: no Array ’ s elements: yes

ספטמבר 04Copyright Meir Kalech28 Pointers and Arrays The meaning of assigning an array to a pointer is assigning the first array address (by array name) into the pointer: int array[5]; int *parr; parr = array; // same as parr = &array[0]; Scanning the array is possible by using: Operator [] Operator * But first, which pointer operators available?

ספטמבר 04Copyright Meir Kalech29 Pointer Arithmetic Operators There are some arithmetic operations that can be performed on pointers. Add/subtract an integer to/from a pointer: int arr[10]; int *ptr1 = arr, *ptr2; ptr2 = ptr1 + 3; //ptr1+3*sizeof(int) ptr1: 100 ?????????? arr ptr2: 112 Conclusion: On adding an integer to a pointer, the new address is determined according to sizeof of the pointed type.

ספטמבר 04Copyright Meir Kalech30 Pointer Relational Operators Comparing pointers: Two pointers can be compared if they point to the same type. Any pointer may be compared to the NULL (zero) pointer. Examples: int *p1 = NULL, *p2 = NULL; if (p1 == p2) … if (p1 < p2) … if (p1 != NULL) … A good programmer verifies the validity of a pointer before using the indirection operator * on it.

ספטמבר 04Copyright Meir Kalech31 Operator [] and Operator * As seen before, we may declare: int a[100]; int *p = a; The following expressions (in each line) have the same meaning:  a,&a[0],p,&p[0] address of the 1st element  *a,a[0],*p,p[0] value of the 1st element  a+1,&a[1],p+1,&p[1] address of the 2nd element  *(a+1),a[1],*(p+1),p[1] value of the 2nd element  a+i,&a[i],p+i,&p[i] address of the i-th element  *(a+i),a[i],*(p+i),p[i] value of the i-th element

ספטמבר 04Copyright Meir Kalech32 Operator [] and Operator * So what is the difference between operator [] and operator * ??? Note: The array name isn ’ t a variable!!! int arr[100]; int *parr = arr; arr++; arr += 5; parr++; parr += 5; Error in compilation Perfectly legal

ספטמבר 04Copyright Meir Kalech33 Operator ++ Given the declarations: int k, *ptr = NULL; int Arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; ptr = Arr; What is the meaning of k = *ptr++; ? k = *ptr++ is equivalent to the following 2 statements: k = *ptr; ptr++; What is the meaning of : k = (*ptr)++; k = *(ptr++); k = *++ptr; k = ++*ptr; Try to avoid such unreadable code, even it works!

ספטמבר 04Copyright Meir Kalech34 Scanning Arrays using Pointers Scanning arrays using pointers is faster than scanning them using indices. Here are 2 functions that perform the same job. 1.Uses indices to sum the n first elements of array v: long scan_with_indices(int v[], int n) { int i = 0; // The operations long total = 0; while(i < n) // < { total += v[i]; // += *(v + i*sizeof(int)) ++i; // ++ } return total; }

ספטמבר 04Copyright Meir Kalech35 Scanning Arrays using Pointers 2.Uses pointers to sum n first elements of array v. long scan_with_pointers(int *v, int n) { long total = 0; // The operations int *save_end = v+n; while(v < save_end) // < { total += *v; // += *(indirection) ++v; // ++ } return total; } Conclusion: Each iteration of scanning with indices involves a multiplying action and an addition action that are not necessary when scanning with pointers - more efficient!

ספטמבר 04Copyright Meir Kalech36 Array of Pointers Array of pointers is an array whose elements are pointer types. Definition: int *arr[5];// array of 5 int pointers This is necessary for 2D-array handling by pointers. For instance: Assume array of strings is defined. Manipulations on the array is preferable by pointers than by array indexing itself. For example: reverse the order of the strings.

ספטמבר 04Copyright Meir Kalech37 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: 100 ? ? ? pnames: ? temp: 300

ספטמבר 04Copyright Meir Kalech38 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: ? temp: 300

ספטמבר 04Copyright Meir Kalech39 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: ? temp: 300 i j

ספטמבר 04Copyright Meir Kalech40 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: temp: 300 i j

ספטמבר 04Copyright Meir Kalech41 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: temp: 300 i j

ספטמבר 04Copyright Meir Kalech42 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: temp: 300 i j

ספטמבר 04Copyright Meir Kalech43 Array of Pointers Example void main() { char names[3][5] = {“DANA”, ”RANI”,”SHIR”}; char *pnames[3], *temp; int i, j; for(i=0; i<3; i++) pnames[i] = names[i]; for(i=0,j=3-1; i<j; i++,j--) { temp = pnames[i]; pnames[i] = pnames[j]; pnames[j] = temp; } for(i=0; i<3; i++) printf(“%s\n”, pnames[i]); } \0ANAD INAR RIHS names: pnames: Output : SHIR RANI DANA