Compiler error: invalid conversion from 'int' to 'int*' [-fpermissive]|
Solution 1
Your error is in this line:
int *pComienzo = vector, *pFinal = vector[nElementos-1];
The reason for this is that vector
is an int*
, but vector[nElementos - 1]
is a regular int
. Thus the declaration
int *pFinal = vector[nElementos - 1];
is trying to assign the integer value at the last index of vector
to the pointer pFinal
, hence the compiler error.
To fix this, you may want to do either
int *pFinal = &vector[nElementos - 1];
which makes pFinal
point to the last element of vector
, or
int *pFinal = vector + (nElementos - 1);
which accomplishes the same thing using pointer arithmetic.
That said, since you're working in C++, why not use the provided std::vector
type and avoid working with pointers altogether?
Hope this helps!
Solution 2
vector
is a pointer, but subscripting it as vector[nElementos-1]
dereferences it to simply an int. What it looks like you want is instead
int *pComienzo = vector, *pFinal = &(vector[nElementos-1]);
Related videos on Youtube
Andres29
Updated on February 07, 2020Comments
-
Andres29 about 4 years
I got a compiler error:
main.cpp|59|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
The offending line is
int *pComienzo = vector, *pFinal = vector[nElementos-1];
Why there is an error? Can someone help me?
Below is my code:
#include <iostream> #include <ctype.h> using namespace std; const unsigned short int MAX_VAL = 10; int LongitudCadena(char*); int BuscarCaracter(char *cadena, char caracter); void Ordenar(int *vector, int nElementos, bool ascendente); int main() { char *cadena = "asdasd"; cout << LongitudCadena(cadena) << endl; cout << BuscarCaracter(cadena, 'a') << endl; int iArray[] = {5,4,3,2,1}; Ordenar(iArray, 5, 1); cout << iArray << endl; return 0; } int LongitudCadena(char *cadena) { char *c = cadena; for(int i = 0; i < MAX_VAL; i++) { if (c[i] == 0) break; cadena++; } return cadena - c; } int BuscarCaracter(char * cadena, char caracter) { char *pCadena = cadena; for (int i = 0; i < MAX_VAL; i++) { pCadena++; if (toupper(cadena[i]) == toupper(caracter)) return pCadena- cadena; } return -1; } void Ordenar(int *vector, int nElementos, bool ascendente) { int *pComienzo = vector, *pFinal = vector[nElementos-1]; if (ascendente) { for (int i = 0; i < nElementos; i++) { for (; pComienzo < pFinal; pComienzo++, pFinal--) { if (*pComienzo > *pFinal) { *pComienzo += *pFinal; *pFinal -= *pComienzo; *pComienzo -= *pFinal; } } } } }
I'm learning...
-
Mr. Llama about 12 yearsI'm pretty sure it's the
*pFinal = vector[nElementos-1]
section. Remember that the although "vector" is anint*
, it's individual elements are regularint
s.
-
-
ruakh about 12 years+1, but I think it's probably better to write
vector + (nElementos - 1)
. (Note that something likevector + nElementos + 1 - 2
would be undefined behavior.vector + nElementos - 1
is technically fine, but is living on the edge!) -
Jay about 12 years@ruakh: I've moved out of C/C++ land so I don't have a compiler handy to try this, but I was under the impression that "vector + nElementos + 1 - 2" is completely legal. I thought that in general a pointer plus an arithmetic expression results in, internally, adding the integer value times the size of the object pointed to to the pointer.
-
templatetypedef about 12 years@Jay- The C++ ISO spec states that pointer arithmetic in arrays is valid as long as you stay within the array or point one step past the end of the array. Going any further technically results in Undefined Behavior, though most implementations would probably have no problem with this.
-
ruakh about 12 years@Jay: Right, but
vector + nElementos + 1
is not a valid pointer. I don't just mean that it points to unallocated memory; I mean that you can't even assume that your compiler can actually create such a pointer. (Pointers are not necessarily integers -- and even if they are, what happens ifvector + nElementos
is the maximum possible integer?) The expressionvector + nElementos + 1
, when evaluated, triggers undefined behavior, even if you never dereference that pointer, and even if you immediately subtract2
. -
Andres29 about 12 yearsthank you all!!! I'm studying c++, so i don't know about std::vector, I will take a look, again, thank you! nice comunity! edit: sorry for my english
-
Jay about 12 years@ruakh: Hmm, interesting. So I take it you're saying that "vector+(nElementos+1-2)" is defined but "(vector+nElementos+1)-2" is not. Pointer addition is not associative. Okay, there's validity to that. I strongly suspect that in practice you'd get the same results on any compiler, but I'll grant you, you shouldn't rely on "technically illegal but appears to work in practice". On the other hand, you may be making invalid assumptions about evaluation order. If I really cared I'd study the specs, I guess.