Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Size: px
Start display at page:

Download "Solution: A pointer is a variable that holds the address of another object (data item) rather than a value."

Transcription

1 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) where a is the base address of the array. 3. Explain the operators used to represent pointers. The address operation (&) The indirection operator (*) 4. Is arr[3] and *(a+3) the same? Yes, both are the same. 5. What is the use of operator? "Arrow operator ( ) - to refer to the structure elements. 6. Can a pointer be nested in a structure? Yes, pointers can be nested in structures. 7. Can we do the operation arr++ if arr is an array? No, it is not possible to perform incrementation operation on an array. 8. What is a call by value? If a variable is passed by value, then the changes to the corresponding data item will not be reflected in the calling program. 9. Does the array name carry the address of the first memory location? 1

2 Yes, array name carries the address of the first memory location. If a is a one dimensional array, then the address of the first element is expressed as &a[0] is simple a. 10. Write a program to explain a passing pointer as address to function. Main () Int num[] = 1,2,3,4,5p,I; For (i=0 ;I <5 ; i++) P =sum (&num[i]); Cout<<"the sum is "<<p; Getch (); Sum (int *n); Static int s=0; St = *n; Return (s); 11. What are the advantages of using the function object? Advantages in using a function object instead of pointers to functions or pointers to members. 1. Function objects are more flexible to design changes, because the object can be modified internally without changing its external interface. 2. In ordinary functions - prototypes remain intact when their implementation changes. 3. A function object may contain data members that store its state, thereby eliminating the need of external variables and complex argument lists. 2

3 4. Function objects present another benefit in regard to performance. Compilers are usually able to inline when a function call is made through a function object, thus optimizing your code. 5. It is difficult to inline - a function call made through a pointer. 12. State an example for function objects? This example demonstrates how an object can be made an argument of a function. #include <iostream.h> struct Paper double Width; ; double Height; void MagazineCover(Paper ppr) cout << "Characteristics of this piece of paper"; cout << "\nwidth = " << ppr.width; cout << "\nheight = " << ppr.height; cout << "\nperimeter = " << 2 + (ppr.width + ppr.height); cout << "\narea = " << ppr.width * ppr.height; int main(int argc, char* argv[]) double w, h; cout << "Provide the dimensions of the magazine cover\n"; cout << "Width: "; cin >> w; cout << "Height: "; 3

4 cin >> h; Paper Cover = w, h; MagazineCover(Cover); getchar(); return 0; 13. How are pointers and structures related? A pointer can point to a structure in the same way as it is used with the other variables. Thus pointers are called structure pointers. Syntax for declaring structure pointers is as follows: Struct [tag] Member 1; Member 2 ;..; Member n ; < variable list >, * ptr var ; Ptr serves as pointer variable pointing to the beginning of structure. Members of structure are accessed using the arrows operator ( ) # include < stdio.h > main ( ) struct emprec int eno; char ename[15]; 4

5 char design[10]; float basic; ; static struct emprec rec1 = 2234, "clinton", "clerk", ; struct emprec *ptr; ptr=&rec1; clrscr(); cout<<"\n from structure \n"; cout<<"emp no : \n"<<rec1.eno; cout<<"emp name : \n"<< rec1.ename; cout<<"destignation : \n"<< rec1.design; cout<<" basic pay : \n "<<rec 1. basic); cout<<"\n from structure pointer \n "; cout<<"emp no : \n "<< ptr eno); cout<<"emp name : \n "<<ptr ename); cout<<"designation :\n"<<ptr design); cout<<"basic pay: \n"<<ptr basic); getch( ); o/p : from structure. emp no : 2234 emp name : clinton designation : clerk basic pay: from structure pointer 5

6 emp no : 2234 emp name : clinton designation : clerk basic pay: In above program, * ptr is a pointer to the structure emprec. The structure pointer *ptr is assigned the base address of structure variable rec1. The output statements that are used to print values "From structure pointers" have been discussed. The output statements that are used to print value "From structure pointer " are different as they use " arrow operator ( ) to refer to the structure elements. On the left hand side of the dot operator (. ), we should always use a structure variable, where as on the left hand side of the arrow operator, we should only use a pointer to a structure. Since the values of the structure members are sorted contiguously in the memory locations, the pointer to the structure will hold the base address of the structure, which is address of the first element. With this base address, the values of other members can be accessed by incrementing value of pointer variable depending upon the data type stored at a referred location. The diagrammatic arrangement of the structure variable and the pointer to structure in memory is illustrated as follows: Rec1.no rec 1. name rec 1. design rec 1. basic 2234 CLINTON CLERK ptr In above figure, pointer to structure * ptr holds the base address of the structure, that is To move to the next members, it is enough to add the memory requirements of the current data type with a base address. As the memory requirement for an int data type is 2, compiler increments 2 bytes to the base address to move to the next location (ie = 2402). Here 2402 is the starting address in the above structure definition. A structure containing bit fields can be used within another structure as they are treated just like any other member. 14. Can we pass an entire array to a function? In this, we have to pass the base address (that is the address of the first array element) long with the size of the array to the called function. 6

