C++ - invalid conversion from 'int' to 'int**' - c++

I have this following code:
#include<iostream>
using namespace std;
void insert(int *a[], const int location, const int numofelements, const int value) {
int n, b[10];
a = *b;
for (int n = numofelements; n > location; n--) {
a[n-1] = a[n];
}
b[location] = value;
*b = a;
}
int main() {
int test[10] = [1,2,3,4,5];
insert(test, 2, 5, 7);
for (int i = 0; i < 6; i++) {
cout << "test[" << i << "] = " << test[i];
}
return 0;
}
And the compiler says:
[Error] invalid conversion from 'int' to 'int**' [-fpermissive]
[Error] invalid conversion from 'int**' to 'int' [-fpermissive]
What does it mean? Thanks

Errors like this mean you are mixing integers and pointers to integers.
There is several cases in your code where that happens.
Lets look at pointers in this example
//defining
int a; //int, a is an integer holding the value
int *b; //*int, a pointer to an integer
int c[10]; //*int, arrays are to pointers to the first element with some different rules
int **d; //**int, this is a pointer to a pointer
int *d[10];//**int, this is a pointer to a an array, and arrays similar to pointers
//using pointers
a = *b; //* before a pointer returns its value
a = b[0]; //resolving the position of an array also returns its value
b = &a; //& before a value returns the address
Lets take a look at your code
#include<iostream>
using namespace std;
void insert(int *a[], const int location, const int numofelements, const int value) {
int n, b[10];
a = *b; //a is **int. *b means the first element so *b is an int.
for (int n = numofelements; n > location; n--) {
a[n-1] = a[n];
}
b[location] = value;
*b = a; //a is **int. *b means the first element so *b is an int.
}
int main() {
int test[10] = [1,2,3,4,5];
insert(test, 2, 5, 7); //test is a *int, the function expects a **int
for (int i = 0; i < 6; i++) {
cout << "test[" << i << "] = " << test[i];
}
return 0;
}
Here is a tutorial on using pointers

Related

Function which copies an array of integers from one array to another using pointers in C++

The purpose of the Copy function in the following code is to copy an array of integers from one array to another using C++ but the output seems wrong.
What could be the problem here?
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
This is the output:
ptr2 is one past the end of the array when your print loop runs.
Try this:
void Copy(int old_array[], int new_array[], int length)
{
int* ptr1 = old_array;
int* ptr2 = new_array;
int i = 0;
for (int i = 0; i < length; i++)
{
*(ptr2++) = *(ptr1++);
}
ptr2 = new_array;
for (int i = 0; i < 2; i++)
{
cout << *(ptr2 + i) << endl;
}
}
Your ptr2 is pointing to the element b[2] (which is out-of-bound access) at the time you are printing it in the second for loop.
You can fix it by subtracting the length from the ptr2 in the second for loop like below.
#include <iostream>
using namespace std;
void Copy(int old_array[],int new_array[],int length)
{
int *ptr1 = old_array;
int *ptr2 = new_array;
int i = 0;
for(int i=0 ; i<length ; i++)
{
*(ptr2++) = *(ptr1++);
}
for (int i = 0; i <2; i++)
{
cout<<*(ptr2 + i - length)<<endl;
}
}
int main()
{
int a[2]={0,1};
int b[2];
Copy(a,b,2);
}
The copy seems fine but the second for is accessing ptr2 which was incremented in the first for and is point to some invalid memory position. You could use new_array in this second loop.
I suppose this second loop is only for debug and will be better located in the main using, in you case, the variable b.

pass address of arr[0][2] , that must be received in a double pointer

need a better approach to pass address arr[0][2], given that is has to be received in a double pointer.
want to pass arr[0][2] without storing in any other variable.
#include <iostream>
using namespace std;
int help(int **arr)
{
cout<<**arr;
}
int main()
{
{
int n=3,m=3,k=0;
int **arr = new int*[n];
for(int i = 0; i < n; i++) {
arr[i] = new int[m];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
arr[i][j]=k;
k++;
}
}
int *g=*arr+2;
int **h=&g;
help(h);
}
}
There is no better way. Unfortunately C++ syntax x[y] can be used to mean two very different operations: if x is an array then is indexing, if x is a pointer they it's indirection and indexing.
If a caller expects a pointer to a pointer and you've a bidimensional matrix there's nothing you can do except actually creating the pointer that is not present in the matrix and pass its address.
The fact that with an array of pointers, with a pointer to a pointer and with a 2d array the syntax to reach an element is x[y][z] is irrelevant... they are three very different operations.
Why not just write
int *p = &arr[0][2];
help( &p );
If you want to get an access to the whole array using a pointer of the type int ** then you can use the following approach.
#include <iostream>
void help(int **arr, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
std::cout << ( *arr )[i] << ' ';
}
std::cout << '\n';
}
int main()
{
int arr[2][3]={{1,2,3},{4,5,6}};
int *p = reinterpret_cast<int *>( arr );
help( &p, 6 );
return 0;
}
The program output is
1 2 3 4 5 6

