[英]3D Arrays in C and Printing Its content
I'm trying to do something that should be simple. 我正在尝试做一些简单的事情。 I am making an array that only has 1 element in it.
我正在制作一个只有1个元素的数组。 Which is coordinates xyz
哪个是坐标xyz
So I figured Id make an array with 1 row and 3 columns, 1 column for the xy and z. 因此,我想出了一个编号为1行3列的数组,其中1列为xy和z。 I don't need anymore rows because this number is going to be constantly updated and I don't need memory of the previous point, so its just going to be constantly overridden
我不再需要任何行,因为该数字将不断更新,并且我不需要存储上一点,因此它将不断被覆盖
so here is my array 所以这是我的数组
int coordinates[1][3] = {1,1,1};
and this is how I am trying to see if I built it correct and that the values print out. 这就是我试图查看构建的是否正确以及输出的值的方式。
printf("%d %d %d\n", coordinates[1][1],coordinates[1][2],coordinates[1][3] );
printf("\n");
My thinking that it will print Row 1 column 1, Row 1 Column 2, and row 1 column 3. 我认为它将打印第1行第1列,第1行第2列和第1行第3列。
What is wrong with my thinking here? 我在这里的想法有什么问题? I'm not getting
1 1 1
as my result. 我没有得到
1 1 1
作为结果。 I'm getting random numbers. 我正在获取随机数。
I'm not too familiar with C. This seem trivial and I'm starting to spin my wheels too much on it. 我对C不太熟悉。这似乎微不足道,并且我开始在其上旋转太多。
EDIT: MY PROCESS OF WORKING THINGS(Also see comment) 编辑:我的工作流程(另请参阅评论)
"This was the first thing I tried. Thinking back to arrays 101 and indexing is n-1 because It seemed like I was getting elements in random memory address outside the arrays bounds. Still did not work unfortunately im starting to beat my head against a wall." “这是我尝试的第一件事。回想一下数组101,索引为n-1,因为似乎我在数组边界之外的随机内存地址中获取了元素。不幸的是,仍然无法正常工作,我开始为反对壁。”
I did try 我确实尝试过
printf("%d %d %d\n", coordinates[0][0],coordinates[0][1],coordinates[0][2] );
printf("\n");
but again i got random numbers 但是我又得到了随机数
CONCLUSION 结论
Ok I finally figured out what I was doing wrong. 好的,我终于弄清楚我在做什么错。 You all were right.
你们都是对的。 I was looking in the wrong spot of my code.
我在寻找错误的代码。 Thank you for your prompt responses.
感谢您的及时答复。 What was happening was I Originally called my coordinates as a float in my code.
发生的事情是我最初将我的坐标称为代码中的浮点数。 I didnt copy and past it here so I called it an int here.
我没有在此处复制和粘贴它,因此在这里将其称为int。 SO when I was trying to printf with %d it was trying to print an int for a float causing the messed up results.
因此,当我尝试使用%d进行printf时,它试图为浮点数打印一个int值,从而导致结果混乱。 I switched the %d to %f (which i was unaware how the printf worked like that i guess) and it worked!.
我将%d切换为%f(我不知道我想不知道printf是如何工作的),它确实有效! Green horn mistake and im sorry for it!
绿角错误,对此我深感抱歉! Thanks again for the help!
再次感谢您的帮助!
Remember that array index starts from 0 in C, this rule applies to multiple-dimensional arrays as well. 请记住,数组索引从C中的0开始,此规则也适用于多维数组。
For an array int coordinates[1][3]
, its elements are coordinates[0][0]
, coordinates[0][1]
and coordinates[0][2]
. 对于一个数组为
int coordinates[1][3]
的数组,其元素为coordinates[0][0]
, coordinates[0][1]
和coordinates[0][2]
。
It should be like this 应该是这样
printf("%d %d %d\n", coordinates[0][0],coordinates[0][1],coordinates[0][2] );
As the index starts from zero not one. 由于索引从零开始而不是一。
it should be indexed as cordinates[0][0], cordinates[0][1], cordinates[0][2] because array indexing usually starts at 0. 由于数组索引通常从0开始,因此应将其索引为cordinates [0] [0],cordinates [0] [1],coordinates [0] [2]。
here cordinates[1][3], means row size is one but it is indexed using 0. similary columns are indexed from 0 to 3. ie 0 to size-1 这里的cordinates [1] [3]表示行大小为1,但使用0进行了索引。相似列的索引从0到3。即0到size-1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.