从函数返回到main的指针数组为NULL

萨皮尔

我被要求在 C 中创建一个函数,它获取一个 int 数组、它的大小和一个字符,表示排序是需要升序还是降序(0 表示升序,任何其他字符如果降序),并返回一个指针数组。如果排序是升序,则指针将从 int 数组中的值的最小值到最大值进行排序。就像这样,一切都运行良好,直到指针数组以 NULL 值返回到主数组。我的意思是,在函数结束并返回数组之前,数组中有值(并且它们也排序得很好),但是当 main 从函数中获取地址时,它为空。有人熟悉这个问题吗?

谢谢!

#include <stdio.h>

#define SIZE 100

// Functions declaration
int** pointerSort(int* arr, unsigned int size, char ascend_flag);
void sortMerge (int** sortedArr, int left, int right, char ascend_flag);
void mergeSortAscend (int** arr, int left, int middle, int right);
void mergeSortDescend (int** arr, int left, int middle, int right);
void copyAddress(int** addressArray, int* arr, unsigned int size);

int main()
{
    int** sortedArr;
    int  arr[5] = {8,1,7,2,4};
    int size = 5;

    sortedArr = pointerSort(arr, size, '1');

    return 0;
}

// The pointerSort function gets an array, its size and a flag representing "sort by" (ascending, descending)
int** pointerSort(int* arr, unsigned int size, char ascend_flag)
{
    // Variables declaration
    int* sortedArr[size];

    // Copying the addresses of each value in arr to an addresses array
    copyAddress(sortedArr, arr, size);

    // Sending the array of addresses, its start and end and the ascend_flag to the sortMerge function
    sortMerge(sortedArr, 0, size - 1, ascend_flag);

    return (sortedArr);
}

void sortMerge (int** sortedArr, int left, int right, char ascend_flag)
{
    // Variables declaration
    int middle = (left + right) / 2;

    // Checking if there are more than one cell
    if (left < right)
    {
        // Splitting the array into two
        sortMerge(sortedArr, left, middle, ascend_flag);
        sortMerge(sortedArr, middle + 1, right, ascend_flag);

        // Checking if the sort requested is ascending or descending
        if (ascend_flag != '0')
        {
            mergeSortAscend(sortedArr, left, middle, right);
        }
        else
        {
            mergeSortDescend(sortedArr, left, middle, right);
        }
    }
}

void mergeSortAscend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) > *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

void copyAddress(int** addressArray, int* arr, unsigned int size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        addressArray[i] = &(arr[i]);
    }
}

void mergeSortDescend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) <= *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }
    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}
SG7

一旦您的函数返回int* sortedArr[size];inint** pointerSort(int* arr, unsigned int size, char ascend_flag)就会无效,因为它是分配给堆栈的自动变量。

你需要一个永久的存储空间。例如,sortedArr通过malloc函数在堆上分配内存并返回它。

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

#define SIZE 100

// Functions declaration
int** pointerSort(int* arr, unsigned int size, char ascend_flag);
void sortMerge (int** sortedArr, int left, int right, char ascend_flag);
void mergeSortAscend (int** arr, int left, int middle, int right);
void mergeSortDescend (int** arr, int left, int middle, int right);
void copyAddress(int** addressArray, int* arr, unsigned int size);

int main(void)
{
    int** sortedArr;
    int  arr[5] = {8,1,7,2,4};
    int size = 5;
    int i =0;

    sortedArr = pointerSort(arr, size, '1');

    if(sortedArr) // memory was allocated
       for(i=0; i<5; i++){
           printf("%d ", *sortedArr[i]);
       }

    free(sortedArr);
    return 0;
}

