Homework: Making an array using pointers

落爺英雄遲暮 提交于 2019-12-10 16:28:07

问题


I have a homework problem that I'm working out. Me and some other students are pretty sure that our teacher misspoke, but maybe not. I checked through a bit of the questions here already and can't really find a way to use pointers to create what is essentially an array. The instructions read as follows.

  1. Rewrite the following program to use pointers instead of arrays:

The code is this

int main()
{
    int salary[20];
    int i;
    for (i = 0; i < 20; i++)
    {
        cout << "Enter Salary: ";
        cin >> salary[i];
    }
    for (i = 0; i < 20; ++i)
        salary[i] = salary[i] + salary[i] / (i + 1);

    return 0;
}

My solution was this:

int main()
{
    int* salary_pointer = new int;
    for (int i = 0; i < 20; i++)
    {
        cout << "Enter Salary: ";
        cin >> *(salary_pointer + i);
    }
    for (int i = 0; i < 20; ++i)
    {
        *(salary_pointer + i) = *(salary_pointer + i) + *(salary_pointer + i) / (i + 1);
        cout << *(salary_pointer + i) << endl;
    }
    delete salary_pointer;
    return 0;
}

It keeps flagging a segmentation fault at about salary number 13

My main purpose (because I'm almost positive my teacher wrote this down wrong) is to understand more about pointers, so any and all tips and tricks for learning these confusing things would be greatly appreciated. Thank you all!


回答1:


Use

int* salary_pointer = new int[20];

instead, as you allocate 20 ints, not just one. Then, delete the dynamic array using

delete[] salary_pointer;

instead of delete salary_pointer. Be also careful here:

salary[i] / (i + 1);

If the operands are int, then you end up with truncation. Use salary[i]/(i + 1.) in case you want your result as a double (in which case you better make salary an array of doubles or a pointer to double so you don't have this issue anymore).




回答2:


Your teacher did not misspeak. You have bugs in your program.

How many elements did you allocate? How many elements are you trying iterate through and dereference? How many elements did you free?

You're getting a seg fault, because you are dereferencing memory you did not allocate.

I'd be more specific, but giving too much away won't help you get better when it comes to homework.

This kind of manual memory management is done away with later when you will be using STL containers for the most part, but the relationship between pointers and arrays, and the ability to do pointer arithmetic is important.




回答3:


Why is your teacher wrong?

Here is what is happening. You are creating a pointer to a SINGLE integer. As you iterate through your for loop what you are doing is actually overwriting memory that is possibly, and I STRESS possibly, being used by other bits of your program. This causes undefined behavior up to and including a crash.

Redo your memory allocation and your access violation should go away. Also, use a variable to hold your '20'. Something like const int MAX_SALARIES = 20. Learn to do this as it will often save you TONS of headaches in the future.




回答4:


In this statement

int* salary_pointer = new int;

there is allocated only one object of type int.

And as there is used the division operation it is better to use type float instead of int for the array.

I would suggest the following solution. It uses only pointers.

#include <iostream>

int main()
{
    const size_t N = 20;
    float *salary = new float[N];
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    for ( float *p = salary; p != salary + N; ++p )
    {
        std::cout << "Enter Salary: ";
        std::cin >> *p;
    }

    for ( float *p = salary; p != salary + N; ++p )
    {
        *p += *p / ( p - salary + 1 );
    }

    delete [] salary;
    ^^^^^^^^^^^^^^^^
    return 0;
}


来源:https://stackoverflow.com/questions/34097161/homework-making-an-array-using-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!