Arrays Realloc结构数组的第一个元素-旧大小无效;(*指针+;0)的问题

Arrays Realloc结构数组的第一个元素-旧大小无效;(*指针+;0)的问题,arrays,c,pointers,struct,realloc,Arrays,C,Pointers,Struct,Realloc,我有一个数组结构sth #define NUM_ELEM 8 typedef struct sth { int* tab; int lenght; } sth; ... int main(void) { sth* arr; function_allocating_memory_and_values(&arr); ... return 0; } 代码中还有一些函数,它们将指针指向指向结构数组的指针,并在其中修改数组的元素 void

我有一个数组
结构sth

#define NUM_ELEM 8


typedef struct sth {
    int* tab;
    int lenght;
} sth;

...

int main(void) {
    sth* arr;
    function_allocating_memory_and_values(&arr);
    ...
    return 0;
}
代码中还有一些函数,它们将指针指向指向结构数组的指针,并在其中修改数组的元素

void minor_function(*sth element) {
    //Changes something
}
在函数内部,我必须将指针传递给第I个元素。在这里,我提供了两个我尝试过的选项,并给出了一些评论:

void major_function(sth** array) {
     int index = 0;  //the value changes later
     ...
     minor_function(array[index]); //the argument was passed to the function without errors, but nothing happened; during debugging, I have seen also segmentation fault;
     minor_function( (*array + index)); //It worked
}
我相信,
(*array+index)
是指向索引th元素的指针。
(*array)[index]
是第th个元素的索引

1)如果是双指针,
array[index]
是什么?为什么它不起作用?

重新分配的相关问题: 在
主功能中,有一个减小结构大小的功能

void clear(sth* a) { //free would clear more properly in terms of nomenclature, but I want to reuse the pointer
    a->tab = realloc(a->tab, NUM_ELEM*sizeof(int));
    a->tab[0] = 0;
    a->length = 1;
}
它没有返回任何问题-直到我将数组的第一个元素索引0传递给函数:

(*array + index) == (*array + 0) == (*array) //It isn't the code from the program, it is an illuatration
然后我得到一个错误:

realloc(): invalid old size
Aborted (core dumped)
我本以为,*数组将指示数组的第一个元素,但是,它不会发生

  • (*指针)
    有什么问题
  • 3)如何重新分配第一个元素?

    我非常感谢你的回答和解释

    编辑

    我做了一些额外的研究和调试

    以下是分配代码:

    #define NUMBERS 45
    
    void stworz_zmienne(sth** arr) {
        *arr = realloc(*arr, (NUMBERS+3)*(sizeof(sth)));
        for (int i = 0; i<=NUMBERS+3; i++) {
            sth cell;
            cell.tab = NULL;
            cell.tab = realloc(kom.komorka, sizeof(int));
            cell.length = 1;
            cell.tab[0] = 0;
            (*arr)[i] = cell;
        }
        (*arr)[NUMBERS+1].cell[0] = -1;
        (*arr)[NUMBERS+2].cell[0] = 1;
    
    }
    
    我想第一个元素I没有正确分配给realloc,但我不知道原因是什么以及如何解决它。

    关于第一个问题:

    void major_function(sth** array) {
     int index = 0;  //the value changes later
     ...
     minor_function(array[index]); // fails because pointing to somewhere undefined or not allocated in your case. 
     // Only array[0] could eventually be use because it is ‘equivalent’ to *array
     // nut here it would a bad idea
    

    array是指向(指针或数组)的指针,因此*array可以看作是一个数组,用于访问诸如(*array)[index]或最终的'*array+index'之类的元素。在c语言中的后一种情况下,指针或数组上的+运算将随着数组元素的大小或指针指向的对象而递增。

    array[index]
    (*array+index)
    不同。我认为您需要学习指针。post实际代码“(*array+index)是指向第th个元素的指针。(*array)[index]”,在这里使用
    *(*array+index)
    ,那么这将是一个正确的语句。另外,需要了解
    函数如何分配内存和值(&arr)
    已定义。请编辑帖子以添加一个。请查看感谢您的建议;我会记住您关于指针的信息。但是,请您解释一下,为什么我的问题被否决?在我看来,这表明我试图解决问题。感谢您的回答。我不知道,那个数组[索引]在这种情况下,表示未定义的内容
    void major_function(sth** array) {
     int index = 0;  //the value changes later
     ...
     minor_function(array[index]); // fails because pointing to somewhere undefined or not allocated in your case. 
     // Only array[0] could eventually be use because it is ‘equivalent’ to *array
     // nut here it would a bad idea