C字符串内存分配,C字符串表
我想创建一个动态分配的c字符串表,但我想我对这个主题不是很了解,你能给我解释一下还是更正我的代码C字符串内存分配,C字符串表,c,cstring,dynamic-allocation,C,Cstring,Dynamic Allocation,我想创建一个动态分配的c字符串表,但我想我对这个主题不是很了解,你能给我解释一下还是更正我的代码 #include <stdio.h> int main() { int i; char** t; t=(char**)malloc(16*sizeof(char*)); for(i<0;i<100;i++) { *t[i]=(char*)malloc(16*sizeof(char));
#include <stdio.h>
int main()
{
int i;
char** t;
t=(char**)malloc(16*sizeof(char*));
for(i<0;i<100;i++)
{
*t[i]=(char*)malloc(16*sizeof(char));
}
return 0;
}
#包括
int main()
{
int i;
字符**t;
t=(char**)malloc(16*sizeof(char*);
对于(i您的代码可以进行更正,以便它可以创建100X16个字符的表或具有16个字符长度的100个字符串的表
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int noOfStrings = 100;
int eachStringLenght = 16;
char** t;
t=(char**)malloc(noOfStrings*sizeof(char*));
for(i=0;i<noOfStrings;i++)
{
t[i]=(char*)malloc(eachStringLenght*sizeof(char));
}
return 0;
}
#包括
#包括
int main()
{
int i;
int noOfStrings=100;
int-eachStringLenght=16;
字符**t;
t=(char**)malloc(noOfStrings*sizeof(char*);
对于(i=0;i#包括
#包括
#包括
#包括
int main(int I__argC,char*I__argV[]))
{
int rCode=0;
int i;
char**t=NULL;
大小排列;
/*解析命令行参数*/
如果(2!=I__argC)
{
rCode=EINVAL;
printf(“用法:%s{dynamic array size}\n”,I__argV[0]);
去清理;
}
arraySize=strtoul(I__argV[1],NULL,0);
如果(0==阵列化)
{
rCode=EINVAL;
fprintf(stderr,“无法分配大小为零的动态数组。\n”);
去清理;
}
/*分配字符串指针的动态数组*/
errno=0;
t=malloc(数组化*sizeof(*t));
if(NULL==t)
{
rCode=errno?errno:ENOMEM;
fprintf(stderr,“malloc()失败。\n”);
去清理;
}
memset(t,0,arraySize*sizeof(*t));
/*使用动态分配的字符串初始化每个指针*/
对于(i=0;i,您应该使用变量来实现您想要的一切。例如,您为char*类型的16个元素分配了一个内存,因此,您有一个包含16个元素的数组,但随后您将从0变为100?为什么
#include <stdio.h>
int main()
{
int n = 16;
int m = 16;
// Allocate a memory for n elements of type char*
char** t = (char**)malloc(n * sizeof(char*));
for(int i = 0; i < n; ++i)
{
// For each element in array t - initialize with another array.
// Allocate m elements of type char
t[i] = (char*)malloc(m * sizeof(char));
// Initialize
for(int j = 0; j < m; ++j)
t[i][m] = 'a';
}
// Print
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
printf("%c ", t[i][m]);
printf("\n");
}
// Free allocated memory!
for(int i = 0; i < n; ++i)
free(t[i]);
free(t);
return 0;
}
#包括
int main()
{
int n=16;
int m=16;
//为char类型的n个元素分配内存*
char**t=(char**)malloc(n*sizeof(char*));
对于(int i=0;i
因为n应该是100,我没有替换它:)如果malloc()失败,那么它会将errno设置为ENOMEM,所以赋值errno=ENOMEM;
没有多大意义。@MartinR,好眼力!谢谢。(修复)@MartinR malloc()不需要设置errno
。如果有任何故障,它都会设置errno
,具体取决于实现。@BlueMoon:我从OS X上的“man malloc”和:“……否则,它将返回空指针并设置errno以指示错误。”@MartinR这是POSIX行为,不是ISO C。我说不需要,也就是说,设置与否是实现的选择,不能依赖。您需要#包括,以安全地调用malloc
;编译器应该对此发出警告。如果您强制转换malloc,某些编译器会抑制此警告,这是原因之一从20世纪70年代开始,人们就开始不假思索地复制粘贴。
#include <stdio.h>
int main()
{
int n = 16;
int m = 16;
// Allocate a memory for n elements of type char*
char** t = (char**)malloc(n * sizeof(char*));
for(int i = 0; i < n; ++i)
{
// For each element in array t - initialize with another array.
// Allocate m elements of type char
t[i] = (char*)malloc(m * sizeof(char));
// Initialize
for(int j = 0; j < m; ++j)
t[i][m] = 'a';
}
// Print
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
printf("%c ", t[i][m]);
printf("\n");
}
// Free allocated memory!
for(int i = 0; i < n; ++i)
free(t[i]);
free(t);
return 0;
}