数组不初始化。每个元素包含垃圾,但不要求值
问题描述:
第一个问题:0错误,0警告。确保代码是正确的。告诉我什么是错的? (这是程序的一部分。)我不明白什么是错的。至少它会显示
array[3][3] = {{1,1,1},{1,1,1},{1,1,1}}
数组不初始化。每个元素包含垃圾,但不要求值第二个问题:但不是'零的我看清楚的领域。 (我什么都看不到)但是如果有
{{1,1,1},{1,1,1},{1,1,1}}
,我看到'1's ...告诉我为什么?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define L 3 /* Including the [0]-element */
//#define C 3 /* Including the [0]-element */
#define RANGE 100 /* Set up the range of random values */
int fill_in(int *, int, int); /* Prototype of function */
int main()
{
int L = 3, C = 3;
int array[L][C]; // L - Line, C - Column
int i, j; // Global variables
int * aPtr;
aPtr = &array[L][C];
srand((unsigned)time(NULL));
fill_in(aPtr, L - 1, C - 1);
/* Displaying array (AFTER) */ // <--- going to make a function, but my first one
printf("\nAFTER:\n"); // doesn't work correctly =(
for (i = 0; i <= L - 1; i++)
{
for (j = 0; j <= C - 1; j++)
printf("%2.d ", array[i][j]);
printf("\n");
}
return 0;
}
int fill_in(int * aptr, int m, int n) /// PROBLEM? O_о
{
int arr[m][n];
int i, j; // Local variables
/* Filling array with random values */
for (i = 0; i <= m - 1; i++)
{
for (j = 0; j <= n - 1; j++)
arr[i][j] = 1; //1 + rand()%RANGE; // If each element == 1, it works!
}
return arr[i][j];
}
更新: 我已经解决了! 解释是在代码的评论。 下面的代码工作正常:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Lines 2 /* Including the [0]-element */
#define Columns 2 /* Including the [0]-element */
#define RANGE 100 /* Set up the range of random values */
int fill_in(int*, int, int); /* Prototypes of functions */
int display(int*, int, int);
int main()
{
int L = Lines, C = Columns;
int array[L][C]; // L - Line, C - Column
int* aPtr;
aPtr = &array[0][0];
srand((unsigned)time(NULL));
fill_in(aPtr, L, C); /* Filling array with random values. */
display(aPtr, L, C); /* Displaying array. */
return 0;
}
////////////////////////////////////////////////////////////////////
/** Eureka! The fact is that pointer of a[0][0] is *ptr[0], and
the pointer of a[2][2] is *ptr[8] ---> The 8-th element of the array[2][2].
>>>> Pointer sees array[][] not as matrix (square), but as a line!
>>>> As if to make a line of a square matrix!
*/
int fill_in(int* aptr, int m, int n) /* Filling array with random values. */
{
int i; // Local variables
int max_number_of_element = ((m+1)*(n+1)-1);
for (i = 0; i <= max_number_of_element; i++)
*(aptr + i) = 1 + rand()%RANGE;
return *aptr;
}
int display(int* aptr, int m, int n) /* Displaying array. */
{
int i;
int count = 1;
int max_number_of_element = ((m+1)*(n+1)-1);
for (i = 0; i <= max_number_of_element; i++)
{
printf("%2.d ", *(aptr + i));
if (count % (n+1) == 0)
printf("\n");
count++;
}
return i;
}
相反的:
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
aptr[i * n + j] = 1;
我用:
for (i = 0; i <= max_number_of_element; i++)
*(aptr + i) = 1;
//Pointer sees array[][] not as matrix (square), but as a direct sequence (line).
不知道是否确实数组以外的任何东西,但主要问题解决了。
P.S. 让我知道我是否错了。
答
在fill_in
函数中,您不会对aptr
做任何事情。相反,您填写本地arr
阵列。做到这一点,而不是:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
aptr[i * n + j] = 1;
}
而且,这样的:
aPtr = &array[L][C];
需要对NE
aPtr = &array[0][0];
你想要的一个指针数组的第一个元素。
此外,而不是counter <= max - 1
,使用更简洁和传统的counter < max
表示法。
答
这
int * aPtr;
aPtr = &array[L][C];
你出数组边界的分配指针的位置是错误的。该数组在行和列上都是从0-2定义的,并且您正在为指针分配位置(3,3)。
除此之外,您不会将您作为参数传递给fill_in函数的指针做任何事情。
/*这是什么意思?不明白它做什么?*/ aptr [i * n + j] = 1;/*仅显示[0](1)*/ /*我试过:*/ aptr [i * j] = 1;/*显示[0] [0..2](1 1 1)(这比只有第一个元素要好)*/ – yulian 2013-03-19 12:23:48
@ user2186301数组在内存中连续存在。现在是时候开启一个关于数组和指针的好指南。 http://c-faq.com – 2013-03-19 12:25:34