简体   繁体   English

在C ++中复制整数数组

[英]Copying integer array in C++

I'm teaching myself C++, and working out this program on pointers: 我正在自学C ++,并针对指针编写了该程序:

Write a program that stores the following numbers in the array named miles: 15, 22, 16, 18, 27, 23, and 20. Have your program copy the data stored in miles to another array named dist, and then display the values in the dist array. 编写一个程序,在名为Miles的数组中存储以下数字:15、22、16、18、27、23和20。让程序将以Miles存储的数据复制到另一个名为dist的数组中,然后在其中显示值。 dist数组。 Your program should use pointer notation when copying and displaying array elements. 复制和显示数组元素时,程序应使用指针符号。

So I worked out the problem and my code is below. 所以我解决了这个问题,下面是我的代码。 In my while loop, I have the statement: while(ptr1 < miles + SIZE) . 在我的while循环中,我有while(ptr1 < miles + SIZE)语句: while(ptr1 < miles + SIZE) If you change this statement to while(ptr1 < ptr1 + SIZE) and run it, you get a segmentation fault. 如果将此语句更改为while(ptr1 < ptr1 + SIZE)并运行它,则会出现分段错误。 Why? 为什么? Even though miles is an array, I understand that is actually a pointer. 即使Miles是一个数组,但我知道它实际上是一个指针。 If you do cout << ptr1; 如果您执行cout << ptr1; and cout << miles; cout << miles; , the output will be the same. ,输出将相同。 Can someone please explain? 有人可以解释一下吗? Thank you. 谢谢。

#include<iostream>
#include<iomanip>

using namespace std;

void prob4()
{
    int miles[] = {15, 22, 16, 18, 27, 23, 20};
    const int SIZE = sizeof(miles)/sizeof(miles[0]);

    int dist[SIZE] = {0};

    int *ptr1 = miles;
    int *ptr2 = dist;

    while(ptr1 < miles + SIZE)
    {
        *ptr2 = *ptr1;
        ptr2++;
        ptr1++;
    }   

    for(int i=0; i < SIZE; i++)
        cout << setw(4) << dist[i];
    cout << endl;

    return;
}

int main()
{
    prob4();
    return 9;
}

You're asking several questions here. 您在这里问几个问题。

The conditional ptr1 < ptr1 + SIZE always evaluates to true, so the loop never ends, the pointers march past the ends of the arrays, you dereference invalid pointers, which causes undefined behavior (ie anything can happen), and you're lucky all you get is a segmentation fault. 条件ptr1 < ptr1 + SIZE 始终计算为true,因此循环永远不会结束,指针行进通过数组的末端,您取消引用无效的指针,这会导致未定义的行为 (即, 任何事情都可能发生),并且您很幸运您得到的是细分错误。

The array variable miles is actually a pointer to the first element in the array-- exactly the same as ptr1 , at first. 数组变量miles实际上是一个指针,指向array--完全相同的作为第一要素ptr1 ,在第一。 That's just how C++ handles arrays. 这就是C ++处理数组的方式。

Arrays are not pointers. 数组不是指针。

Pointers point to things. 指针指向事物。 Arrays contain things. 数组包含事物。

A safe method for your loop is to count by items, not by pointer locations. 循环的一种安全方法是按项目计数,而不是按指针位置计数。

One issue is that the ending pointer value may not be in the range or non-existent. 一个问题是结束指针值可能不在范围内或不存在。

Analysis - "ptr1 < miles + size" 分析-“ ptr1 <英里+大小”
Don't compare pointers to arrays, it's dangerous. 不要比较指向数组的指针,这很危险。
Try this: "ptr1 < &miles[size]" 试试这个:“ ptr1 <&miles [size]”

Analysis - "ptr1 < ptr1 + SIZE" 分析-“ ptr1 <ptr1 +大小”
This is a moving target because ptr1 is always changing. 这是一个移动的目标,因为ptr1总是在变化。

Just so you know, your accepted answer suggests many things which are wrong. 众所周知,您接受的答案表明很多错误。


"A safe method for your loop is to count by items, not by pointer locations." “循环的一种安全方法是按项目计数,而不是按指针位置计数。”

while indeed, looping by count is indeed easier for the novice programmer, if you work on projects (games) which require you not to write slow code: 实际上,如果您从事不需要编写慢速代码的项目(游戏),那么对于新手程序员而言,按计数循环确实更容易。

T *begin = X;
T *end = X + size; 
T *copyTo = Y;

for (T *i=begin; i!=end; ++i, ++copyTo)
  *copyTo = *i;

is faster and just as elegant.. the compiler will optimize out the variables for begin and end and copyTo, (the end variable will end up being a register most likely, as well as copyTo) 更快,更优雅。.编译器将优化变量begin和end以及copyTo,(end变量和copyTo最终将成为寄存器)

this is probably what your book was asking for. 这可能是您的书所要的。


"Pointers point to things. Arrays contain things." “指针指向事物。数组包含事物。”

In C/C++ this is just a weird thing to say. 在C / C ++中,这只是一个奇怪的说法。 Arrays are pointers, pointers are arrays. 数组是指针,指针是数组。

int x[32];
x[5] equates to *(x + 5) where x really is "int *"

x is a pointer to an area of the stack in this case.

this is why you can cast pointers into things such as char *x[] for function declarations. 这就是为什么您可以将指针转换为char * x []之类的函数声明的原因。


"One issue is that the ending pointer value may not be in the range or non-existent." “一个问题是结束指针值可能不在范围内或不存在。”

This is also a weird thing to say. 这也是奇怪的话。

if you write a loop which goes out of range, then it goes out of range, whether you are dealing with dereferencing pointers (*p) or doing pointer arithmetic (x[5]) 如果编写的循环超出范围,则无论您处理的是解引用指针(* p)还是进行指针算术(x [5]),该循环都超出范围


Read your book before you take the word of stack overflow as truth. 在将堆栈溢出这个词视为真理之前,请先阅读本书。

on the first iteration of you while loop ptr1 does indeed equal miles. 在您的第一次迭代中,循环ptr1确实等于英里。

but what does ptr1 equal on the second iteration? 但是ptr1在第二次迭代中等于什么?

... ...

while (...) is evaluated with each iteration (some optimizers may do some cleverness to simplify the evaluation, if they can determine it is possible) (...)在每次迭代中进行评估(如果某些优化器可以确定可行的话,某些优化器可能会做一些聪明的事情来简化评估)

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

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