warning: initialization makes pointer from integer without a cast [-Wint-conversion]

huangapple go评论164阅读模式
英文:

warning: initialization makes pointer from integer without a cast [-Wint-conversion]

问题

以下是您要翻译的代码部分:

我是C语言新手,要求我创建一个4D数组来完成一个简单的任务

我首先创建了一个常规数组,可以根据您提供的项数进行打印。

然后我尝试将其扩展为由两个数组组成,但似乎没有成功。

它给了我这个错误,我该如何创建一个数组内嵌另一个数组?

```c
#include <stdio.h>

int main(){

    int arrayprint;
    int a = 10;
    int b = 20;
    int c = 30;
    int d = 40;
    int* array1[] = {a, b};
    int* array2[] = {c, d};
    int** topArray[] = {array1, array2};
    int i;
    
    printf("要打印的矩阵项数是多少?(最多2项):\n");
    scanf("%d", &arrayprint);
    printf("\n您选择了%d项。\n", arrayprint);
    
    for (i = 0; i < arrayprint; i++) {
        printf("\n%d", topArray[i]);
    }

}

我之前使用普通数组编写了这段代码,它运行得很好。

但现在它似乎不喜欢它的当前状态,它给了我错误,但也能够打印出看起来像内存值的内容?


<details>
<summary>英文:</summary>

I&#39;m new to C, and for a simple task I was asked to make a 4d array.

I started by making a regular array, which would print the amount of items you tell it to.

I tried to then expand it to be composed of 2 arrays, which I haven&#39;t gotten to work.

It gives me this error, what can I do to make an array within an array?

#include &lt;stdio.h&gt;

int main(){

int arrayprint;
int a = 10;
int b = 20;
int c = 30;
int d = 40;
int* array1[] = {a, b};
int* array2[] = {c, d};
int** topArray[] = {array1, array2};
int i;

printf(&quot;Amount of matrix items to print? (Maximum 2):\n&quot;);
scanf(&quot;%d&quot;, &amp;arrayprint);
printf(&quot;\nYou chose %d items.&quot;, arrayprint);

for (i = 0; i &lt; arrayprint; i++) {
printf(&quot;\n%d&quot;, topArray[i]);
}

}


I had this code before with just a regular array, and it worked great.

But it doesn&#39;t seem to like how it is now, it gives me errors, but also works and prints what looks like memory values?

</details>


# 答案1
**得分**: 1

当声明数组时,你不使用指针,只使用指针引用整个数组。

```c
int nums[2][2] = {{1, 2}, {1, 2}};

基本上,从任何地方减去一个 * 符号。以下是一个可以复制粘贴的版本(假设两个数组具有相同的宽度):

#include <stdio.h>

int main() {

  int arrayprint;
  int a = 10;
  int b = 20;
  int c = 30;
  int d = 40;
  int array1[] = {a, b};
  int array2[] = {c, d};
  int* topArray[] = {array1, array2};

  printf("要打印的矩阵项数是多少?(最多 2 项):\n");
  scanf("%d", &arrayprint);
  printf("\n你选择了 %d 项。\n");

  for (int i = 0; i < arrayprint; i++) {
    for (int j = 0; j < arrayprint; j++) {
      printf("\n%d", topArray[i][j]);
      // 处理方形多维数组时,i 和 j 的顺序不重要。
    }
  }
}

另外,最佳实践是使用 #include <stdio.h> 而不是 #include \&lt;stdio.h\&gt;

希望这有所帮助!

英文:

When declaring arrays, you don't use pointers, you only use pointers to refer to the array in its entirety.

int nums[2][2] = {{1, 2}, {1,2};

Essentially, subtract one * from anywhere you used it. Here's a version you can copy and paste if you'd like:
(assuming both arrays are the same width)

#include &lt;stdio.h&gt;

int main(){

  int arrayprint;
  int a = 10;
  int b = 20;
  int c = 30;
  int d = 40;
  int array1[] = {a, b};
  int array2[] = {c, d};
  int* topArray[] = {array1, array2};

  printf(&quot;Amount of matrix items to print? (Maximum 2):\n&quot;);
  scanf(&quot;%d&quot;, &amp;arrayprint);
  printf(&quot;\nYou chose %d items.&quot;, arrayprint);
  
  for (int i = 0; i &lt; arrayprint; i++) {
    for (int j = 0; j &lt; arrayprint; j++) {
      printf(&quot;\n%d&quot;, topArray[i][j]);
      //when dealing with square multidimensional arrays, the order of i and j don&#39;t matter.
    }
  }
}

Also, it's best practice to use #include &lt;stdio.h&gt; instead of #include \&lt;stdio.h\&gt;.

Hope this helps!

huangapple
  • 本文由 发表于 2023年8月5日 08:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定