Tipos incompatíveis - é porque um array já é um ponteiro?
No código abaixo eu crio um objeto baseado na estrutura de livros, e para que ele possua vários "livros", defino é uma matriz (o objeto que é definido / iniciado, isto é). No entanto, sempre que fui testar meu conhecimento de ponteiros (a prática ajuda) e tentei fazer um ponteiro que aponta para o objeto criado, ele me dá o erro:
C: \ Users \ Justin \ Desktop \ Projeto \ wassuip \ main.cpp | 18 | erro: tipos incompatíveis na atribuição de 'livros'para' livros * [4] '| *
Posso perguntar, isso é porque o objeto book_arr [] já é considerado um ponteiro, pois é um array? Obrigado (novo para C ++ e só quero verificar).
#include <iostream>
#include <vector>
#include <sstream>
#define NUM 4
using namespace std;
struct books {
float price;
string name;
int rating;
} book_arr[NUM];
int main()
{
books *ptr[NUM];
ptr = &book_arr[NUM];
string str;
for(int i = 0; i < NUM; i++){
cout << "Enter book name: " << endl;
cin >> ptr[i]->name;
cout << "Enter book price: " << endl;
cin >> str;
stringstream(str) << ptr[i]->price;
cout << "Enter book rating: " << endl;
cin >> str;
stringstream(str) << ptr[i]->rating;
}
return 0;
}
*NOVO CÓDIGO APÓS RESPOSTAS (SEM ERROS) *
#include <iostream>
#include <vector>
#include <sstream>
#define NUM 4
using namespace std;
/* structures */
struct books {
float price;
string name;
int rating;
} book[NUM];
/* prototypes */
void printbooks(books book[NUM]);
int main()
{
string str;
books *ptr = book;
for(int i = 0; i < NUM; i++){
cout << "Enter book name: " << endl;
cin >> ptr[i].name;
cout << "Enter book price: " << endl;
cin >> str;
stringstream(str) << ptr[i].price;
cout << "Enter book rating: " << endl;
cin >> str;
stringstream(str) << ptr[i].rating;
}
return 0;
}
void printbooks(books book[NUM]){
for(int i = 0; i < NUM; i++){
cout << "Title: \t" << book[i].name << endl;
cout << "Price: \t$" << book[i].price << endl;
cout << "Racing: \t" << book[i].rating << endl;
}
}