Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用类型为struct且返回指针的函数未运行_C_Arrays_Function_Pointers_Struct - Fatal编程技术网

调用类型为struct且返回指针的函数未运行

调用类型为struct且返回指针的函数未运行,c,arrays,function,pointers,struct,C,Arrays,Function,Pointers,Struct,我在运行程序时遇到问题,代码块中没有出现警告或错误。当我创建指针类型的ArrayList结构并尝试使用malloc动态分配内存时,麻烦就来了。我使用的语法是“->”。我已经做了几个小时了,没有真正的线索 #include <stdio.h> #include <string.h> #define DEFAULT_INIT_LEN 10//this is in another header file typedef struct ArrayList{//this is a

我在运行程序时遇到问题,代码块中没有出现警告或错误。当我创建指针类型的ArrayList结构并尝试使用malloc动态分配内存时,麻烦就来了。我使用的语法是“->”。我已经做了几个小时了,没有真正的线索

#include <stdio.h>
#include <string.h>
#define DEFAULT_INIT_LEN 10//this is in another header file

typedef struct ArrayList{//this is also in another header file
    char **array;
    int size;
    int capacity;
} ArrayList;

int main(void){
    ArrayList *test=createArrayList(12);
    printf("size: %d capacity: %d\n", test->size, test->capacity);
}

ArrayList *createArrayList(int length){
    int i=0;//index variables
    ArrayList *r;
    if (length>DEFAULT_INIT_LEN){
        r->array=malloc(sizeof(char)*(length+1));//create memory for internal array
        r->capacity=length;
        r->size=0;
        for (i=0; i<length; i++)//sets members in the array to NULL
            r->array[i]=NULL;
        printf("Created new ArrayList of size %d\n", length);
    }
    else{
        r->array=malloc(sizeof(char)*(DEFAULT_INIT_LEN+1));//create memory for internal    array
        r->capacity=DEFAULT_INIT_LEN;
        r->size=0;
        for (i=0; i<DEFAULT_INIT_LEN; i++)//sets members in the array to NULL
            r->array[i]=NULL;
        printf("Created new ArrayList of size %d", DEFAULT_INIT_LEN);
    }
    return r;
}
#包括
#包括
#定义DEFAULT_INIT_LEN 10//这在另一个头文件中
typedef struct ArrayList{//这也在另一个头文件中
字符**数组;
整数大小;
国际能力;
}ArrayList;
内部主(空){
ArrayList*test=createArrayList(12);
printf(“大小:%d容量:%d\n”,测试->大小,测试->容量);
}
ArrayList*createArrayList(整数长度){
int i=0;//索引变量
ArrayList*r;
如果(长度>默认初始长度){
r->array=malloc(sizeof(char)*(length+1));//为内部数组创建内存
r->容量=长度;
r->size=0;
对于(i=0;iarray[i]=NULL;
printf(“已创建大小为%d的新ArrayList\n”,长度);
}
否则{
r->array=malloc(sizeof(char)*(默认值_INIT_LEN+1));//为内部数组创建内存
r->capacity=默认初始长度;
r->size=0;
对于(i=0;iarray[i]=NULL;
printf(“已创建大小为%d的新ArrayList”,默认值为\u INIT\u LEN);
}
返回r;
}

必须首先初始化Arraylist*r:

ArrayList *createArrayList(int length){
int i=0;//index variables
ArrayList *r=malloc(sizeof(ArrayList);

必须首先初始化Arraylist*r:

ArrayList *createArrayList(int length){
int i=0;//index variables
ArrayList *r=malloc(sizeof(ArrayList);

必须首先初始化Arraylist*r:

ArrayList *createArrayList(int length){
int i=0;//index variables
ArrayList *r=malloc(sizeof(ArrayList);

必须首先初始化Arraylist*r:

ArrayList *createArrayList(int length){
int i=0;//index variables
ArrayList *r=malloc(sizeof(ArrayList);
您可能不想分配
length+1
元素,因为您有
r->capacity

您可能不想分配
length+1
元素,因为您有
r->capacity

您可能不想分配
length+1
元素,因为您有
r->capacity



您可能不想分配
length+1
元素,因为您有
r->capacity

我不熟悉c编程中的新功能。我试过了,但它不起作用,可能您把它和java混淆了。@abdolah:c没有
new
关键字。因此-1。@abdolah:coups;)但是您也没有强制转换
malloc
的结果。看一看。我不熟悉c语言中的新编程。我试过了,但它不起作用,可能您把它和java混淆了。@abdolah:c没有
new
关键字。因此-1。@abdolah:coccesss;)但是您也没有强制转换
malloc
的结果。看一看,我不熟悉c语言编程的新功能。我试过了,但没用,也许你把它和java混淆了。@abdolah:C没有
new
关键字。因此-1。@abdolah:发生;)但是您也不会强制转换
malloc
的结果。看一看,我不熟悉c语言编程的新功能。我试过了,但没用,也许你把它和java混淆了。@abdolah:C没有
new
关键字。因此-1。@abdolah:发生;)但是您也不会强制转换
malloc
的结果。看看。顺便说一句,
malloc(sizeof(char)*(length+1))中的
sizeof(char)
是错误的。谢谢!我应该用这个approach@user3125367它没有错,只是没用,因为它的值是每个定义的
1
。@glglgl比较长度数组的大小
length
和以这种方式分配的内存块的大小。顺便说一句,
sizeof(char)
中的
malloc(sizeof(char)*(length+1))
是错误的。谢谢!我应该用这个approach@user3125367它没有错,只是没用,因为它的值是每个定义的
1
。@glglgl比较长度数组的大小
length
和以这种方式分配的内存块的大小。顺便说一句,
sizeof(char)
中的
malloc(sizeof(char)*(length+1))
是错误的。谢谢!我应该用这个approach@user3125367它没有错,只是没用,因为它的值是每个定义的
1
。@glglgl比较长度数组的大小
length
和以这种方式分配的内存块的大小。顺便说一句,
sizeof(char)
中的
malloc(sizeof(char)*(length+1))
是错误的。谢谢!我应该用这个approach@user3125367这并没有错,只是没用,因为每个定义的值都是
1
。@glglgl比较length
length
数组的大小和以这种方式分配的内存块的大小。