1.

Read the structure definition given below and answer the following questions:struct sample{int num;char *str;} *sptr(a) Write C++ statements to dynamically allocate a location for sample type data and store its address in sptr. (b) Write C++ statements to input data into the location pointed to by sptr. (c) Modify this structure into a self-referential structure.

Answer»

(a) sptr=new sample;

(b) cin>>sptr->num;

cin>>sptr->str;

Example

#include<iostream>

using namespace std;

struct sample

{

int num;

char *str;

}*sptr;

int main()

{

sptr = new  sample;

cout <<"Enter value for num:";

cin>>sptr->num;

cout<<"Enter value for str:";

cin>>sptr->str;

cout<<sptr->num<<sptr->str;

}

(c) struct sample

{

int num;

char *str;

sample *str;

};



Discussion

No Comment Found