vendredi 20 avril 2018

a program that involves pointers is not running c++

Here's a code my professor had given us to analyze, and there are 3 problems and additions and/or modifications I have to do in order for this code to run, can anyone help, please?

#include <iostream>
using namespace std;

int main(int argc, char **args) {
    int *ptr, *current;
    const int SIZE = 5;
    ptr = new int[SIZE];
    int value[SIZE];
    ptr = current = value;

    for (int i = 0; i <= SIZE; i++)
        value[i] = i * 2;
    for (int i = 0; i < SIZE; i++) {
        cout << *ptr << endl;
        ptr++;
    }

    cout << *(--ptr) << endl;
    for (int i = 0; i < SIZE; i++) {
        cout << *current << endl;
        current++;
    }

    cout << *(ptr) << endl;
    cout << *(--current) << endl;

    for (int i = 0; i <= SIZE; i++)
        *(ptr++) = i * 2;

    for (int i = 0; ptr <= &value[SIZE - 1]; ptr += 1)
        *ptr = i++ * 2;
    ptr--;
    cout << "The value where pointer 'ptr' pointing to is:  " << *ptr << endl;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire