[英]How to use pointers with arrays?
I have tried to solve this problem with pointers, but I couldn't.我试图用指针解决这个问题,但我做不到。 The requirement was writing a function in which you see if the array is要求是编写一个 function ,您可以在其中查看数组是否为
Here is what I wrote:这是我写的:
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;
}
The compiler is giving编译器给出
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 few things to notice,需要注意的几点,
3
, when i == 2
, comparing a[i]
with a[i + 1]
would access a[3]
, which is not within array boundaries that goes from index 0
to index 2
.当使用 'i < len' 作为循环条件时,您的代码正在测试越界索引(例如:考虑到数组长度为3
,当i == 2
时,将a[i]
与a[i + 1]
将访问a[3]
,它不在从index 0
到index 2
的数组边界内。With that in mind, a possible implementation with some tests is provided below, from what I can see from the requirements list, but bear in mind that I made some assumptions, since there was no restriction about them.考虑到这一点,下面提供了一些测试的可能实现,从我从需求列表中可以看到,但请记住,我做了一些假设,因为对它们没有限制。
#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();
}
The return type is a pointer here, but you return a value of an int
.返回类型在这里是一个指针,但是你返回一个int
的值。 You should return the address of the variable sign
instead of the value.您应该返回变量sign
的地址而不是值。 You can do it by changing the last line of your code like this:您可以通过更改代码的最后一行来做到这一点,如下所示:
return &sign;
But it will also give a runtime error.但它也会给出运行时错误。 Because compiler always makes a stack for a function call.因为编译器总是为 function 调用创建一个堆栈。 As soon as the function exits the function stack also gets removed which causes the local variables of functions to go out of scope. As soon as the function exits the function stack also gets removed which causes the local variables of functions to go out of scope.
To resolve this you can declare the variable as static.要解决此问题,您可以将变量声明为 static。 Static Variables have the property of preserving their value even after they are out of their scope. Static 变量具有保留其值的特性,即使在它们超出其 scope 之后也是如此。 So to execute the concept of returning a pointer from a function in C you must define the local variable as a static variable.因此,要执行从 C 中的 function 返回指针的概念,您必须将局部变量定义为 static 变量。
So you should declare the variable sign
like it shown bellow:所以你应该声明变量sign
,如下所示:
static int sign;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.