如何在数组中使用指针?

Posted

技术标签:

【中文标题】如何在数组中使用指针?【英文标题】:How to use pointers with arrays? 【发布时间】:2022-01-21 10:24:11 【问题描述】:

我试图用指针解决这个问题,但我做不到。 要求是编写一个函数,您可以在其中查看数组是否为

排序增加(返回 1) 排序减少(返回 -1) 根本没有排序(返回0)

这是我写的:

int *test(int l,int *T)

   int i,A[l],sign;
   int *t=&A[0];
   for (i=0; i<l; i++)
   
       if(A[i]>A[i+1])
           t++;
       sign =-1;
       
       if (A[i]<A[i+1])
           t++;
       sign =1;
       
       if (A[i]!=A[i+1])
            t++;
          sign =0;
       
   
   return sign;
 

编译器给出

returning ‘int’ from a function with return type ‘int *’ makes pointer from integer without a cast [-Wint-conversion]
   61 |     return sign;


error: ld returned 1 exit status

【问题讨论】:

A[i]&gt;A[i+1] undefined 当i== l-1时的行为 int test(int l, int *T) ... signint 你想返回指向int 的指针(见星号) A 未初始化 - 未定义行为 你能告诉我我能做些什么让micro pro比较数组的所有元素然后返回值 【参考方案1】:

有几点需要注意,

“T”参数未使用,因此已删除。 “t”变量未使用,因此已删除。 根据您的要求,函数返回类型不应该是指向整数的指针,而应该只是一个整数。 您的代码正在针对函数范围内声明的数组进行测试,由于它是一个自动变量,因此它没有被初始化并且可能包含垃圾值。 当使用 'i 3,当i == 2 时,比较a[i] 和@987654324 @ 将访问a[3],它不在从index 0index 2 的数组边界内。

考虑到这一点,下面提供了一些测试的可能实现,从我从需求列表中可以看到,但请记住,我做了一些假设,因为对它们没有限制。

#include <assert.h>

#define SORTED_ASC 1
#define SORTED_DES -1
#define UNSORTED 0

int is_sorted(int *arr, int len)

    int sorted = 0;

    // I am assuming that this approach is reasonable, check your requirements.
    if (len <= 1)
        return UNSORTED;

    for (int i = 0; i < len - 1; i++)
           
        // Previous iteration detected order as 'descending', but current 
        // is 'ascending'.
        if (sorted == SORTED_DES && arr[i] < arr[i + 1])
            return UNSORTED;

        // Previous iteration detected order as 'ascending', but current 
        // is 'descending'.
        if (sorted == SORTED_ASC && arr[i] > arr[i + 1])
            return UNSORTED;

        // I am assuming that arrays with repeated values should remain classified 
        // as 'unsorted' until a different value appears, check your requirements.
        if (arr[i] > arr[i + 1])
            sorted = SORTED_DES;
        else if (arr[i] < arr[i + 1])
            sorted = SORTED_ASC;
   

   return sorted;
 

void test_unsorted()

    int arr[4][3] = 
         1, 3, 2 ,
         2, 1, 3 ,
         2, 3, 1 ,
         3, 1, 2 
    ;

    for (int row = 0 ; row < 4 ; row++)
    
        int res = is_sorted(arr[row], 3);
        assert(res == UNSORTED);
    


void test_sorted_ascending()

    int arr[] =  1, 2, 3 ;
    int res = is_sorted(arr, 3);
    assert(res == SORTED_ASC);


void test_sorted_descending()

    int arr[] =  3, 2, 1 ;
    int res = is_sorted(arr, 3);
    assert(res == SORTED_DES);


void test_with_repeated_values()

    int sorted_asc[] =  1, 1, 2 ;
    int sorted_asc_res = is_sorted(sorted_asc, 3);
    assert(sorted_asc_res == SORTED_ASC);

    int sorted_des[] =  3, 3, 2 ;
    int sorted_des_res = is_sorted(sorted_des, 3);
    assert(sorted_des_res == SORTED_DES);

    int unsorted[] =  1, 1, 1 ;
    int unsorted_res = is_sorted(unsorted, 3);
    assert(unsorted_res == UNSORTED);


int main(void)
   
    test_unsorted();
    test_sorted_ascending();
    test_sorted_descending();
    test_with_repeated_values();

【讨论】:

最后但并非最不重要的一点是,编译器给出的错误是因为当您的返回类型是整数指针时,您试图返回一个整数(int 符号);]

以上是关于如何在数组中使用指针?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 STL 中使用指向向量的指针,就像我们在将数组地址传递给另一个函数时将指针分配给数组一样?

如何在 Python 指针中有效地迭代数组?

如何使用函数指针数组?

如何在 C++ 中初始化一个指针数组

如何使用 cppyy 从指针索引类数组

如何在 C MPI 中发送指向数组的指针数组