在堆上存储整数数组并通过指针访问它们

霍莉·玛丽·巴切洛

我希望有人可以阐明我在哪里出错了指针。.我读了无数的网页并尝试了各种尝试,但是由于某种原因,我的代码返回了乱码(我猜这可能是内存地址)而不是数组中的数据)。该程序的目的是在堆上创建一个包含100个元素的数组,并通过一个指向函数的指针将该数组传递给函数(以及两个整数变量start和end);将在堆上创建一个新数组(该数组包含使用开始和结束变量的原始数组的一部分),并将指向该数组的指针传递回main方法,以便可以输出新数组。我的问题不仅是输出似乎是位置而不是值,而且输出的似乎是100个值,而不是预期的20个。一世' 我花了数小时试图弄清楚我哪里出了问题,以及当我认为我了解了指针的概念时,我的信念被红色的弯曲和错误的输出所破坏。请帮忙!我的代码如下:

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;

double* getSubArray(double*, int, int);// Declare a function that will get the sub array

int _tmain(int argc, _TCHAR* argv[])
{
    const int size = 100;// Declare the size of the array
    double* pA;// Declare the variable to hold the pointers to the data in array
    double* pB;

    int start = 15;
    int end = 35;

    pA = new double[size];// Create space for the array
    srand(clock());// Seed the program to the computers current time so that random gets a different set of random numbers everytime it is run

// Use a for loop to traverse through each element of the array (starting at index 0) placing a number defined by the random function that is no higher than 250
    for (int i = 0; i < size; i++)
    {
        pA[i] = rand()%250;
    }

cout << "An Array of 100 numbers is created and stored in the heap, these values are:" << endl;
// Output the Array for the user to see
for (int j = 0; j < size; j++)
{
    // Place 10 numbers on each line
    if (j % 10 == 0)
    {
        cout << endl;
    }
    cout << *(pA + j) << " ";
}

cout << endl << "The program will build a second array using the data between the indexes " << start << " & " << end << endl;

pB = getSubArray(pA, start, end);// Pass the data to the method

// Output second array for user to compare
for (int k = 0; k < size; k++)
{
    // Place 10 numbers on each line
    if (k % 10 == 0)
    {
        cout << endl;
    }
    cout << *(pB + k) << " ";
}

system("pause");


return 0;
}

double* getSubArray(double* pA, int start, int end)
{
    double* pB = new double[end-start];// Declare space in the heap for the new array whoes size is the size of the criteria given

for (int i = 0; i < (end - start); i++)
{
    for (int j = start; j < end; j++)
    {
        *(pB + 0) = pA[j];
    }
}
return pB;
}
迈克·西摩
*(pB + 0) = pA[j];

那一直在写数组的第一个元素。当然,您想依次写入每个元素:

for (int i = start; i < end; ++i) {
    pB[i-start] = pA[i];
}

或者如果您不想编写自己的循环

std::copy(pA+start, pA+end, pB);

不要忘了delete[]所有事情,new[]或者,为了避免低级内存管理带来的麻烦,请使用它std::vector来为您管理动态阵列。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

指向整数数组的指针

来自分类Dev

通过它们的指针访问二维数组

来自分类Dev

循环通过整数数组

来自分类Dev

C:创建随机生成的整数,将它们存储在数组元素中,并打印每个元素中存储的整数数量

来自分类Dev

通过指针访问数组

来自分类Dev

通过指针访问数组

来自分类Dev

在MySQL中存储整数数组

来自分类Dev

整数数组未正确存储

来自分类Dev

将字符存储在整数数组中

来自分类Dev

使用DataBinding访问整数数组

来自分类Dev

与使用malloc在堆上分配内存相比,使用整数数组有什么优势?

来自分类Dev

c中的整数数组(例如int a []):为什么当我使用a [i]访问它们时返回错误的值?

来自分类Dev

通过指针访问数组元素?

来自分类Dev

通过指针访问多维数组

来自分类Dev

无法创建访问类型以访问整数数组

来自分类Dev

整数数组初始化的C ++指针

来自分类Dev

使用Malloc()使用指针创建整数数组

来自分类Dev

如何在数组中存储整数数组的引用

来自分类Dev

JavaScript-将多个对象存储在数组中,并通过以下方式访问它们的属性

来自分类Dev

通过指针访问和存储值

来自分类Dev

如何存储多个数组并使指针数组指向它们

来自分类Dev

如何存储多个数组并创建指向它们的指针数组

来自分类Dev

将多个用户输入存储到整数数组中

来自分类Dev

Postgres:如何从存储的函数返回整数数组

来自分类Dev

如何在RFduino闪存中存储整数数组?

来自分类Dev

用整数数组作为参数调用存储过程?

来自分类Dev

将整数数组传递给 SQL Server 存储过程

来自分类Dev

仅使用指针算法访问整数指针数组的元素

来自分类Dev

memset如何通过-1初始化整数数组?

Related 相关文章

热门标签

归档