英文:
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'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't gotten to work.
It gives me this error, what can I do to make an array within an array?
#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("Amount of matrix items to print? (Maximum 2):\n");
scanf("%d", &arrayprint);
printf("\nYou chose %d items.", arrayprint);
for (i = 0; i < arrayprint; i++) {
printf("\n%d", topArray[i]);
}
}
I had this code before with just a regular array, and it worked great.
But it doesn'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 \<stdio.h\>
。
希望这有所帮助!
英文:
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 <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("Amount of matrix items to print? (Maximum 2):\n");
scanf("%d", &arrayprint);
printf("\nYou chose %d items.", arrayprint);
for (int i = 0; i < arrayprint; i++) {
for (int j = 0; j < arrayprint; j++) {
printf("\n%d", topArray[i][j]);
//when dealing with square multidimensional arrays, the order of i and j don't matter.
}
}
}
Also, it's best practice to use #include <stdio.h>
instead of #include \<stdio.h\>
.
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论