7 Main () int num[]=1,2,3,4,5,p; P=sum (&num [0],5); Cout<<"the sum is"<<p; Getch (); Sum (int *n, int, count) Int i,s=0; For (i=0; i<count; i++) S+=*(n+i); Return (s); 15. Write a note on default arguments. C++ allows us to assign default values to a function s parameter which is useful in case a matching argument is not passed in the function call statement. The default values are specified at the time of function declaration. #include<iostream.h> void main( ) int sm(int a,int b,int c); int add(int a,int b=6); int m,n,l; cin>>m>>n>>l; sum(m,n); -fn call the argument can be avoided sum(m,n,l);-fn call all arguments can also be used 7

8 add(m); cout<<sum(m,b) int sum (int a,int b,int c=5) return(a+b+c); int add(int a,int b=6) return(a+b); The default arguments should appear as the last argument of the list of arguments. #include<iostream.h> inline float multi(float x,float y,float z=5.5) return(x*y*z): void main( ) void add (int &a,int &b) int m,n; cin>>m>>n; add(m,n); int l,k; cin>>l>>k; cout<<multi (l,k); 8

9 void add(int &a,int &b)] int t; t=a+b; cout<<t; 16. Explain pass by reference and call by reference. CALL BY REFERENCE:- In this case, the addresses of the actual arguments in the function call are passed to the parameters of the formal argument of the called function. i.e, changer () since the addresses are copied by the formal arguments, whatever changes that are made to the contents of these memory addresses which will be carried over to the main () function at the time of returning the control. Memory location of i and j From the figure, it is clear that the memory locations (addresses) 3284 and 3286 of the variables I and j respectively are passed to the changer () function. These addresses are copied by the pointer variables a and b respectively. These two variables now point to the integer values that are stored in the memory locations 3284 and If the swapping procedure takes place using the temporary variable temp, the values are directly returned to the memory addresses of I and j. When the control is transferred back to the calling programs the interchanged value will be retained. Pass By Reference The call by reference method uses a different mechanism. In place of passing a value to the function being called, a reference to the original variables is passed. Remember that a reference is an alias 9

10 (i.e., a different name) for a predefined variable. That is, the same variable value can be accessed by any of the two names: the original variables name and the reference name. When a function is called by reference, then, the formal parameters become references (or aliases) to the actual parameters in the calling function. This means that in the call by reference method, the called function does not create its own copy of original values; rather, it refers to the original values only by different names i.e., the references. Thus, the called function works with the original data and any change in the values gets reflected to the data. The call by reference method is useful in situations where the values of the original variables are to be changed using a function. The function change ( ) of program when called by reference will work in the desired manner. WORKING OF CALL BY REFERENCE METHOD OF A FUNCTION WORKING: #include<iostream.h> #include<conio.h> int main ( ) clrscr ( ); void change ( int &); \\ notice prototype int orig =10; \\ original value is 10 cout<< " the original value is : " <<orig << "\n"; change (orig); cout<<"value after change ( ) is over : "<<orig << "\n"; return 0; void change (int &a) a=20; cout<<"value of orig in function change ( ) is :"<< a<<"\n"; return; The above program will produce the following output. 10

11 The original value is 10 Value of orig in function change ( ) is: 20 Value after change ( ) is over : 20 In the above program change ( ) refers to the original value orig which is (10) by its reference a. As the same memory location is being referred to as orig by main () and as a by change ( ). So the charge in a (to 20) is done in the same location as that of orig s and hence, this change is reflected back to main ( ).Thus, the above output is produced. Void swap(int &a,int&b) int t=a; a=b; b=t; swap(m,n); void swap(int &a,int &b) int t=a; a=b; b=t; Memory used efficiently 17. Write a note on const pointers. Const keyword is used for declaring symbolic constants. We can also declare constant pointers or pointers to constants. A constant pointer means that the pointer in consideration will always point to the same address. Its address cannot be modified. A pointer to a constant refers to a pointer which is pointing to a symbolic constant. Using a pointer to a constant, the constant value cannot be modified; the pointer can be made to point to another location. 11