Copying one array to another using pointers

I have to use pointers to copy values of one array to another. The problem is I'm not allowed to use'[ ]' operators, which makes this more difficult for me. Here is my attempt:
#include <iostream>
using namespace std;
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
int *x = p2;
for(int i=0 ; i<length ; i++){
p2 = x;
p2 = p2 + i;
p2 = p1 + i;
}
for(int i=0; i<5; ++i){
cout << p2[i] << endl;
}
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
}
An easier way to do it would be to put p2[i] = p1[i] in the loop, but I cant do that. Any help is appreciated.
The standard way of implementing your function is as follow:
for(int i = 0; i < length; ++i)
*new_array++ = *old_array++;
To be a bit more explicit, it's the same as:
void cpyia(int old_array[],int new_array[],int length){
int *p1 = old_array;
int *p2 = new_array;
for(int i=0 ; i<length ; i++){
*(p2+i) = *(p1+i);
// another way: *(p2++) = *(p1++);
}
}
In real code, you would use std::copy before even thinking about rewriting such a simple thing yourself.
Here is a complete example:
#include <iostream>
#include <algorithm>
void cpyia(int old_array[],int new_array[],int length){
std::copy(old_array, old_array + length, new_array);
}
int main() {
int a[5]={1,2,3,4,5};
int b[5];
cpyia(a, b, 5);
// test results:
for (int index = 0; index < 5; ++index)
{
std::cout << a[index] << " <-> " << b[index] << "\n";
}
}
However, your question says that you are "not allowed to use" something, which sounds a lot like a homework assignment. In that case, you could look at possible implementations of std::copy to get an idea of how to do it. Here is one way:
void cpyia(int old_array[],int new_array[],int length){
int* first = old_array;
int* last = old_array + length;
int* d_first = new_array;
while (first != last) {
*d_first++ = *first++;
}
}
#include<iostream>
using namespace std;
int main() {
const int size = 5;
int arr1[size] = { 4,21,43,9,77 };
int arr2[size];
int *ptr_a = arr1;
int *ptr_b = arr2;
for (int i = 0; i < size; i++)
{
*(ptr_b + i) = *(ptr_a + i);
cout << *(ptr_b + i) << " ";
}
}

comparison between pointer and int C++ ellipse

#include <iostream>
using namespace std;
char problem5(char alc[], char a, int *n);
int main() {
char aloc1[]={ 'g','g','c','g','a','g','g','g','t','g'};
int size=sizeof(aloc1)/sizeof(aloc1[0]);
cout << aloc1 << endl;
int nalc = problem5(aloc1, 'g' ,&size);
cout << nalc << endl;
return 0;
}
char problem5(char alc[], char c, int *n){
int a = 0;
for(int i = 0; i < n; i++){
if(alc[0]!=c){
a++;
}
}
int nalc[a];
int b=0;
for(int j = 0; j < n; j++){
if(alc[0]!=c){
nalc[b]=alc[j];
b++;
}
}
*n=&a;
return nalc;
}
why do I keep having errors at the two for loops of the problem5?
it says something like comparison between pointer and int.
how can i fix that while the argument n remains pointer.
Use *n to access the value pointed by n:
for(int i = 0; i < *n; i++){
And to change the value pointed by n to be equal to a:
*n = a;
You can't have an address in a for-loop. Try dereferencing the pointer. and also post your exact error please.

Why does this code give segmentation fault?

I wrote a small example to test my understanding of pointers and memory, however I was sure this would work but then it gave seg fault... Any idea what I am doing wrong? Add: I ran this code with other code.
#include <iostream>
using namespace std;
struct Card {
int a;
Card(int a) : a(a) { }
};
int main() {
int **p;
int **p2;
int *a;
int b =3;
char ** cArray;
Card **c = new Card*[5];
for (int i = 0; i<5; i++)
c[i] = new Card(1);
a = &b;
for (int i = 0; i< 10; i++) {
p = &a;
// p2[i] = new int;
*(cArray + i) = "string";
cout << cArray[i]<< endl;
}
for (int i = 0; i< 10; i++) {
// p2[i] = a;
cout << *a << endl;
}
}
char ** cArray;
cArray is an unintialized pointer to pointer. You cannot de-reference it.
*(cArray + i) = "string";
cArray uninitialized. Correct this.
char ** cArray = new char*[10];
cArray variable does not seem to be initialised.