// The pointerSort function gets an array, its size and a flag representing "sort by" (ascending, descending)
int** pointerSort(int* arr, unsigned int size, char ascend_flag)
{
    // Variables declaration
    //int* sortedArr[size];
    int ** sortedArr = malloc( sizeof(int *) * size);
    if(sortedArr == NULL) return NULL; // no memory

    // Copying the addresses of each value in arr to an addresses array
    copyAddress(sortedArr, arr, size);

    // Sending the array of addresses, its start and end and the ascend_flag to the sortMerge function
    sortMerge(sortedArr, 0, size - 1, ascend_flag);

    return (sortedArr);
}

void sortMerge (int** sortedArr, int left, int right, char ascend_flag)
{
    // Variables declaration
    int middle = (left + right) / 2;

    // Checking if there are more than one cell
    if (left < right)
    {
        // Splitting the array into two
        sortMerge(sortedArr, left, middle, ascend_flag);
        sortMerge(sortedArr, middle + 1, right, ascend_flag);

        // Checking if the sort requested is ascending or descending
        if (ascend_flag != '0')
        {
            mergeSortAscend(sortedArr, left, middle, right);
        }
        else
        {
            mergeSortDescend(sortedArr, left, middle, right);
        }
    }
}

void mergeSortAscend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) > *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }
    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

void copyAddress(int** addressArray, int* arr, unsigned int size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        addressArray[i] = &(arr[i]);
    }
}

void mergeSortDescend (int** arr, int left, int middle, int right)
{
    int l = left, m = middle + 1, r = right, i = left;
    int* temp[SIZE];

    while ((l <= middle) && (m <= right))
    {
        if (*(arr[l]) <= *(arr[m]))
        {
            temp[i] = arr[m];
            m++;
        }
        else
        {
            temp[i] = arr[l];
            l++;
        }
        i++;
    }

    if (l <= middle)
    {
        while (l <= middle)
        {
            temp[i] = arr[l];
            l++;
            i++;
        }
    }

    if (m <= right)
    {
        while (m <= right)
        {
            temp[i] = arr[m];
            m++;
            i++;
        }
    }

    for (int k = left; k <= right; k++)
    {
        arr[k] = temp[k];
    }
}

测试:

1 2 4 7 8 

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从函数调用返回后指针为空

来自分类Dev

将数组地址从函数返回到指针(C ++)

来自分类Dev

将char数组从C ++函数返回到tcl

来自分类Dev

如何在函数中返回指针数组?

来自分类Dev

如何将值从递归函数返回到数组

来自分类Dev

从C ++中的函数返回指向数组的指针?

来自分类Dev

将两个指针返回到动态数组

来自分类Dev

返回(保持null)后,指针将不会从函数返回到其引用

来自分类Dev

将指针设置为null后,通过指针访问函数

来自分类Dev

C:如何将多个值从函数返回到main

来自分类Dev

将指针返回数组的函数

来自分类Dev

函数文档缺少有关返回的指针为NULL或非NULL的信息。假设什么?

来自分类Dev

将函数的静态数组返回到main()函数中的另一个数组

来自分类Dev

将2D Cython指针返回到Python数组

来自分类Dev

从以指针为参数的函数返回指针

来自分类Dev

函数返回的数组指针类型

来自分类Dev

将foreach值从函数返回到数组

来自分类Dev

将数组地址从函数返回到指针(C ++)

来自分类Dev

函数返回数组作为指针

来自分类Dev

编辑函数中的数组并将数组返回到C中的main

来自分类Dev

从C ++中的函数返回指向数组的指针?

来自分类Dev

进入函数时,存储在全局变量中的函数指针将设置为0,而在退出函数时将其返回到先前的状态

来自分类Dev

C语言如何将指针从函数返回到指针

来自分类Dev

从指针数组返回字符串的函数

来自分类Dev

如何将字符串从函数返回到 main()?

来自分类Dev

从函数返回 NULL 的指针数组

来自分类Dev

返回函数指针数组

来自分类Dev

将整个 unsigned char 数组返回到 main

来自分类Dev

如何从函数返回指向数组的指针?

Related 相关文章

热门标签

归档