我有一个数组:array[3][3]
只要数组未满,我会让用户将数据输入到数组中。一旦数组变满,我想阻止用户向其中插入更多数据。
C 没有数组边界检查。你必须自己做。使用变量来跟踪您插入了多少项目,并在您的计数器等于数组大小时停止。
您无法检查“数组是否已满”。要做你想做的事,在向数组添加元素时跟踪索引。
您需要跟踪用户插入的数据。当您的计数器达到数组的大小时,数组已满。:D C 中没有其他方法可以实现此结果,因为它不提供任何方法来验证数组中有多少元素。
引入一个变量来计算填充的单元格的数量。每当您向数组添加/删除数据时,您都会调整此变量。然后,为了检查您的数组是否已满,只需检查此变量是否等于数组中的单元格总数。
我认为最简单的方法是使用 calloc() 动态分配内存,您可以在其中将数组元素初始化为例如零。您可以通过检查数组中的最后一个元素是否仍为零来检查数组是否已满。当然,如果最后一个元素仍然为零,则数组未满。
If possible, you could initialize all elements in the array to a certain value that your program would otherwise consider "illegal" or "invalid". E.g. an array of positive numbers can be initialized to be all -1's. Or an array of chars can be initialized to be all NULL characters. Then, just look at the last element if it is set to the default value.
measurement = sizeof(myarray) / sizeof(element); //or just a constant
while(myarray[measurement-1] == defaultvalue){
//insert code here...
}
首先,这不是一个数组,而是一个矩阵(又名数组数组)。您已经知道矩阵的尺寸为 3x3,然后您可以执行以下操作:
int x, y;
int array[3][3];
for (x = 0; x<2; x++)
{
for (y = 0; y<2; y++)
{
//I assume that it is an array of int
printf("Insert number at %d - %d" ,x,y);
scanf("%d" ,&array[x][y]);
}
}
现在用户只能插入 3*3=9 个值。
使用检查所需向量的最大长度的 getter/setter 函数将行为封装到结构中:
typedef varvector
varvector;
struct varvector {
int length;
void* vector;
};
varvector* varvector_create(int length) {
varvector* container = malloc(sizeof(varvector));
void* vector = malloc(length);
if(container && vector) {
container->vector = vector;
}
return(container);
}
void varvector_destroy(varvector* container) {
free(container.vector);
free(container);
}
varvector_get(varvector* container, int position) {
if(position < container.length) {
return(container->vector[position]);
}
}
varvector_set(varvector* container, int position, char value) {
if(position < container.length) {
container->vector[position] = value
}
}
面向对象编程是一种设计模式,它恰好在某些语言中具有句法支持,而在C
.
这并不意味着你不能在你的工作中使用这种设计模式,它只是意味着你必须使用已经为你提供的库(glib
,ooc
),或者如果你只需要这些功能的一小部分,编写你自己的基本功能。
在 C 中没有办法进行数组绑定检查,但是通过更好的编码实践,我们检查数组是否已满。
例如,考虑在您的数组 a[3][3] 中您不希望具有某些特定值。这个值可以是任何东西!0xFF 或 0 或整数范围内的任何值!并且您必须确保该值永远不会作为数组的输入,然后您可以验证您的数组 a[3][3] 是否已满!
/*part of coed in main*/
/*here initialize the array with 0, assuming that 0 will never be a part of array a[3][3]*/
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
a[i][j] = 0; // assuming that 0 will never be a part of array a[3][3]
}
}
while(CheckArray(**a)!=0)
{
printf("give array input:\n")
scanf("%d", &a[row][column]); //writing to empty cell of an array
}
//CheckArray code
int CheckArray(int a[][])
{
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
if(a[i][j] == 0) // assuming that 0 will never be a part of array a[3][3]
{
row = i; // row and column must be global variables in this example!
column = j; // row and column must be global variables in this example!
return 1;
}
else
{
// no need to do anything here
}
}
}
//if code reaches here then array is full!
printf("Array is full.\n");
return 0;
}
你仍然可以优化上面的代码!这只是通过更好的编码练习检查数组是否已满的一种方法!
您可以假设数组的最后一个元素具有id=0
.
然后在函数中add
检查是否存在带有id=0
.
int add(char *source, char *target, int size) {
int index = 0;
for (int i = 0; i < size; i++) {
if (target[i].id == 0) {
index = i;
break;
}
}
if (index >= 0 && index < size) {
if (index < size - 1) target[index + 1].id = 0;
// check that element with id=0 is the last in the array
//write code to add your element here
return index;
}
}