0

In the below code I create an object based off of the books structure, and to have it hold multiple "books" I set is an array (the object that is defined/initiated, that is). However, whenever I went to test my knowledge of pointers (the practice helps) and attempted to make a pointer that points to the created object, it gives me the error:

C:\Users\Justin\Desktop\Project\wassuip\main.cpp|18|error: incompatible types in assignment of 'books' to 'books* [4]'|*

May I ask, is this because the object book_arr[] is already considered a pointer as it's an array? Thanks (new to C++ and just want to verify).

#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;
}

*NEW CODE AFTER ANSWERS (NO ERRORS) *

#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;
    }
}
X33
  • 1,310
  • 16
  • 37
  • 1
    I see you are including ``. Why don’t you use `std::vector` instead of those inferior C-arrays? –  Oct 24 '12 at 10:53
  • @WTP'-- Would that mean any substantial advantage?... – X33 Oct 24 '12 at 21:37
  • Yes, such as decent semantics and clear syntax, and you’re actually writing C++ rather than C. See [this article by DeadMG](http://codepuppy.co.uk/cpptuts/CClass/CArrays.aspx) on why C-arrays are terrible. –  Oct 24 '12 at 21:50
  • @WTP'--But this IS C++. I'm not trying to write C code. o.O – X33 Oct 24 '12 at 21:52
  • I know, so you should use `std::vector` instead of C-style arrays. –  Oct 24 '12 at 21:53
  • @WTP'-- Alright, thank you. I will do. :) – X33 Oct 24 '12 at 22:01
  • For starters, vectors don't decay to iterators; isn't that enough of an advantage? :) – fredoverflow Oct 25 '12 at 18:36

2 Answers2

15

An array is not a pointer

See How do I use arrays in C++? for details.

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
-5

That's right. For any array:

BlahClass myArray[COUNT];

&myArray[0] is equivalent to just myArray. i.e. The identifier myArray is a pointer to the first element of the array. This is why you'll hear it said, rather confusingly, that "arrays and pointers are the same thing in C++". What is really meant is that the array identifier is implicitly converted to a pointer to the first element of the array.

sleep
  • 4,855
  • 5
  • 34
  • 51
  • 2
    While it's true that you will here it said that *"arrays and pointers are the same thing in C++"*, the people who say it are completely wrong. – Benjamin Lindley Oct 24 '12 at 00:57
  • @BenjaminLindley Agreed. Updated my answer to make this clearer. – sleep Oct 24 '12 at 01:07
  • Actually "the array identifier is a pointer to the first element of the array" is also incorrect. "The array identifier _decays_ to a pointer to the first element of the array" is correct. – Seth Carnegie Oct 24 '12 at 01:07
  • 1
    Still not right. What is really meant is *"an array can be implicitly converted to a pointer to its first element"* – Benjamin Lindley Oct 24 '12 at 01:07
  • 2
    An array identifier **is not** a pointer to the first element. It may convert to one in certain situations, but that does not make it the same thing. – Jesse Good Oct 24 '12 at 01:07
  • 2
    Here's a little code snippet I made a few days ago that demonstrates some of the differences between pointers and arrays. http://ideone.com/vb6Nj – Benjamin Lindley Oct 24 '12 at 01:10
  • I don't understand the downvotes unless people aren't reading my whole response. At no point in the answer do I state that arrays and pointers are the same thing. I explicitly refer to the array *identifier*, not the array itself. The idea in my answer was to guide a beginner, starting with what they knew about pointers (addressing the actual question they had) through to an understanding of how array *identifiers* decay to a pointer to the *first element* of the array. But don't let that get in the way of a good sanctimonious trashing, including telling the OP to "Just Use STL (TM)". – sleep Oct 24 '12 at 23:49