添加和打印由指向结构的指针控制的 ** 指针的元素
adding and printing elements of ** pointer that is controlled by a pointer to a struct
我有一个 SmartArray 的 struct typedef,它有一个变量 char ** 数组。我已经尝试调试代码几个小时,并且取得了很大的进步。但是,我坚持这个特定的错误。我有一个函数可以打印出所述数组。它会打印两次,然后第三次它根本不打印!我觉得这与我将 malloc 添加到数组的方式有关,因为一个数组不能正确打印出来。对于数组的最后一部分,它打印 "Testing Na"。有任何想法吗?我将不胜感激。
我怀疑这是函数的一部分,但是,我似乎找不到它://allocate min space for string
printf("approaching malloc\n");
strPtr = malloc( sizeof(char) * (strlen(str) + 1) );
if(strPtr == NULL)
{
return NULL;
}
printf("made it past malloc!\n");
strcpy(strPtr, str);
//if crash probably this code
smarty->array[index] = strPtr;
if(smarty->array[0] == NULL)
{
return NULL;
}
return strPtr;
这是我的测试代码:
typedef struct SmartArray
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;
// Size of array (i.e., number of elements that have been added to the array)
int size;
// Length of the array (i.e., the array's current maximum capacity)
int capacity;
} SmartArray;
int main(void)
{
int i; char buffer[32];
SmartArray *smarty1 = createSmartArray(-1);
printf("Array created\n");
// Print the contents of smarty1.
printf("\n-- SMART ARRAY 1: --\n");
printSmartArray(smarty1);
printf("Made it past print!\n");
put(smarty1,"Hi, my name is ");
put(smarty1, "Hello, my name is");
put(smarty1, "Testing Names");
printf("made it past put!\n");
printf("smart away is now\n");
printSmartArray(smarty1);
printf("end of main!\n");
我觉得这是很明显的事情,我只是忽略了,因为我是新手。
这是我试图让它在内存中看起来像的图片:
click here for memory diagram
更新:我弄明白了为什么它没有打印所有的名字,但是程序在打印函数之后出现段错误。
我认为这是因为您正在尝试使用 malloc
扩展您的数组。 C数组一次只能指向一个存储块。当您使用 malloc
时,它将分配一个全新的存储块,并且您在编写 smarty->array[index] = strPtr
.
时试图将其添加到数组的末尾
如果您想扩展 C 数组的大小,请改用 realloc
,这将为您的数组分配一个更大的新内存块,并将现有内容也复制到其中。
如果那不能解决问题,你能 post 你的整个 put
功能吗?
我有一个 SmartArray 的 struct typedef,它有一个变量 char ** 数组。我已经尝试调试代码几个小时,并且取得了很大的进步。但是,我坚持这个特定的错误。我有一个函数可以打印出所述数组。它会打印两次,然后第三次它根本不打印!我觉得这与我将 malloc 添加到数组的方式有关,因为一个数组不能正确打印出来。对于数组的最后一部分,它打印 "Testing Na"。有任何想法吗?我将不胜感激。
我怀疑这是函数的一部分,但是,我似乎找不到它://allocate min space for string
printf("approaching malloc\n");
strPtr = malloc( sizeof(char) * (strlen(str) + 1) );
if(strPtr == NULL)
{
return NULL;
}
printf("made it past malloc!\n");
strcpy(strPtr, str);
//if crash probably this code
smarty->array[index] = strPtr;
if(smarty->array[0] == NULL)
{
return NULL;
}
return strPtr;
这是我的测试代码:
typedef struct SmartArray
{
// We will store an array of strings (i.e., an array of char arrays)
char **array;
// Size of array (i.e., number of elements that have been added to the array)
int size;
// Length of the array (i.e., the array's current maximum capacity)
int capacity;
} SmartArray;
int main(void)
{
int i; char buffer[32];
SmartArray *smarty1 = createSmartArray(-1);
printf("Array created\n");
// Print the contents of smarty1.
printf("\n-- SMART ARRAY 1: --\n");
printSmartArray(smarty1);
printf("Made it past print!\n");
put(smarty1,"Hi, my name is ");
put(smarty1, "Hello, my name is");
put(smarty1, "Testing Names");
printf("made it past put!\n");
printf("smart away is now\n");
printSmartArray(smarty1);
printf("end of main!\n");
我觉得这是很明显的事情,我只是忽略了,因为我是新手。
这是我试图让它在内存中看起来像的图片: click here for memory diagram
更新:我弄明白了为什么它没有打印所有的名字,但是程序在打印函数之后出现段错误。
我认为这是因为您正在尝试使用 malloc
扩展您的数组。 C数组一次只能指向一个存储块。当您使用 malloc
时,它将分配一个全新的存储块,并且您在编写 smarty->array[index] = strPtr
.
如果您想扩展 C 数组的大小,请改用 realloc
,这将为您的数组分配一个更大的新内存块,并将现有内容也复制到其中。
如果那不能解决问题,你能 post 你的整个 put
功能吗?