我似乎无法为指针分配一个数组,然后更改数组的内容
I cant seem to assign a pointer an array and then change the contents of the array
我不知道如何让函数返回数组,所以我决定尝试将一个空数组(大小正确(传递到我的函数中,而不是将地址重新分配给相同大小的不同数组。这到底是一种做事方式吗???有人可以告诉我该怎么做吗?如果这是错误的,你能告诉我如何做到这一点吗?
这是我的代码:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray,int s, int f){
int *ptrarray = &earray;
int prenum_size = std::abs(s) + f - 1;
int pre_num[prenum_size];
for(int x=s;x<f;x++){
pre_num[x+std::abs(s)] = x;
}
*ptrarray = pre_num;
}
int Main(){
int first = -10;
int second = 15;
int temp[abs(first) + abs(second)];
ArrayFiller(temp, first, second);
int n = sizeof(temp)/sizeof(temp[0]);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
return 0;
}
我想你正在寻找这样的东西:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray[],int s, int f){
for(int x=s;x<f;x++){
earray[x+(std::abs(s))] = x;
}
}
int main(){
int first = -10;
int second = 15;
int n = abs(first)+abs(second);
int* temp = new int[n];
ArrayFiller(temp, first, second);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
delete [] temp;
return 0;
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 我似乎无法为指针分配一个数组,然后更改数组的内容
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何在C++中初始化值,然后稍后为uint32_t数组赋值
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
- 将 char[] 数组转换为字节,然后将字节转换为 int,反之亦然
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序
- C++ 如何创建 2D 数组,将其传递给另一个函数,然后打印
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃
- 按字母顺序排序字符数组,然后按长度排序
- 将2D数组传递到函数,然后将其乘以两个
- 将结构数组BYRE从C#传递到C ,然后将其恢复
- 如何发送(和接收)从 Swift 到 Objective c++ 再到 c++,然后备份到 Swift 的超大型浮动数组
- 创建对象数组然后有效删除它们的更好方法
- 如何从.txt文件复制单词到数组.然后将每个单词打印在单独的一行上