12 Int *const cpyr =&n; Const int kn =99; 18. Explain pointer arithmetic operations. INCREMENTING A POINTER A pointer holds the address of another variable. If a pointer variable is incremented by 1, then the pointer will point to the next memory location of its type, which is adjunct to the previous pointed address. #include<iostream.h> Main () Int i=4,*ipr; Float f=98.4,*fpr; Char c= a,*cpr; /*INITIALIZE*/ Ipr=&I; Fpr =&f; Cpr =&c; cout<<"\n the value of I is"<<*ipr); cout<<"\n the value of f is "<< *fpr); cout<<"the value of c is "<<cpr); cout<<"\n ACTUAL ADDRESS STORED IN POINTER"; cout<<"\n ipr points to "<< ipr); cout<<"\n fpr points to "<< fpr); cout<<"\n cpr points to"<< cpr); Ipr++; 12

13 Fpr++; Cpr++; cout<<"address STORED IN POINTER AFTER POINTER INCREMENTED"; cout<<"\n ipr points to"<< ipr; cout<<"\n fpr points to "<< fpr; cout<<"\n cpr points to "<< cpr; Getch (); DECREMENTING THE POINTER A pointer variable if incremented points to the next memory location. Likewise, if a pointer variable is decremented, it points to the previous memory location. #include<iostream.h> Main () Int i=4,*ipr; Float f=98.4,*fpr; Char c= a,*cpr; /*INITIALIZE*/ Ipr=&I; Fpr =&f; Cpr =&c; cout<<"\n the value of I is "<<*ipr; cout<<"\n the value of f is "<< *fpr; cout<<"the value of c is"<<*cpr); cout<<"\n ACTUAL ADDRESS STORED IN POINTER"; cout<<"\n ipr points to"<< ipr; 13

14 cout<<"\n fpr points to "<< fpr); cout<<"\n cpr points to"<< cpr); Ipr--; Fpr--; Cpr--; cout<<"address STORED IN POINTER AFTER POINTER INCREMENTED"; cout<<"\n ipr points to "<<ipr; cout<<"\n fpr points to "<< fpr; cout<<"\n cpr points to "<< cpr; Getch (); POINTERS ADDITION An integer value can be added to a pointer variable which points to an integer, and then the value of the pointer variable will be incremented by 14. Since the memory requirement for an integer is 2. Hence, for 7 integers a total of 14 bytes is required. Real value should not be applied while performing pointer addition. Int *ipr, i=5; (valid) Ipr = &I; (valid) Ipr =ipr + 1.5; (invalid) POINTERS SUBTRACTION Pointers subtraction can be used to shift the pointer to point to the previous location. The following operations should not be performed. If used, the following error messages will be generated. (i) Addition of a pointer with a real value - illegal use of floating point (ii) Addition of two pointers-invalid pointer addition (iii) Multiplying a pointer with a value illegal use of pointer. (iv) Dividing a pointer with a value illegal use of pointer 19. Differentiate static and dynamic memory allocation. 14

15 Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variables have a static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time. Static memory allocation is before run time, but the values of variables may be changed at run time. Static memory allocation saves running time, but can't be possible in all cases. Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time. Dynamic memory allocation is at runtime. Dynamic memory allocation stores its memory on heap, and the static memory allocation stores its data in the "data segment" of the memory. 20. How do you declare and initialize pointers? C provides the following two special pointer operators to manipulate the data item directly from the memory location. The address operation (&) The indirection operator (*) Int a=5; The above variable instructs the C compiler to reserve a memory location to hold an integer value because in the declaration we have used the type of the variable, as int. This memory location is referred by the variable name a and finally, the value 5 will be stored in the memory location. In simpler terms, we say that 5 is assigned to a. we know that it is a number and now the question is how to find the address of the memory location. The answer is very simple. We can find the address by using the address of operators (&) as illustration in the following example. Main () Int a=5; cout<<"the value of a<< a; cout<<"the address of a"<<&a); Getch (); 15

