I have a question. Is it possible to create multiple objects during run time for classes or structures?
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int no;
};
int main()
{
int i;
for(i=0;i<4;i++)
{
struct node s[i];
}
cout<<"Enter the values";
for(i=0;i<4;i++)
{
cin>>s[i].no;
}
cout<<"The values are:";
for(i=0;i<4;i++)
{
cout<<s[i].no<<endl;
}
getch();
return 0;
}
I tried the method above , but didn’t succeed . Any help would be appreciated
replace
with
the way you wrote your program will not work. You defined the node array s inside a block so it will not be visible outside of that block.
If you want to dynamically allocate the memory you have to do something like:
or if you like the c style (not recommended):
and don’t forget to free the memory.