invalid pointer conversion C++ - c++

I'm happy to post my first question here .
so i was play a little bit with pointers to understand the concept and i found this error
error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
here is the code :
#include <iostream>
using namespace std;
int main(){
int* pa,pb,pc,a,b,c;
pa = &a;
cin >> a;
cout <<"the value of a :"<<a<<endl;
cout <<"the value of pointer of a :"<<*pa<<endl;
// the problem begins when reading values of b :
pb = &b; //<== error
cin >> b;
cout << "the value of b : "<<b<<endl;
cout <<"the value of pointer of b" <<*pb<<endl;
return 0;
}
i don't know why it went successfully with variable a but failed with the same syntax in b ?
EDIT : thanks for everyone , i know this question is very simple but i've learned from you :)

The * binds to the variable name, not the type. So what you really want is:
int *pa,*pb,*pc,a,b,c;

In the declaration
int* pa,pb,pc,a,b,c;
Only pa is declared as int*. The other variables are declared as int.
You would need to declare the variables as
int *pa, *pb, *pc, a, b, c;

A common recomendation is to declare one variable per line (see for example ES.10: Declare one name (only) per declaration), because * belongs to the variables, not the type and this can be confusing. Your
int* pa,pb,pc,a,b,c;
is actually
int* pa;
int pb;
int pc;
int a;
int b;
int c;
But you wanted:
int* pa;
int* pb;
int* pc;
int a;
int b;
int c;
In other words, you get the error becaue in your code pb is an int but &b is an int*. The first assignment is ok, because pa is a pointer.
Another common recommendation is to always initialize your variables (see ES.20: Always initialize an object), so even nicer would be
int a = 0;
int b = 0;
int c = 0;
int* pa = &a;
int* pb = &b;
int* pc = &c;
And once you got it straight what type pa, pb and pc are you can use auto to get "just the right type":
auto a = 0; // 0 is an integer literal of type int
auto b = 0;
auto c = 0;
auto* pa = &a; // auto would be fine too, &a is a int*
auto* pb = &b;
auto* pc = &c;

Related

Dynamic Memory Allocation acting weird

I just found out that I can't write code like this
#include <iostream>
using namespace std;
int main()
{
int* a, b;
int N;
cin >> N;
a = new int[N];
b = new int[N];
}
But I can't understand why:
int*a, b;
is wrong, What is b's type?
To put my comment as an answer:
bs type is int, not int* as you expect. That is a good reason to not declare several variables on the same line. The correct way to do it on a single line is int *a, *b;
If you want to know more, the rules are listed here. In your example int is in the decl-specifier-seq category and *a, *b is in the init-declarator-list category.
The best is of course to initialize your variables as you declare them to avoid potential bugs:
int main() {
int N = 0;
std::cin >> N;
int* a = new int[N];
int* b = new int[N];
//dont forget to delete
delete[] a;
delete[] b;
return 0;
}
This is because of the first line:
int* a, b;
It declared "a" as a pointer variable and "b" as an integer so you can write this in two ways:
int* a, *b;
OR
int* a;
int* b;

reference & pointers confusing parts(declarations and their types)

I'm studying pointer and reference parts and having very hard time to study them.
I think I understand now some simple usage of reference and pointers in the function, but there is something I cannot totally understand.
Here are some variable declarations:
int a = 1;
float b = 2;
int* p = &a;
string s = "Hello";
vector<int*> values = {p};
What will be the types of the following expressions?
&a
&b
p
&p
&s
&(s.at(1))
values.at(0)
&(values.at(0))
&values
I have no idea what their types exactly are, but I tried it myself.
&a : pointer to int
&b : pointer float
p : pointer to int
&p : pointer to pointer to int
&s : pointer to string
&(s.at(1)) : pointer to string
values.at(0) : pointer to int
&(values.at(0)) : pointer to pointer to int
&values : pointer to pointer to int
and one more problem >
write the following variable declarations:
a) A pointer to a string
b) A reference to a float
c) An array of pointers-to-ints.
d) A pointer to a pointer to bool
e) A reference to a pointer to an int
and my answers are:
a: string* s = "Hello"
b: float& f = g;
c: int n =1;
int*x =&n;
int arr[] = {*x};
d: bool y = true;
bool* x = &y;
bool** qq = &x;
e: int a = 1;
int* x = &a;
int& z = *x;
I'm not sure about my answers. Please help these confusing parts.
First, a std::string is a complex object that contains a char array but it is definitely not a char array. And a std::vector<int> is also a complex object that contains an array of ints.
That means that for the first part some of your tries are wrong:
&(s.at(1)) : pointer to string WRONG: pointer to char
values.at(0) : pointer to int OK
&(values.at(0)) : pointer to pointer to int OK
&values : pointer to pointer to int WRONG: pointer to `vector<int>`
For the second part, as a string is not a char array, you cannot initialize a pointer to string with a litteral char array
string *s = "hello"; WRONG syntax error
You must first create a string and then create a pointer to it
string s = "hello"; OK std::string initialized from a const char *
string *ps = &s; OK pointer to std::string
c is wrong too int arr[] declares an array of int. You must write:
int *arr[] = { x }; OK array of 1 pointers initialized from x which is a pointer to int
For e, int&z = *x; declares a reference to int initialized as a ref to a. To get a ref to a pointer, you must write:
int *&z = x; OK ref to a pointer to int initialized as a ref to x
Part 1:
If the expression e has type T, the type of &e is pointer to T; T*.
Consider the types of the expressions s.at(1) and values more carefully.
Part 2:
c: int arr[] is not "array of pointers-to-ints", it's "array of int".
An array of T is T arr[]; an array of int* is int* arr[].
int n = 1;
int* arr[] = {&n};
e: int& z is not "reference to a pointer to an int", it's "reference to int".
A reference to T is T&; a reference to int* is int*&.
int a = 1;
int* x = &a;
int*& z = x;
If you need to know the exact type of an expression and if you can use Boost, it provides a great functionality.
#include <boost/type_index.hpp>
#include <iostream>
#include <string>
#include <vector>
#define TYPE(x) std::cout << "decltype(" << #x << ") == " << type_id_with_cvr<decltype(x)>().pretty_name() << std::endl
int main()
{
using boost::typeindex::type_id_with_cvr;
int a;
TYPE(a);
int * p = &a;
TYPE(p);
TYPE(&p);
std::vector<int*> values {p};
TYPE(values.at(0));
TYPE(&(values.at(0)));
TYPE(&values);
}
If you can't use Boost, you can still deliberately create a template error to force the compiler to show you the type of expressions (from a talk of Scott Meyers, a great expert of C++).
#include <iostream>
#include <string>
#include <vector>
template <typename T>
class TD;
template <typename T>
void function(T & param)
{
TD<T> templateType;
TD<decltype(param)> paramType;
}
int main()
{
int a;
function(a);
int * p = &a;
function(p);
function(&p);
std::vector<int*> values {p};
function(&values);
}
c) An array of pointers-to-ints
You created an array of int containing a single value, the value to wish the pointer refers to. As said previously, you need a int* [] array.
&(values.at(0)) : pointer to pointer to int OK
Why &(values.at(0)) is a pointer ?
Pointer is variable that contains address in memory
Pointer has its type
unsigned long ip = 0xC0A80A01; // 192.168.10.1
unsigned char *p = (unsigned char *)&ip;
printf("%d.%d.%d.%d\n", p[3], p[2], p[1], p[0]);
// Result: 192.168.10.1
I think it is only takes address of variable.
int a = 1;
int *p = &a;
&a is not a pointer
p is a pointer
Is it?
and
int &refVar = a;
refVar is reference

