Function arguments as pointer reference – Output writing – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-CBSE12A-2019-A01D #CPP#6953    siteicon   siteicon  

Problem Statement - Function arguments as pointer reference – Output writing

Find and write the output of the following C++ program code:

Note: Assume all required header files are already included in the program.

void Alter(char *S1, char *S2)
{
  char *T;
  T=S1;
  S1=S2;
  S2=T;
  cout<<S1<<"&"<<S2<<endl;
}
void main()
{
  charX[]="First", Y[]="Second";
  Alter(X,Y);
  cout<<X<<"*"<<Y<<endl;
}

 

Solution

TC++ #6953

 

Run Output

Second&First
First*Second

Second&First
First*Second

This is how it works:
char X[]=”First”, Y[]=”Second”; //Two character array are initialised with First and Second
Alter(X,Y);                                      //This transfers location of first member of X and first member of Y

void Alter(char *S1, char *S2)    //Here it enters function using pointer types as they are just location addresses
{
char *T;   //A char pointer type is declared
T=S1; S1=S2; S2=T; //This is typical pointer swapping technique using temporary pointer T
cout<<S1<<“&”<<S2<<endl; //Here Second&First is displayed as values are read from alternative locations due to swapping
S1 is actually being read from where S2 was there previously and vice-versa. remember content actually remains at their original
positions only. With no instruction content has been moved or copied.
}

cout<<X<<“*”<<Y<<endl; 
This is output line back in main routine. Since the actual content is at there respective positions only so This will display
First*Second   only

Common Errors

  • Here learners may find it difficult to absorb the concept as only reading of pointers have been changed and content has not been changed.


Share

CSKC| Created: 8-Apr-2019 | Updated: 22-Feb-2020|CBSE12A-2019






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back