1

The following code is one that I written for myself in order to test how pointers and vectors work.

I am very new to C++.

#include <vector>
#include <iostream>

using namespace std;

int main(void)
{
   //Create the integer pointer vector, and clean it to initialize
   vector<int *> lol;
   lol.clear();

   //Create the pointers and point them to 1,2,3
   int a1=1, a2=2, a3=3;
   int* a, b, c;
   a=&a1;
   b=&a2; 
   c=&a3;

   //Put the pointers into the vector
   lol.push_back(a);
   lol.push_back(b);    
   lol.push_back(c);

   //Return the value of the middle pointer
   cout << *lol[1];
}

I get a whole wall of errors while compiling.

Can anyone help? Bear in mind I can only understand novice.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
Yellow Skies
  • 145
  • 9

6 Answers6

4

The problem is with this line:

int* a, b, c;

a is int*, but b and c are just ints.

int *a, *b, *c;

Would make it all int*s.

int* a;
int* b
int* c;

does the same thing, but with clearer intentions of declaring three int*s.

See: Placement of the asterisk in pointer declarations

UPDATE: Even better:

int* a = &a1;
int* b = &a2; 
int* c = &a3;

Whenever you can, don't separate variable initialization and its declaration.

Community
  • 1
  • 1
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
0

When declaring multiple pointers on one line, you have to specify the * symbol in front of each pointer variable.

int * a, b, c;

should be :

int *a, *b, *c;

The line :

int * a, b, c;

is interpreted as :

int *a;
int b;
int c;
0

Change this

int* a, b, c;

to

int *a, *b, *c;

In your declaration you are declaring

a as pointer to int 
b as int
c as int
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

If you declare variables, do not declare multiple variables on the same line.

What you thought you did was to declare three pointers to int. What you did, was to declare three ints, one of them a pointer:

int* a, b, c;

means

int *a; int b; int c;

Think of the * as belonging to the variable name. Unintuitive, but that's the way the language works.

You want to declare all of the three as pointers:

int* a;
int* b;
int* c;
nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

The main problem, as others already noted, are the definitions here:

int* a, b, c;

Basically, only a is an int *; b and c are just ints.

It's just better to have one variable definition per line:

int* a = &a1;
int* b = &a2;
int* c = &a3;

If you use these raw pointers, and if for some reason you want to first define them and then assign their values later, consider at least initializing them to nullptr (or NULL if you are using C++98/03):

// Initialize to NULL/nullptr, to avoid pointers pointing to junk memory
int* a = nullptr;
int* b = nullptr;
int* c = nullptr;

....

// assign proper values to pointers...

Moreover, there are also other notes that can be made for your code:

int main(void)

Since this is C++ - not C - you can omit the (void), and just use () instead:

int main()

When you create the vector:

vector<int *> lol;
lol.clear();

you don't need to call its clear() method after the vector definition: in fact, vector's default constructor (implicitly called by the compiler when you defined the vector in the first line) has already initialized the vector to be an empty vector.
This is just fine:

vector<int *> lol;    // Creates an empty vector

Considering these notes, your code can be written something like this:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<int *> lol;

   int a1 = 1;
   int a2 = 2;
   int a3 = 3;

   int * a = &a1;
   int * b = &a2; 
   int * c = &a3;

   lol.push_back(a);
   lol.push_back(b);    
   lol.push_back(c);

   cout << *lol[1] << endl;
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
0

First problem is at declaration of pointers

          int* a, b, c;

This will create a as pointer and b & c as int.

Use declaration like

          int* a,*b,*c;

And While accessing the elements of vector use .at() method of vector.

          cout << *lol.at(0) << endl;
user3013022
  • 196
  • 1
  • 3
  • 16