user1527227
user1527227

Reputation: 2238

Copying integer array in C++

I'm teaching myself C++, and working out this program on pointers:

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. 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). If you change this statement to while(ptr1 < ptr1 + SIZE) and run it, you get a segmentation fault. Why? Even though miles is an array, I understand that is actually a pointer. If you do cout << ptr1; and 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;
}

Upvotes: 1

Views: 541

Answers (4)

iamacomputer
iamacomputer

Reputation: 565

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)

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. 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.


"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])


Read your book before you take the word of stack overflow as truth.

Upvotes: 1

Thomas Matthews
Thomas Matthews

Reputation: 57718

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"
Don't compare pointers to arrays, it's dangerous.
Try this: "ptr1 < &miles[size]"

Analysis - "ptr1 < ptr1 + SIZE"
This is a moving target because ptr1 is always changing.

Upvotes: 3

iamacomputer
iamacomputer

Reputation: 565

on the first iteration of you while loop ptr1 does indeed equal miles.

but what does ptr1 equal on the second iteration?

...

while (...) is evaluated with each iteration (some optimizers may do some cleverness to simplify the evaluation, if they can determine it is possible)

Upvotes: 0

Beta
Beta

Reputation: 99104

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 (i.e. anything can happen), and you're lucky all you get is a segmentation fault.

The array variable miles is actually a pointer to the first element in the array-- exactly the same as ptr1, at first. That's just how C++ handles arrays.

Upvotes: 4

Related Questions