V
V
Vladislav2015-01-02 09:29:49
C++ / C#
Vladislav, 2015-01-02 09:29:49

Pointers. How to create a similar design (in the application)?

Pointer double **p = 0;
It is necessary to create a construction of the form:
5baa898327734feab46b0760c3997d1b.png
The length of the array is entered previously from the keyboard. Zero out all array elements. Put the number 2 in the first and last elements and print the array to the screen. Delete all dynamic objects.
My code:

double **p = 0;
    int size = 0;
    cout << "Введите размер массива: ";
    size = inputInt();
    cout << "Размер массива: " << size << endl; 
    if (size <= 1) {
        size = 1;
    }
    p = new double*[size];
    for (int i = 0; i < size;i++) {
        p[i] = new double(0);
        cout << p[i] << "\t";
    }
    **p = 2;
    *p[size-1] = 2;
    for (int i = 0; i < size;i++) {
        cout << *p[i] << "\t";
    }    

     for (int i = 0; i < size;i++) {
        delete[] p[i];
    }
    delete[] p;

But I am tormented by vague doubts, I believe that my code is a construction of the form:
04df26c16601448d972156de0bd45cd8.png
Tell me, is it so?
Could you explain to me in words what the 1st construction means?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2015-01-02
@OnlyBooid

p -> x -> []

double **p;
p = new double *;
*p = new double[size];
*p[i] = ...

A
AtomKrieg, 2015-01-02
@AtomKrieg

Yes it is. Why do you need a pointer to a pointer in this lab?
How to do the first drawing

p = new (double*)[1];
p[0] = new double[size];

A
abcd0x00, 2015-01-02
@abcd0x00

The code
#include <iostream>

using namespace std;

int main()
{
    double **p = 0;

    p = new double *;

    int size;
    cout << "Input size: ";
    cin >> size;

    *p = new double[size];

    for (int i = 0; i < size; i++)
        *(*p + i) = 0;

    **p = *(*p + size - 1) = 2;

    cout << "size: " << size << endl;
    for (int i = 0; i < size; i++)
        cout << *(*p + i) << " ";
    cout << endl;

    delete [] *p;
    delete p;
    p = 0;

    return 0;
}

Conclusion
[[email protected] cpp]$ .iso++ t.cpp -o t
[[email protected] cpp]$ ./t
Input size: 10
size: 10
2 0 0 0 0 0 0 0 0 2 
[[email protected] cpp]$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question