简体   繁体   English

警告:赋值可从指针生成整数,而无需在shellsort算法中强制转换

[英]Warning: assignment makes integer from pointer without a cast in shellsort algorithm

I'm writing a program to perform shellsort on an array of numbers. 我正在编写一个程序来对数字数组执行shellsort。 I first have to generate the sequence of numbers that shellsort will be performed with. 我首先必须生成将执行shellsort的数字序列。 This function is to generate numbers of the form 2^p*3^q that are less than the length of the array to be sorted. 此函数将生成2 ^ p * 3 ^ q形式的数字,该数字小于要排序的数组的长度。 Then I sort the sequence array that I just generated. 然后,对刚刚生成的序列数组进行排序。 Here's my implementation of this: 这是我的实现:

long * Generate_2p3q_Seq(int length, int *seq_size) {
  int ind = 0;
  long * arr[1000];
  int product;
  int power = 1;
  while (power < length) {
    product = power;
    while (product < length) {
      arr[ind] = product;
      product *= 3;
      ind++;
    }
    power *= 2;
  }
  int i, j, k;
  for (i = 0; i < ind; ++i) {
    for (j = i + 1; j < ind; ++j)
    {
      if (arr[i] > arr[j])
      {
        k =  arr[i];
        arr[i] = arr[j];
        arr[j] = k;
      }
    }
  }
  *seq_size = ind;
  for (int count = 0; count < ind; count++) {
    printf("arr[%d] = %li\n", count, arr[count]);
  }
  return arr;
}

The code is meant to return a long * array and set seq_size to the length of the sequence array. 该代码旨在返回一个长*数组,并将seq_size设置为序列数组的长度。 For example, if I'm given an array of 16 integers to be sorted, the sequence array generated here should be 8 integers (1, 2, 3, 4, 6, 9, 8, 12) and seq_size should equal 8. I believe my understanding of pointers is wrong because my terminal output looks like this: 例如,如果给我一个要排序的16个整数数组,则此处生成的序列数组应该是8个整数(1、2、3、4、6、9、8、12),并且seq_size应该等于8。相信我对指针的理解是错误的,因为我的终端输出如下所示:

sequence.c: In function ‘Generate_2p3q_Seq’:
sequence.c:14:16: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
       arr[ind] = product;
                ^
sequence.c:26:11: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
         k =  arr[i];
           ^
sequence.c:28:16: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
         arr[j] = k;
                ^
sequence.c:34:25: warning: format ‘%li’ expects argument of type ‘long int’, but argument 3 has type ‘long int *’ [-Wformat=]
     printf("arr[%d] = %li\n", count, arr[count]);
                       ~~^            ~~~~~~~~~~
                       %ln
sequence.c:36:10: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
   return arr;
          ^~~
sequence.c:36:10: warning: function returns address of local variable [-Wreturn-local-addr]

However, I'm not sure how to change this to make it work. 但是,我不确定如何更改它以使其工作。 I call this function with: 我称这个功能为:

  long * sequence = Generate_2p3q_Seq(size, &seq_size);

Please let me know if there's any information I've left out, I really appreciate any help. 如果您遗漏了任何信息,请告诉我,我非常感谢您的帮助。

There are two main issues here. 这里有两个主要问题。 First, you declare arr as long *arr[1000] , which means it is an array of pointer to long , not an array of long . 首先,需要声明arr作为long *arr[1000]这意味着它是指针的阵列,以 long ,而不是阵列long That is why you're getting about conversions between pointers and integers. 这就是为什么要在指针和整数之间进行转换。

The proper way to define an array of long is: 定义long数组的正确方法是:

long arr[1000];

But this then leads to the second problem, namely that you are returning a pointer to a local variable. 但这然后导致第二个问题,即您正在返回指向局部变量的指针。 When the function returns its local variables go out of scope, so the returned pointer no longer points to valid memory. 当函数返回其局部变量时,它们将超出范围,因此返回的指针不再指向有效内存。

To fix this, declare arr as a pointer and use malloc to dynamically allocate memory for it: 要解决此问题,请将arr声明为指针,然后使用malloc为它动态分配内存:

long *arr = malloc((product * power) * sizeof *arr);
if (!arr) {
    perror("malloc failed");
    exit(1);
}

Then you can return the value of arr , which points to dynamically allocated memory. 然后,您可以返回arr的值,该值指向动态分配的内存。

Pass a pointer to an array as an additional parameter, and manipulate that. 将指针传递给数组作为附加参数,并对其进行操作。

void Generate_2p3q_Seq(long * arr, int length, int *seq_size) {
    // Method stores result in pre-initialized arr.
}

// Call with:

long arr[1000];
Generate_2p3q_Seq(arr, length, seq_size)

// Result stored correctly in arr.

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

相关问题 警告赋值使指针来自整数而不在 C 中进行强制转换 - Warning assignment makes pointer from integer without a cast in C C程序-警告:赋值使指针从整数开始而没有强制转换 - C Program - warning: assignment makes pointer from integer without a cast 警告:赋值使指针从整数开始,没有强制转换和排序参数 - warning: assignment makes integer from pointer without a cast, sorting arguments 警告:赋值使指针从整数变为整数,而不进行强制转换 - Warning: assignment makes integer from pointer without a cast malloc 警告:赋值使指针从整数变为整数而不进行强制转换[默认启用] - warning: assignment makes integer from pointer without a cast [enabled by default] C Linux警告:赋值使指针不带强制转换而成为整数 - C Linux Warning: assignment makes integer from pointer without a cast 警告:赋值从指针产生整数而没有强制转换,分段错误 - warning: assignment makes integer from pointer without a cast, segmentation fault 警告:赋值使用整数而不使用强制转换 - Warning: assignment makes pointer from integer without a cast gcc警告:赋值使指针从整数变为整数,而不进行强制转换 - gcc warning: assignment makes integer from pointer without a cast 任何想法? 警告分配使来自 integer 的指针没有强制转换 - Any idea ? Warning assignment makes pointer from integer without a cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM