简体   繁体   English

使用指针作为动态分配的数组

[英]Using pointers as a dynamically allocated array

I'm trying to write a program that dynamically allocates memory for an array, which the user then fills with integer values and the program sorts said integer values. 我正在尝试编写一个为数组动态分配内存的程序,然后用户将其填充为整数值,然后该程序会对所述整数值进行排序。 However, it seems that my array isn't working as intended. 但是,看来我的阵列无法正常工作。 I have managed to get the program working with a static array, but the dynamic allocation is causing me a lot of problems with incorrect values and whatnot. 我设法使程序与静态数组一起使用,但是动态分配给我带来了很多问题,包括不正确的值和诸如此类的问题。 Here's what I have so far for the dynamically allocated version (if it would help you guys, I can also provide the version that uses a static array): 到目前为止,这是我动态分配的版本(如果有帮助的话,我还可以提供使用静态数组的版本):

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

void sortArray (int *numbers, int i2); 
int indexMax (int *numbers, int low, int high);
void swap (int *num1, int *num2);
int getArray (int *numbers);
void displayArray (int *numbers, int i2);

main()
{
    int *numbers, i2;
    i2=getArray(numbers); 
    sortArray(numbers, i2); 
    displayArray (numbers, i2); 
}

int getArray (int *numbers)
{
    int i, i2;
    printf("Please enter the amount of elements you wish to sort: ");
    i2=GetInteger();
    numbers=(int *)malloc(i2*sizeof(int));
    for(i=0;i<i2;i++, numbers++)
    {
        printf("Enter next integer: ");
        *numbers=GetInteger();
        printf("\n");
    }
    return(i2);
}

void displayArray (int *numbers, int i2)
{
    int i;
    printf ("\nThe sorted list is: \n\n");
    for (i=0;i<i2;i++, numbers++)printf ("%d\n", *numbers); 
}

void sortArray (int *numbers, int i2)
{
    int i, minInd;
    for(i=0;i<i2;i++)
    {
        minInd=indexMax(numbers, i, i2-1);
        swap(&numbers[i], &numbers[minInd]);        
    }
}

int indexMax (int *numbers, int low, int high)
{
    int i, maxInd;
    maxInd=high;
    for (i=high;i>=low;i--)
    {
        if(*(numbers+i)>*(numbers+maxInd)) maxInd=i;
    }
    return (maxInd);
}

void swap (int *num1, int *num2)
{
    int temp;
    temp=*num1;
    *num1=*num2;
    *num2=temp;
}    

Try this: 尝试这个:

int main(void)
{
    int *numbers, i2;
    i2 = getArray(&numbers); 
    sortArray(numbers, i2); 
    displayArray (numbers, i2); 

    return 0;
}

int getArray (int **numbers)
{
    int i, i2;
    printf("Please enter the amount of elements you wish to sort: ");
    i2 = GetInteger();
    *numbers = malloc(i2 * sizeof int);

etc... 等等...

You simply need one more level of indirection to make int getArray(int**) return a pointer to an allocated array. 您只需int getArray(int**)一层间接即可使int getArray(int**)返回指向已分配数组的指针。

Here is a working solution: 这是一个可行的解决方案:

#include <stdio.h>
#include <stdlib.h>

void sortArray (int *numbers, int i2); 
int indexMax (int *numbers, int low, int high);
void swap (int *num1, int *num2);
int getArray (int **numbers);
void displayArray (int *numbers, int i2);

main()
{
    int *numbers, i2;
    i2=getArray(&numbers); 
    sortArray(numbers, i2); 
    displayArray (numbers, i2); 
}

int getArray (int **numbers)
{
    int i, i2;
    printf("Please enter the amount of elements you wish to sort: ");
    scanf("%d", &i2);
    (*numbers) = malloc(i2 * sizeof(int));
    int *temp = *numbers;
    for(i = 0; i < i2; i++)
    {
        printf("Enter next integer: ");
        scanf("%d", &temp[i]);
        printf("\n");
    }
    return(i2);
}

void displayArray (int *numbers, int i2)
{
    int i;
    printf ("\nThe sorted list is: \n\n");
    for (i=0;i<i2;i++, numbers++)printf ("%d\n", *numbers); 
}

void sortArray (int *numbers, int i2)
{
    int i, minInd;
    for(i=0;i<i2;i++)
    {
        minInd=indexMax(numbers, i, i2-1);
        swap(&numbers[i], &numbers[minInd]);        
    }
}

int indexMax (int *numbers, int low, int high)
{
    int i, maxInd;
    maxInd=high;
    for (i=high;i>=low;i--)
    {
        if(*(numbers+i)>*(numbers+maxInd)) maxInd=i;
    }
    return (maxInd);
}

void swap (int *num1, int *num2)
{
    int temp;
    temp=*num1;
    *num1=*num2;
    *num2=temp;
}

The thing is in your main when you are declaring int *numbers , numbers pointer is pointing to some junk memory location as local variables can have any garbage value, so while you are passing this numbers pointer to getArray() function you are passing its value, Suppose numbers is pointing to some random value = 1234 and suppose address of numbers is = 9999. Now when you call getArray(numbers) you tell taht whatever is there in numbers pass it to getArray's numbers variable that we are letting is 1234. 当您声明int * numbers时,事情就在您的主计算机中,因为本地变量可以具有任何垃圾值,所以数字指针指向某个垃圾内存位置,因此,当您将此数字指针传递给getArray()函数时,您将传递其值,假设数字指向某个随机值= 1234,并且假设数字的地址为=9999。现在,当您调用getArray(numbers)时,您告诉taht中存在的任何数字都将其传递给getArray的numbers变量,我们将其设为1234。

Then when you allocate memory to numbers that is local variable of getArray() function not main's and it might have address suppose = 0x8888. 然后,当您将内存分配给作为getArray()函数的局部变量而不是main的局部变量的数字时,其地址可能假设为0x8888。 Then malloc allocates some address space as specified and stores the start address of that allocated address space(suppose ie = 0x7777) into location 0x8888 not 0x9999 which is the adress of main's numbers variable. 然后,malloc按照指定的方式分配一些地址空间,并将该分配的地址空间的起始地址(假设即= 0x7777)存储到位置0x8888而不是地址0x9999(这是main的数字变量的地址)中。

Thus when the getArray function ends and next time you call sortArray you pass it value present in numbers variable of main which is still junk 1234. and the actual value you should be passing is present at address 0x8888. 因此,当getArray函数结束并且下次调用sortArray时,您将传递给main的number变量中存在的值,该值仍然是垃圾1234。而您应该传递的实际值位于地址0x8888处。

As an aside, you can use the C library function qsort() to do the sorting work for you. qsort() ,您可以使用C库函数qsort()为您完成排序工作。 Here is an example: 这是一个例子:

int main(void)
{
    int a[]={4,7,9,1,34,90,66,12};

    qsort(a, sizeof(a)/sizeof(a[0]), sizeof(int), compare);

    return 0;
}

int compare(const void *a, const void *b){
     const int *x = a, *y = b;
     if(*x > *y)
       return 1;
     else
       return(*x < *y) ? -1: 0;
}  

Works just as well with dynamically allocated int arrays, or char arrays 与动态分配的int数组或char数组一样好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM