简体   繁体   English

C ++-如何使用指针填充数组?

[英]C++ - How to fill array using pointers?

I am learning C++, but I don't completely understand the mechanic of using pointers. 我正在学习C ++,但是我并不完全了解使用指针的机制。

How can I achieve filling array tab2 using pointers in this code: 如何在此代码中使用指针实现填充数组tab2:

int * tab1 = new int[10];
int * tab2 = new int[10];

for (int i = 0; i < 10; i++){
  tab1[i] = i;
  *tab2 = i; 
  tab2++;
}
for (int i = 0; i < 10; i++){ 
  std::cout << tab1[i] << "\t" << tab2[i] << std::endl;
}

The teacher in my school doesn't explain it clearly and I don't understand how to adjust array elements using pointers and putting new values in it. 我学校的老师没有清楚地解释它,我也不明白如何使用指针调整数组元素并将新值放入其中。 Please, help me understand a correctly working example with it. 请帮助我理解一个正确的示例。

When you do tab2++ you lose the original pointer. 当您执行tab2++您将丢失原始指针。

And since both tab1 and tab2 are pointer, you are already doing it with pointers. 并且由于tab1tab2都是指针,因此您已经在使用指针了。 Fact: An expression like tab1[i] is exactly the same as *(tab1 + i) (and that's valid for any pointer or array). 事实:类似于tab1[i]的表达式与*(tab1 + i)完全相同(并且对任何指针数组均有效)。

And if your teacher want you to use increment, then use another pointer variable that you increase. 而且,如果您的老师希望您使用增量,请使用另一个您增加的指针变量。 Like 喜欢

int * tab3 = tab2;
for (...) { ...; *tab3++ = i; }

Now you can still use tab2 without problems. 现在,您仍然可以毫无问题地使用tab2

It's hard to guess what specifically is confusing you. 很难猜测到底是什么使您困惑。 I'll try to be general. 我会尝试成为一般的。 An array is a continuous space in memory, it is allocated either statically 数组是内存中的连续空间,可以静态分配

int arr[10];

or dynamically 或动态

int *arr = new int[10];

Arrays are accessed using pointers. 使用指针访问数组。 The pointer stores a value which references a single element of an array. 指针存储一个值,该值引用数组的单个元素。 Usually the first one. 通常是第一个。

In your program you can then use two ways to access an array. 然后,您可以在程序中使用两种方式访问​​数组。 Either using the square brackets and putting in the number of an element starting from 0. 使用方括号并输入从0开始的元素编号。

arr[0] = 1;

Or using the * operator and accessing the array directly. 或使用*运算符并直接访问数组。

*arr = 1;
arr++;
*arr = 2;

The code above stores one in the first element, increases the value of the pointer, then store two in the second element. 上面的代码在第一个元素中存储一个,增加指针的值,然后在第二个元素中存储两个。

Rewrite your loop as below 如下重写循环

for (int i = 0; i < 10; i++)
{
    tab1[i] = i;
    *(tab + i) = i;
}

With tab1 your currently using array syntax to add values to the dynamic array. 使用tab1,您当前正在使用的数组语法将值添加到动态数组。

tab[i] = i;

if you want to fill tab2 using pointer syntax, you do it as follow: 如果要使用指针语法填充tab2,请按照以下步骤操作:

*(tab2 + i) = i; 

This way your not changing the address of pointer tab2, your only accessing the elements. 这样,您无需更改指针tab2的地址,而仅访问元素。

However, when you alter tab2 directly, your actually changing the value stored in pointer tab2, so you need to set it back to the first element here's how you could do it this way: 但是,当您直接更改tab2时,实际上是更改了存储在指针tab2中的值,因此您需要将其设置回第一个元素,这是使用这种方式的方法:

for (int i = 0; i < 10; i++)
{
    tab1[i] = i;
    tab2 += i;
    *tab2 = i;
    tab2 -= i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM