C++ - 从 'int' 到 'int**' 的转换无效
C++ - invalid conversion from 'int' to 'int**'
我有以下代码:
#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;
}
编译器说:
[错误]从'int'到'int **'[-fpermissive]的转换无效 [错误]从'int **'到'int'的无效转换[-fpermissive] 这是什么意思?谢谢
这样的错误意味着您将整数和指针混合到整数。您的代码中有几种情况发生。
让我们看这个示例中的指针
//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
让我们看一下您的代码
#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;
}
这是有关使用指针的教程
相关文章:
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度