|
|
- Blaise Townsend
- 6 years ago
- Views:
Transcription
1 POINTERS INTRODUCTION: A pointer is a variable that holds (or whose value is) the memory address of another variable and provides an indirect way of accessing data in memory. The main memory (RAM) of computer consists of bytes which are numbered sequentially. Each byte is numbered [i.e. from 0 to (n-1)] and this number is its address. When we declare a variable, we mention its type and its name, and the compiler along with operating system allocates a block of memory for storing the value of the variable. Generally, the address of a variable is the address (byte number) of the very first byte of the memory block allocated to the variable. This address is known as base address. While programming, a programmer doesn t knows what address is to be allocated by the compiler to a variable. However, C++ provides address-of operator (&), also called reference operator to retrieve the address of a variable. For instance, if n is an integer variable, its address (i.e. address of the first byte, also called base address) is given by &n. We may declare another variable for storing &n, which is the address of n. That variable is called pointer to n p is a pointer to n p (pointer variable) n POINTERS : DECLARATION AND INITIALIZATION A pointer variable can be declared as follows: type *ptrvarname; The operator (*) tells the compiler that the variable name on its right is a pointer and is going to store a memory address. int *iptr; // pointer to an integer char *cptr; // pointer to a character float *fptr; // pointer to a floating point number. Page 21
2 Initializing a pointer (Creating a Link): Like any other variable, a pointer variable must also be initialized before using it. Pointers may be initialized to an address or otherwise to 0 or NULL, which is a symbolic constant. NULL is defined in header file iostream.h and other C++ header files like stddef.h. It is equivalent to 0. A NULL pointer does not point to any valid data. A pointer variable can be initialized as follows: ptrvarname=&variable; iptr=&a; cptr=&ch; fptr=&f; // assuming a is declared as a variable of type int. // assuming ch is declared as a variable of type char. // assuming f is declared as a variable of type float. The pointer variable can be declared as well as initialized in a single line as follows: type *ptrvarname=&variable; int *iptr = &a; char *cptr = &ch; float *fptr = &f; Indirection or Dereference Operator: The value of a variable may be obtained from its pointer by using indirection operator also called dereference operator (*). It is called indirection because it obtains the value indirectly. int n = 5, *iptr ; iptr = &n ; *iptr = 60; // read as value at address iptr is 60; this expression makes the value of n as 60. In this way pointers may be used in place of the variables they point to NOTE: i) The pointer variables must always point to the correct type of data. Making a pointer point to incorrect type of data may lead to loss of information. float f; int *ptr; cin>>f; ptr=&f; // NOTE pointer ptr is of type int and is initializing it with the address of a float point value. ii) A pointer variable must not remain uninitialized since uninitialized pointers cause the system crash. To avoid this problem, initialize such pointers with NULL. Page 22
3 iii) NULL Pointer: A NULL Pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid memory address. int *p=null; (OR) int *p=0; iv) In pointer arithmetic, all pointers increase or decrease by the length of the data-type they point to. int a; // Suppose address of a is 1020 int *iptr=&a; iptr++; // iptr now points to 1022 instead of 1021 iptr+=3; // iptr now points to 1028 instead of 1025 iptr iptr+1 iptr+2 iptr+3 iptr Fig: To illustrate all pointer arithmetic is relative to its base type. v) The pointers to two or more variables of same type may be declared in the same line. int m, n, *m, *n; m=&m; n=&n; vi) A pointer variable itself is allocated 4-bytes of memory space (on 32-bit systems) to store the memory address irrespective of the fact whether the variable to which it points to is integer, double or character. The following program illustrates this: int main() int n = 6,*iptr; double PI = 3.14,*fptr; fptr = &PI; iptr = &n; char ch = 'M',*cptr; cptr = &ch; // initializing by &ch cout<< "Size of the pointer to int n = " <<sizeof(iptr)<<endl; cout <<"Size of the pointer to double PI = "<<sizeof(fptr)<<endl; cout<< "Size of the pointer to char ch = "<<sizeof(cptr) <<endl; cout<<"variables are "<<*iptr <<", "<<*fptr<<" and "<<*cptr<< endl; return 0; Page 23
4 OUTPUT: Size of the pointer to int n = 4 Size of the pointer to double PI = 4 Size of the pointer to char ch = 4 Variables are 6, 3.14 and M vii) POINTER TO POINTER: Just like we have a pointer to a variable, we can also define a pointer to a pointer which keeps the address of the pointer to the variable. int*iptr = &n ; int** pptr = & iptr ; // iptr is pointer to n // pptr is pointer to iptr Note that both for pointer and pointer to pointer, the type is int because n is integer. Also note that for getting the value of variable, we use one dereference operator (*) to pointer while for getting the value of variable from pointer to pointer, we have to use two dereference operators (**). This situation is called multiple indirection. But there is no change in the application of address-of operator (&). viii) CONSTANT POINTERS: The attribute modifier const may be used with pointers as well. However, the declaration should be done carefully keeping in view the following: const int* ptr ; int* const ptr ; const int* const ptr ; // Here ptr is a non-constant pointer to a constant int. // Here ptr is a constant pointer to a non-constant int. // Here ptr is a constant pointer to a constant int POINTER TO ARRAY: [1-D array] Arrays and Pointers are very closely linked in C++ and may be used almost interchangeably. C++ treats the name of an array as a constant pointer that always point to the first element of the array; and the pointer to array also carries the address of the first element of the array. Thus the pointer to an array has same value as the name of the array. The declaration of a pointer to an array is illustrated below: int A[] = 12, 25, 36,50; // A is the name of array int *ptra ; // pointer ptra of type int declared ptra = A ; // assigns address of array A to ptra; Notice there is no & operator before A. The above definition may also be written as below: int *ptra = A; This is equivalent to taking the address of the first element of the array as follows: int *ptra = A[0]; // It is so because the address of A[0] is same as of A. Page 24
5 C++ provides two methods of accessing array elements: array indexing and pointer arithmetic. The elements of the above array A[0], A[1], A[2], and A[3] have respective values as 12, 25, 36 and 50 which may also be obtained from pointers as illustrated below: Pointer *ptra *(ptra+1) *(ptra+2) *(ptra+3) arithmetic Array indexing A[0] A[1] A[2] A[3] The above discussion shows that array subscript and pointer offset are equal. If we call array A without a subscript, it will give the address of first element of array. A +1 gives the address of second element, A+2 gives address of third elements and so on. Therefore, we can also get the values stored at these addresses by using dereference operator (*). Thus for the above declared array we may get the values of array elements as given below: *A = A[0] = 12 *(A+1) = A[1]= 25 *(A+2 )= A[2]= 36 *(A+3) = A[3]= 50 Note that expression *A++ is not legal because A is a constant pointer to array and a constant cannot be incremented/decremented. Program to Illustrate declaration of pointer to an array: int main() int A [] = 12, 25, 36,50; int *ptra = A; // A is an array of integers //ptra is pointer to A. Note that & is not used. cout<<"*ptra ="<<*ptra <<"\t ptra ="<<ptra<<endl; cout<<"*(ptra+1)="<<*(ptra+1)<<"\t ptra+1="<<(ptra+1)<<endl; cout<<"*(ptra+2)="<<*(ptra+2)<<"\t ptra+2="<<(ptra+2)<<endl; cout<<"*(ptra+3)="<<*(ptra+3)<<"\t ptra+3="<< (ptra+3)<<endl<<endl; cout<<"a = "<< A<<endl; cout<<ptra[0]<< \t <<ptra[1]<< \t <<ptra[2]<< \t <<ptra[3]<<endl; cout <<*(A+0)<< \t <<*(A+1)<< \t <<*(A+2)<< \t <<*(A+3)<<endl; // These will display the values of array elements. return 0; Page 25
6 The expected OUTPUT is as below: *ptra =12 ptra =0x225f2450 *(ptra+1)=25 ptra+1=0x225f2452 *(ptra+2)=36 ptra+2=0x225f2454 *(ptra+3)=50 ptra+3=0x225f2456 A = 0x225f ARRAY OF POINTERS: Similar to other variables, we can create an array of pointers in C++. The pointers may be arrayed by declaring a pointer array of specific type and then assigning each element of the pointer array by the addresses. type *pointerarrayname[size]; Ex. An array holding 6 integer pointers can be declared as: int *p[6]; // declares an array P that can hold 6 int pointers After this declaration, contiguous memory would be allocated for 6 pointers that can point to integers. To assign the address of an integer variable called var to the third element of the pointer array, we may write: p[2] = &var; To get the value of that var, we can write : *p[2] The following program illustrates this: int main() int *p[6]; int a=11,b=22,c=33,d=44,e=55,f=66; p[0]=&a; p[1]=&b; p[2]=&c; p[3]=&d; p[4]=&e; p[5]=&f; cout<< "THE VALUES ARE:\n"; for(int i=0;i<6;i++) cout<<*p[i]<<"\t"; cout<< \nthe size of p is: <<sizeof(p); cout<< \nthe size of (p[0]) is: <<sizeof(p[0]); cout<< \nthe size of (p[1]) is: <<sizeof(p[1]); Page 26
7 cout<< \nthe size of (*p[0]) is: <<sizeof(*p[0]); cout<< \nthe size of (*p[1]) is: <<sizeof(*p[2]); return 0; The expected OUTPUT is as below: THE VALUES ARE: The size of p is: 24 The size of (p[0]) is: 4 The size of (p[1]) is: 4 The size of (*p[0]) is: 2 The size of (*p[1]) is: 2 POINTERS AND STRINGS: [storing several strings in the memory] We know that a string is a one-dimensional array of characters, which start with the index 0 and ends with the null character \0. C++ also allows us to handle strings with pointers, simply called string array. Each entry in the array is a string, but in C++ a string is essentially a pointer to its first character, so each entry in an array of strings is simply a pointer to the first character of a string. Consider the declaration of string array pstr that might be useful in representing different subjects: char *pstr[ 4 ] = "PHY", "CHEM", "MATHS", "BIO" ; The pstr[4] portion of the declaration indicates an array of four elements. The char * portion of the declaration indicates that each element of array pstr is of type "pointer to char. Although it appears that these strings are being placed in the pstr array, only pointers are actually stored in the array, as shown in figure below. Each pointer points to the first character of its corresponding string. Thus, even though the pstr array is fixed in size, it provides access to character strings of any length. This flexibility is one example of C++'s powerful data-structuring capabilities. pstr[0] pstr[1] P 1020 C 2020 H 1021 H 2021 Y 1022 E 2022 \ M 2023 \ pstr[2] pstr[3] M 3020 B 4020 A 3021 I 4021 T 3022 O 4022 H 3023 \ S 3024 \ pstr[ ] array Page 27
8 Program to Illustrate string pointer pointing to several strings: void main() char *pstr[]= "PHY", "CHEM", "MATHS", "BIO" ; for(int i=0;i<4;i++) cout<<pstr[i]<<"\t"; cout<<&(pstr[i])<<"\t"; cout<<"size of (pstr[i]): "<<sizeof(pstr[i])<<endl; cout<<*pstr[i]<<"\t"; cout<<&(*pstr[i])<<"\t\t"; cout<<"size of (*pstr[i]): "<<sizeof(*pstr[i])<<"\n\n"; The expected OUTPUT is as below: PHY 0x24cf241c size of (pstr[i]) : 4 P PHY size of (*pstr[i]): 1 CHEM 0x24cf2420 size of (pstr[i]) : 4 C CHEM size of (*pstr[i]): 1 MATHS 0x24cf2424 size of (pstr[i]) : 4 M MATHS size of (*pstr[i]): 1 BIO 0x24cf2428 size of (pstr[i]) : 4 B BIO size of (*pstr[i]): 1 Note: we cannot perform pointer arithmetic in the above program as array elements (of the array pstr) are not stored in contiguous memory locations POINTERS AND FUNCTIONS: We know that a function is a user defined operation that can be invoked by passing the values of arguments (call by value method) or references to arguments (call by reference method). The call by reference method can itself be used in two ways: by passing references by passing the pointers Page 28
9 Invoking functions by passing the pointers: When the pointers are passed to the function, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. i.e. in the function-call statement, we have to pass addresses of actual arguments. Thus, the called function doesn t create its own copy of original values, rather, it refers to the original values by the addresses it receives in corresponding pointers. The declaration of a function (Function Prototype) that is invoked by passing pointer looks like: return-type functionname( type*, type*,... ); Ex: Program to swap values of two variables by passing pointers void main() void swap(int*, int*); int a=10,b=20; cout<<"original values are:"<<endl; cout<<"a="<<a<<"\t b="<<b; swap(&a,&b); // prototype // function call cout<<"\nvalues after swapping are:"<<endl; cout<<"a="<<a<<"\t b="<<b; Here, pointers x and y are are initialized with &a and &b resp. i.e. *x=&a, *y=&b void swap(int *x, int *y) int temp; temp=*x; *x=*y; *y=temp; // function definition The OUTPUT of the program is as below: original values are: a=10 b=20 values after swapping are: a=20 b=10 Page 29
10 Functions returning pointers: Like any other data-type, a function may also return a pointer. The general form of prototype of a function returning a pointer would be type *functionname(argument-list); where type specifies the pointer type being returned by the function specified by functionname Program to illustrate a function returning a pointer: void main() int *bigger(int*, int*); // prototype int a,b,*big; cout<<"enter two integers:"<<endl; cin>>a>>b; big= bigger(&a, &b); // function call cout<<"\nthe bigger value is:"<<*big; int *bigger(int *x, int *y) if(*x>*y) return(x); else return(y); // note that return(x) is same as return(&x) The OUTPUT of the program is as below: Enter two integers: 4 9 The bigger value is:9 Page 30
11 POINTERS TO STRUCTURES: Just like pointers to any other data-type, C++ also allows pointers to structures. The pointers to structures are also known as structure pointers. The general form of declaration of a structure pointer is as follows: structname *structpointer; where structname is the name of an already defined structure and structpointer is the pointer to this structure. struct stu int rollno; char name[20]; float marks; ; void main() stu s1=1, "mohan", 76,*pstu; // declaration pstu=&s1; // initialization cout<<"the details of S1 are:"<<endl; cout<<pstu->rollno<<"\t"<<pstu->name<<"\t"<<pstu->marks; The OUTPUT of the above program is: The details of S1 are: 1 mohan 76 Note that the members of structures are accessed either by using dot operator (.) and using the arrow operator (->). Accessing members of structures using dot operator (.) using the arrow operator (->) in case of structure pointers s1.rollno; s1.name; s1.marks; pstu-> rollno; pstu->name; pstu->marks; Page 31
12 Self-Referential Structures: A structure having a member element that refers to the structure itself is known as selfreferential structure. For Example: struct stu int rollno; char name[20]; float marks; stu *next; ; // *next is referring to the structure stu itself In the above example, * next is referring to the structure stu itself. It is useful in defining linked lists wherein each element points to the next element of same type POINTERS AND OBJECTS: C++ also allows us to have pointers to objects. The pointers pointing to objects are referred to as object pointers. The general form of declaration of an object pointer is as follows: classname *objectptr; where classname is the name of an already defined class and objectptr is the pointer to an object of this class type. student s1; // s1 is an object of class student s1 *optr; // optr is the object pointer. Accessing public members of a class By using dot operator (.) in case by using the arrow operator (->) while accessing the class members in case while accessing the class using an object members using an object pointer. s1.getdata(); s1.display(); optr-> getdata(); optr -> display(); Page 32
13 Consider the program : #include<string.h> class stu int rollno; char name[20]; float marks; public: void getdata(int r, char n[], float m) rollno=r; strcpy(name,n); marks=m; void display() cout<<rollno<<"\t"<<name<<"\t"<<marks; ; void main() stu s1; stu *optr=&s1; // object pointer declared & initialized optr->getdata(1, "rohan", 76); cout<<"the details of object S1 are:"<<endl; optr->display(); The OUTPUT of the above program is: The details of object S1 are: 1 rohan 76 Page 33
14 this POINTER The this pointer is an object pointer which points to the currently calling object. It stores the address of the object that is invoking a member-function. The this pointer is, by default, available to each called member-function. Following example illustrates this: Consider the program : #include<string.h> class stu char name[20]; public: stu(char *n) Note that this can be used only within a member function and it stores the address of the calling object strcpy(name,n); void display() cout<<this->name<<endl; // here, cout<<this->name has the same effect as cout<<name ; void main() stu s1("sohan"), s2("rohan"), s3("mohan"); s1.display(); s2.display(); s3.display(); The OUTPUT of the above program is: sohan rohan mohan The this pointer is useful in returning the object (address) of which the function is a member Page 34
Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:
Chapter-11 POINTERS Introduction: Pointers are a powerful concept in C++ and have the following advantages. i. It is possible to write efficient programs. ii. Memory is utilized properly. iii. Dynamically
More informationKapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8
Chapter 8 Introduction C++ Memory Map Free Stores Declaration and Initialization of pointers Dynamic allocation operators Pointers and Arrays Pointers and Const Pointers and Function Pointer and Structures
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationFunctions. Introduction :
Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationPointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a
3.1. Getting the Address of a Variable Pointers (read Chapter 9. Starting Out with C++: From Control Structures through Objects, Tony Gaddis.) Le Thanh Huong School of Information and Communication Technology
More informationDarshan Institute of Engineering & Technology for Diploma Studies
1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value
More informationMEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES
[1] ~~~~~~~~~~~~~~~~~ POINTER A pointers is a variable that holds a memory address, usually the location of another variable in memory. IMPORTANT FEATURES OF POINTERS (1) provide the means through which
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationDECLARAING AND INITIALIZING POINTERS
DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,
More informationPointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables Pointer variable: content is a memory address There is no name associated with the pointer data type in C++. Declaring Pointer Variables Syntax: Data-type *identifier
More informationLecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings
Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in
More informationChapter 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.
Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationPointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.
Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value
More information9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable
CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int
More informationSyntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:
STRUCTURE IN C++ 1 A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your current program using which items of different
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
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
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More information[0569] p 0318 garbage
A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the
More informationC++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I
C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory
More informationUnit IV & V Previous Papers 1 mark Answers
1 What is pointer to structure? Pointer to structure: Unit IV & V Previous Papers 1 mark Answers The beginning address of a structure can be accessed through the use of the address (&) operator If a variable
More informationPointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017
Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36 1 Pointer Arithmetic
More informationPointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013
Pointers Lecture 2 Sections 10.3-10.8 Robb T. Koether Hampden-Sydney College Fri, Jan 18, 2013 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 1 / 35 1 Introduction 2 Pointer Arithmetic
More informationWhat is an algorithm?
Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address
More informationMODULE 3: Arrays, Functions and Strings
MODULE 3: Arrays, Functions and Strings Contents covered in this module I. Using an Array II. Functions in C III. Argument Passing IV. Functions and Program Structure, locations of functions V. Function
More informationScott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes
Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationPointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014
Pointers Lecture 2 Sections 10.3-10.8 Robb T. Koether Hampden-Sydney College Mon, Jan 20, 2014 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 1 / 35 1 Endianness 2 Pointer Arithmetic
More informationCSC 211 Intermediate Programming. Arrays & Pointers
CSC 211 Intermediate Programming Arrays & Pointers 1 Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement.
More informationPOINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:
POINTERS Pointer is a memory variable which can store address of an object of specified data type For example: #include int x=5; int *a;//here a is a pointer to int which can store address of
More informationLecture 8: Pointer Arithmetic (review) Endianness Functions and pointers
CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationFORM 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):
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): 1. If a function has default arguments, they can be located anywhere
More informationChapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationChapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationSolution: A pointer is a variable that holds the address of another object (data item) rather than a value.
1. What is a pointer? A pointer is a variable that holds the address of another object (data item) rather than a value. 2. What is base address? The address of the nth element can be represented as (a+n-1)
More informationPointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays
EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and
More informationName MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs
More informationCS 161 Exam II Winter 2018 FORM 1
CS 161 Exam II Winter 2018 FORM 1 Please put your name and form number on the scantron. True (A)/False (B) (28 pts, 2 pts each) 1. The following array declaration is legal double scores[]={0.1,0.2,0.3;
More informationOOP THROUGH C++(R16) int *x; float *f; char *c;
What is pointer and how to declare it? Write the features of pointers? A pointer is a memory variable that stores the address of another variable. Pointer can have any name that is legal for other variables,
More informationDownloaded from
Unit I Chapter -1 PROGRAMMING IN C++ Review: C++ covered in C++ Q1. What are the limitations of Procedural Programming? Ans. Limitation of Procedural Programming Paradigm 1. Emphasis on algorithm rather
More informationWhat is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.
CHAPTER 08 POINTERS What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++ s most useful and powerful features. How Pointers
More informationPointers. Memory. void foo() { }//return
Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable
More informationPointers as Arguments
Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)
More informationC Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations
55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures
More informationPointers. Variable Declaration. Chapter 10
Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the
More informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationCS31 Discussion 1E Spring 17 : week 08
CS31 Discussion 1E Spring 17 : week 08 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Project 5 - Map cipher to crib Approach 1: For each pair of positions, check two letters in cipher
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationCS31 Discussion. Jie(Jay) Wang Week8 Nov.18
CS31 Discussion Jie(Jay) Wang Week8 Nov.18 Outline Pointer Struct Memory Management When the program gets executed, it gets some amount of memory allocated for use. memory Program 1 Program 2 Memory Management
More informationWhat have we learned about when we learned about function parameters? 1-1
What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? Call-by-Value also known as scalars (eg. int, double, char,
More informationCS2255 HOMEWORK #1 Fall 2012
CS55 HOMEWORK #1 Fall 01 1.What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; a. 10 b. 7 c. The
More informationUEE1302 (1102) F10 Introduction to Computers and Programming (I)
Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data
More informationby Pearson Education, Inc. All Rights Reserved.
Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]
More informationData type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:
Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before
More informationa data type is Types
Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental
More informationPointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015
Pointers Lecture 1 Sections 10.1-10.2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 1 / 23 1 Pointers 2 Pointer Initialization
More informationDynamic Allocation of Memory
Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More informationEM108 Software Development for Engineers
EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and
More informationREFERENCES, POINTERS AND STRUCTS
REFERENCES, POINTERS AND STRUCTS Problem Solving with Computers-I https://ucsb-cs16-sp17.github.io/ Pointer assignment 2 int *p1, *p2, x; p1 = &x; p2 = p1; Q: Which of the following pointer diagrams best
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationFORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. The declaration below declares three pointer variables of type pointer to double that is
More informationScheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C
Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. DECLARING POINTERS POINTERS A pointer represents both
More informationBRAIN INTERNATIONAL SCHOOL. Term-I Class XI Sub: Computer Science Revision Worksheet
BRAIN INTERNATIONAL SCHOOL Term-I Class XI 2018-19 Sub: Computer Science Revision Worksheet Chapter-1. Computer Overview 1. Which electronic device invention brought revolution in earlier computers? 2.
More informationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation ALGORITHMS & DATA STRUCTURES 9 TH SEPTEMBER 2014 Last week Introduction This is not a course about programming: It s is about puzzling. well.. Donald Knuth Science
More informationPointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as
Pointers and Strings Chapters 10, 12 2/5/07 CS250 Introduction to Computer Science II 1 3.1 Pointers and Arrays (10.3) An array of ints can be declared as o int numbers[] = 1, 2, 3, 4, 5; numbers is also
More informationDownloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from
Downloaded S. from Kiran, www.studiestoday.com PGT (CS) KV, STRUCTURES WHAT IS A STRUCTURE? Structure is a collection of logically related data. It is also a collection of dissimilar datatype. Downloaded
More informationParameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265
Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =
More informationWeek 3: Pointers (Part 2)
Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationCA31-1K DIS. Pointers. TA: You Lu
CA31-1K DIS Pointers TA: You Lu Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a
More informationCHAPTER 4 FUNCTIONS. 4.1 Introduction
CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main
More informationChapter 7 C Pointers
Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationCS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C
CS2351 Data Structures Lecture 7: A Brief Review of Pointers in C 1 About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of
More informationEL2310 Scientific Programming
Lecture 11: Structures and Memory (yaseminb@kth.se) Overview Overview Lecture 11: Structures and Memory Structures Continued Memory Allocation Lecture 11: Structures and Memory Structures Continued Memory
More informationCSC 211 Intermediate Programming. Pointers
CSC 211 Intermediate Programming Pointers 1 Purpose Pointers enable programs to simulate call-by-reference; to create and manipulate dynamic data structures, i.e. data structures that can grow and shrink,
More informationPointers. Pointers. Pointers (cont) CS 217
Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,
More informationChapter 11: Pointers
Chapter 11: Pointers Christian Jacob 1 Pointer Basics 3 1.1 What Are Pointers? 3 1.2 Pointer Operators 4 1.3 Pointer Expressions 14 2 Pointers and Arrays 19 2.1 Pointer Arithmetic And Array Indexing 19
More informationCS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018
CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make
More informationCOMPUTER SCIENCE (083)
Roll No. Code : 112012-083 Please check that this question paper contains 7 questions and 8 printed pages. CLASS-XI COMPUTER SCIENCE (083) Time Allowed : 3 Hrs. Maximum Marks : 70 General Instructions
More informationChapter 11: Structured Data
Chapter 11: Structured Data 11.1 Abstract Data Types (ADT's) Abstract Data Types A data type that specifies values that can be stored attributes operations that can be done on the values behaviors User
More informationC Pointers. 6th April 2017 Giulio Picierro
C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword
More informationPOINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES
1 POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES 2.1 POINTERS Pointer is a variable that holds the address of another variable. Pointers are used for the indirect manipulation of the variable.
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationCSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic
CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to
More informationPOINTER & REFERENCE VARIABLES
Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationPointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor
Pointer in C Presented By: Pushpendra K. Rajput Assistant Professor 1 Introduction The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and
More informationChapter-13 USER DEFINED FUNCTIONS
Chapter-13 USER DEFINED FUNCTIONS Definition: User-defined function is a function defined by the user to solve his/her problem. Such a function can be called (or invoked) from anywhere and any number of
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationC++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.
Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack
More informationTo declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows
Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful
More information