16 If we run the program, the output would be as follow: The value of a=5. The address of a =5466. The first output statement prints the value stored in the memory location referred by the variable name a whereas in the second output statement we have included & values precede the variable name a. This operation instructs the compiler to print the address of the variable name a. The address operator (&) acts on ordinary variables or single array elements associated with a unique address and not on arithmetic operators. The indirection operator is also called as the value at address operator. This operator returns the value stored at the specified address where the indirection (*) operator operates only on operands called pointer variable. POINTER VARIABLE DECLARTION POINTER VARIABLE A pointer variable like any other variable should be declared before using it where the pointer variable is declared as follows:- DATA TYPE * POINTER-VARIABLE-NAME The data type refers to the data type of the variable that the pointer will point to. Note: The pointer variable name should be preceded by an asterisk (*). Int * ptr-to-int; INITIALIZATION OF POINTER It is advisable to initialize pointer variable as soon as they are declared. Since pointer variable stores addresses, they can address any portion of the memory. Example: - int a, *ptr-to-int; The first statement instructs the compiler to reserve a space for the variable a in the memory that holds an integer value. Next, another memory space for the pointer variable ptr-to-int, that will hold the address of an integer variable, is declared. Main () int a=5; int *pr; 16

17 Clrscr (); Pr = &a; cout<<"address of a "<< &a); cout<<"address of a "<< pr); cout<<"value of pr "<< pr); cout<<"value of a = "<< a); cout<<"value of a = "<< *(&a) ; cout<<"value of a = "<< *pr); cout<<"address of pr = "<< &pr); Getch (); OUTPUT IS:- Address of a = f6c Address of a = f6c Value of pr = f6c Value of a = 5 Value of a = 5 Value of a = 5 Address of pr = f6c 17

POINTERS - 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 information

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

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 information

Homework #3 CS2255 Fall 2012

Homework #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

Kapi 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

Kapi 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 information

Data 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:

Data 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 information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 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 information

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

C++ for Java Programmers

C++ 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 information

Downloaded from

Downloaded 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 information

What is an algorithm?

What 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 information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan 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 information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

by Pearson Education, Inc. All Rights Reserved.

by 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 information

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

First of all, it is a variable, just like other variables you studied Pointers: Basics 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 address (rather than the value)

More information

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

Outline. 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 information

Functions. Introduction :

Functions. 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 information

OOP THROUGH C++(R16) int *x; float *f; char *c;

OOP 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 information

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

What 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 information

CS201- Introduction to Programming Current Quizzes

CS201- 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 information

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C Mechatronics and Microcontrollers Szilárd Aradi PhD Refresh of C About the C programming language The C programming language is developed by Dennis M Ritchie in the beginning of the 70s One of the most

More information

Exam 3 Chapters 7 & 9

Exam 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 information

Pointers. Introduction

Pointers. Introduction Pointers Spring Semester 2007 Programming and Data Structure 1 Introduction A pointer is a variable that represents the location (rather than the value) of a data item. They have a number of useful applications.

More information

DECLARAING AND INITIALIZING POINTERS

DECLARAING 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 information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. I Internal Examination Sept. 2018 Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. [I]Very short answer questions (Max 40 words). (5 * 2 = 10) 1. What is

More information

C++ Programming Chapter 7 Pointers

C++ Programming Chapter 7 Pointers C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department

More information

Pointers. Pointers. Pointers (cont) CS 217

Pointers. 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 information

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

Pointer 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 information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

More information

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

C: 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 information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

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

Pointers. 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 information

Advanced C Programming and Introduction to Data Structures

Advanced C Programming and Introduction to Data Structures FYBCA Semester II (Advanced C Programming and Introduction to Data Structures) Question Bank Multiple Choice Questions Unit-1 1. Which operator is used with a pointer to access the value of the variable

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours? Lecture 16 Functions II they re back and they re not happy Daily Puzzle If it is raining at midnight - will we have sunny weather in 72 hours? function prototypes For the sake of logical clarity, the main()

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

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

calling 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 information

Arrays, Pointers and Memory Management

Arrays, Pointers and Memory Management Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by

More information

Pointer Data Type and Pointer Variables

Pointer 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 information

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts CENG 447/547 Embedded and Real-Time Systems Review of C coding and Soft Eng Concepts Recall (C-style coding syntax) ; - Indicate the end of an expression {} - Used to delineate when a sequence of elements

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

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

Output 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 information

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Introduction to Scientific Computing and Problem Solving

Introduction 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 information

Pointers and Arrays 1

Pointers and Arrays 1 Pointers and Arrays 1 Pointers and Arrays When an array is declared, The compiler allocates sufficient amount of storage to contain all the elements of the array in contiguous memory locations The base

More information

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different. C++ Character Set a-z, A-Z, 0-9, and underscore ( _ ) C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different. Identifier and Keywords:

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Basic Source Character Set for C++ Language:

