#include<iostream>
#include<string>
usingnamespace std;
usingnamespace std;
struct data
{
string car;
string brand;
};
int main ()
{
int ncar;
cout<<"Enter the number of cars";
cin >> ncar;
data *f= new data[ncar];
size_t i;
for(i=0;i<ncar;i++)
{
cout<<"Observation "<<i+1<<": \n";
cout<<"Enter with car"<<": \n";
cin>> f[i].car;
cout<<"Enter with the brand"<<": \n";
cin>> f[i].brand;
}
data *f=&f[ncar]; // I think the problem is here because I cant using arrays with pointers...
(*f).car = newCar;
for(i=0;i<ncar;i++)
{
cout<<"Enter with correct car"<<": \n";
cin>> f[i].newcar;
}
return 0;
}
Put the code you need help with here.
Are you trying to create a new data with data *f=&f[ncar];?
A data * type is the same as data[] type when changing its values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
int * i = newint[65];
i[53]=5;
int j[65];
j[53]=5;
cout<<i[53]<<" "<< j[53]<<endl; // this prints 5 two times
delete [] i; //don't forget to free memory
return 0;
}
data *f=&f[ncar];
First, you do introduce a pointer variable with name "f". However, you already have a pointer variable with name "f". Your compiler should say something about that.
Second. f[ncar] dereferences the pointer. It is equivalent to *(f+ncar). However, f points to a memory block that has only ncar data elements. You do dereference memory one past your array. If you want your new pointer to point to the beginning of the array, you can use f directly.
int * i = newint[5];
int * j = newint[5];
//You would need to give the ints inside i and j values before doing this
for(int a = 0; a<5; a++)
{
i[a]=j[a];
}