如何制作一系列指针并使用户输入其大小?

发布于 2025-01-21 13:32:34 字数 749 浏览 0 评论 0原文

我想制作一个数组,在此数组内有这样的指针: int *arrp [size];,我希望用户输入它的大小。 我试图这样做:

#include <iostream>
using namespace std;

int main ()
{
   int size;
   cout << "Enter the size of the array of pointers" << endl;
   cin >> size;
   int *arrp[size];
   
   return 0;
}

但这无效。 我还试图这样做:

#include <iostream>
using namespace std;

int main ()
{
   int size;
   cout << "Enter the size of the array of pointers" << endl;
   cin >> size;
   int* arrp[] = new int[size];
   
   return 0;
}

也不起作用,有人可以帮忙吗?

第一个代码的错误是大小必须是恒定的,我试图通过编写第二个代码来修复这一点,但在第9行中给出了“ new”一词的错误: E0520的初始化,'{...}'预期的聚合对象 以及同一行中大小的另一个错误: C2440“初始化”:无法从'int *'转换为'int *[]'

I want to make an array, and inside this array there are pointers, like this:
int *arrp[size]; and I want the user to enter the size of it.
I tried to do this:

#include <iostream>
using namespace std;

int main ()
{
   int size;
   cout << "Enter the size of the array of pointers" << endl;
   cin >> size;
   int *arrp[size];
   
   return 0;
}

but this doesn't work.
I also tried to do this:

#include <iostream>
using namespace std;

int main ()
{
   int size;
   cout << "Enter the size of the array of pointers" << endl;
   cin >> size;
   int* arrp[] = new int[size];
   
   return 0;
}

also doesn't work, can someone help?

The error of the first code is that the size must be constant, I tried to fix that by writing the 2nd code but it gives an error for the word "new" in line 9:
E0520 initialization with '{...}' expected for aggregate object
and another error for the size in the same line:
C2440 'initializing': cannot convert from 'int *' to 'int *[]'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2025-01-28 13:32:34

要制作一系列指针,您应该输入:int ** arr = new Int*[size]
我们键入2星'*',第一个是指通向整数的指针,第二个指指向整数指针的指针,然后我们通过键入= new Int*来在这些指针的内存中占有一席之地。 [size],您可以将其用作存储在堆中的2D数组(而不是堆栈),请访问此网站以了解差异: https://www.geeksforgeeks.org/stack-vs-vs-heap-memory-allocation/
要了解如何使用一系列指针来指向整数指针,您可以看到此视频: https://www.youtube.com/watch?v=gnguma_ur0u&ab_channel=thecherno

To make an array of pointers you should type: int** arr = new int*[size]
we type 2 stars '*', the first mean a pointer to an integer, the second means a pointer to the pointer to the integer, and then we make a place in the memory for those pointers by typing = new int*[size], you can use this as a 2D array that stored in the heap (not the stack) go to this website to know the difference: https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/.
to know more about how to use an array of pointers to a pointer to an integers you can see this video: https://www.youtube.com/watch?v=gNgUMA_Ur0U&ab_channel=TheCherno.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文