Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Passing null terminated string to function using array notation
//using array notation
#include
#include
using namespace std;
int space_count(char []);
int main()
{
char str[80];
int spaces=0;
cout<<"Input a small sentence: " ;
cin.getline(str,80);
spaces=space_count(str);
cout<<"Number of spaces in given input = "
<
#include
#include
using namespace std;
struct Person
{
char name[40];
int age;
};
void modify(Person &p,char n[],int a=16)
{
strcpy(p.name,n);
p.age=a;
}
int main()
{
Person pers={"Lata",16};
cout<
×
Ask AI About This Code
Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.
×
Test CPP
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Lata, 16
Arun, 17
Suraj, 16
Code Understanding
struct Person { char name[40]; int age;};
A structure Person defined with char array name and age variables. Instance of this will be modified by program by reference passing.
void modify(Person &p,char n[],int a=16)
Here the structure instance p has been passed as &p means it is passed as reference. This means that original copy of structure instance would be affected. Here the char array is also passed by reference and variable a is passed by value with a default value of 16.
{ strcpy(p.name,n); p.age=a;
Original structure instance which is passed would be affected with this instruction. For name being a char array strcpy function is used and for age the value has been directly updated with an assignment.
}
Person pers={“Lata”,16};
Structure instance pers initialised with name and age.
cout<<pers.name<<“, “<<pers.age<<endl;
This prints as per initialisation
modify(pers,”Arun”,17);
This will modify the structure instance pers value as it has been passed with reference.
cout<<pers.name<<“, “<<pers.age<<endl;
Modified values shall be printed.
modify(pers,”Suraj”);
This will again modify the values of pers. Here name has been passed but age has not been passed. In this case default value of age given with function definition will come into picture.
cout<<pers.name<<“, “<<pers.age<<endl;
Modified values shall be printed again.
Notes
Since structures instances can be of very large size, thus passing them by value may not be recommended as a very large memory copying operation is required for that. SO! passing by reference as done in this example is a good technique. Although programmer needs to take additional care that the original data if required later in calculations should be saved in some other variables.
Common Errors
Student may accidentally forget to put the & while passing the structure by reference. It is advised that they should try it without giving this sign and see the difference. If you remove & sign in the function header then the output would be –
Lata, 16
Lata, 16
Lata, 16Please do try with this error and analyse.