Basic Source Character Set for C++ Language: Lecture 4 1. Programming in C++ Language: C++ was created by Bjarne Stroustrup, beginning in 1979. The development and refinement of C++ was a major effort, spanning the 1980s and most of the 1990s. Finally,

More information

DELHI PUBLIC SCHOOL TAPI

DELHI PUBLIC SCHOOL TAPI Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

More information

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):

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): 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 information

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through

More information

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Downloaded 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 information

Algorithms & Data Structures

Algorithms & Data Structures GATE- 2016-17 Postal Correspondence 1 Algorithms & Data Structures Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key

More information

[0569] p 0318 garbage

[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 information

Pointers, Dynamic Data, and Reference Types

Pointers, 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 information

UNDERSTANDING THE COMPUTER S MEMORY

UNDERSTANDING THE COMPUTER S MEMORY POINTERS UNDERSTANDING THE COMPUTER S MEMORY Every computer has a primary memory. All our data and programs need to be placed in the primary memory for execution. The primary memory or RAM (Random Access

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices. Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

Pointers. 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 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 information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

At the end of this module, the student should be able to:

At the end of this module, the student should be able to: INTRODUCTION One feature of the C language which can t be found in some other languages is the ability to manipulate pointers. Simply stated, pointers are variables that store memory addresses. This is

More information

C++ 8. Constructors and Destructors

C++ 8. Constructors and Destructors 8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of

More information

CS201 Spring2009 Solved Sunday, 09 May 2010 14:57 MIDTERM EXAMINATION Spring 2009 CS201- Introduction to Programming Question No: 1 ( Marks: 1 ) - Please choose one The function of cin is To display message

More information

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ. C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,

More information

Memory Allocation in C

Memory Allocation in C Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called

More information

Chapter-13 USER DEFINED FUNCTIONS

Chapter-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 information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

Basics of Programming

Basics of Programming Unit 2 Basics of Programming Problem Analysis When we are going to develop any solution to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Chapter-14 STRUCTURES

Chapter-14 STRUCTURES Chapter-14 STRUCTURES Introduction: We have seen variables of simple data types, such as float, char, and int. Variables of such types represent one item of information: a height, an amount, a count, and

More information

COMMON QUARTERLY EXAMINATION SEPTEMBER 2018

COMMON QUARTERLY EXAMINATION SEPTEMBER 2018 i.ne COMMON QUARTERLY EXAMINATION SEPTEMBER 2018 1. a) 12 2. a) Delete 3. b) Insert column 4. d) Ruler 5. a) F2 6. b) Auto fill 7. c) Label 8. c) Master page 9. b) Navigator 10. d) Abstraction 11. d) Void

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them. 1. Why do you think C++ was not named ++C? C++ is a super set of language C. All the basic features of C are used in C++ in their original form C++ can be described as C+ some additional features. Therefore,

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36 Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar

More information

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths

More information

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

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 1 Date : 28.2.2018 Marks: 40 Subject & Code : DataStructures Using C++ (15EC661) Sem : VI A,B,C Name of faculty : Vandana M L Time : 11:30-1:00 PM Note: Answer FIVE full questions,

More information

COMPUTER APPLICATION

COMPUTER APPLICATION Total No. of Printed Pages 16 HS/XII/A.Sc.Com/CAP/14 2 0 1 4 COMPUTER APPLICATION ( Science / Arts / Commerce ) ( Theory ) Full Marks : 70 Time : 3 hours The figures in the margin indicate full marks for

More information

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7 Computer Programming & Problem Solving ( CPPS ) Chapter No 7 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan

More information

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal TOKENS C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal Tab, Vertical tab, Carriage Return. Other Characters:-

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous

More information

MYcsvtu Notes LECTURE 34. POINTERS

MYcsvtu Notes LECTURE 34.  POINTERS LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain

More information

UNIT - V STRUCTURES AND UNIONS

UNIT - V STRUCTURES AND UNIONS UNIT - V STRUCTURES AND UNIONS STRUCTURE DEFINITION A structure definition creates a format that may be used to declare structure variables. Let us use an example to illustrate the process of structure

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

LSN 3 C Concepts for OS Programming

LSN 3 C Concepts for OS Programming LSN 3 C Concepts for OS Programming ECT362 Operating Systems Department of Engineering Technology LSN 3 C Programming (Review) Numerical operations Punctuators ( (), {}) Precedence and Association Mixed

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information