Invalid conversion from '`A**`' to '`void**`

#include <iostream>
using namespace std;
class A
{
public:
A(int x){ a = x;}
~A();
private:
int a;
};
int main()
{
A* a = new A(10);
void** p;
p = &a;
return 0;
}
After compiling with g++ (GCC) 4.1.2 20080704 , I am getting following error :
test.cpp: In function 'int main()':
test.cpp:17: error: invalid conversion from 'A**' to 'void**'
There's one star to much. void* can already hold any pointer, without casts:
int main()
{
A* a = new A(10);
void* p;
p = &a;
return 0;
}
This works for multiple levels, even:
int main()
{
A* a = new A(10);
A** aa = &a;
A*** aaa = &aa;
void* p = &aaa;
}
This works because A*** is a pointer to a A**.
You need to use reinterpret_cast:
It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.
In this case your expression will be &a which has the type A** and your new_type will have the type void**:
p = reinterpret_cast<void**>(&a);
Do have a care here as C++ isn't really that fond of the void type and your code is very likely going to violate Type Aliasing Rules.
In my mind a cast to void** is an indication that a template should probably be used. Without seeing your code I can't guarantee that, but, if you want to fill me in anymore in the comments perhaps I can help.
You can't just assign the value of one type (A**) to the variable of another one (void**), if it can't be converted implicitly. You need to explicitly convert value from one type to another:
class A
{
public:
A(int x){ a = x;}
~A();
private:
int a;
};
int main()
{
A* a = new A(10);
void** p;
// use `reinterpret_cast'
p = reinterpret_cast<void**>(&a);
// C-style cast works but it is a bad practice in C++
p = (void**)&a;
return 0;
}

What is the behaviour of int &a = *b?

For example, I have the following codes:
int a = 1;
int b = 2;
int *c = &a;
int &d = *c;
c = &b;
d++;
What is behaviour of line 4? If i want a reference to a pointer,is it correct to use
int *&e = c;
Is there any reason why to choose a reference to a pointer?
This declaration
int &d = *c;
declares a reference that refers the object pointed to by pointer c.
When this declaration was executed pointer c pointed to object a
int *c = &a;
So the referecne d refers object a. References may not be reassigned. They shall be initialized when they are declared.
Thus the expression in statement
d++;
increases object a.
You may declare a reference to a pointer.
This declaration
int *&e = c;
is valid.
Constant references can be bound to a temporary object. You may not take an address of a temporary object. Refrences alow to use more simple expressions.
Consider for example a simplified function swap that swaps two integers.
Using pointers the function would look like
void swap( int *a, int *b )
{
int tmp = *a;
*a = *b;
*b = tmp;
}
and it could be called like
int a = 5;
int b = 10;
swap( &a, &b );
Using references you could write the function the following way
void swap( int &a, int &b )
{
int tmp = a;
a = b;
b = tmp;
}
and its call would look simpler than the call of the function with pointers.
int a = 5;
int b = 10;
swap( a, b );
regarding the meaning of line 4
int a = 1;
int*c = &a; // okay: pointer to int, points to a
int&d = *c; // okay; reference to int, refers to *c=a;
A reference to a pointer is useful as argument to a function that may alter its value (=address pointed to), for example
void allocate(int*&p)
{ p=new int[10]; }

What is wrong with this simple code with vectors?

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