使用 strdup 进入 malloc 保留空间

发布于 2024-08-24 08:32:16 字数 492 浏览 2 评论 0原文

我从未使用过 malloc 来存储多个值,但我必须使用 strdup 来对输入文件的行进行排序,但我没有办法让它工作。

我虽然使用 strdup() 来获取指向每一行的指针,然后根据使用 malloc() 保留的行数将每个行放入一个空间中。

我不知道是否必须像保留内存是指向指针的数组一样这样做,我的意思是使用 char** ,然后将每个指向每个 strdup 的指针放入保留空间。

我想是这样的:

char **buffer;
char *pointertostring;
char *line; // line got using fgets

*buffer = (char*)malloc(sizeof(char*));
pointertostring = strdup(line);

我不知道之后要做什么,我什至不知道这是否正确,在这种情况下,我应该做什么来将指向字符串的指针存储在缓冲区的位置?

问候

I've never used malloc to store more than values but I have to use strdup to order the lines of an input file and I dont get a way to make it work.

I though using strdup() to get a pointer to each line and later, put each one into a space according to the number of lines reserved with malloc().

I dont know if I have to do it like reserved memory was an array to pointers, I mean using char** and later put each pointer to each strdup into reserved space.

I though something like this:

char **buffer;
char *pointertostring;
char *line; // line got using fgets

*buffer = (char*)malloc(sizeof(char*));
pointertostring = strdup(line);

I don't know what to do after that, I don't even know if this is correct, in that case, what should I do to store the pointer to the string in a position of buffer?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2024-08-31 08:32:16

如果我正确理解你的要求。你必须做类似的事情:

char **buffer; 
char line[MAX_LINE_LEN]; // line got using fgets
int count; // to keep track of line number.    

// allocate one char pointer for each line in the file.
buffer = (char**)malloc(sizeof(char*) * MAX_LINES); 

count = 0; // initilize count.

// iterate till there are lines in the file...read the line using fgets.
while(fgets(line,MAX_LINE_LEN,stdin)) {
    // copy the line using strdup and make the buffer pointer number 'count' 
    // point to it
    buffer[count++] = strdup(line);
}
....
....
// once done using the memory you need to free it.
for(count=0;count<MAX_LINES;count++) {
     free(buffer[count]);
}
....
....

If I understand your requirement correctly. You'll have to do something like:

char **buffer; 
char line[MAX_LINE_LEN]; // line got using fgets
int count; // to keep track of line number.    

// allocate one char pointer for each line in the file.
buffer = (char**)malloc(sizeof(char*) * MAX_LINES); 

count = 0; // initilize count.

// iterate till there are lines in the file...read the line using fgets.
while(fgets(line,MAX_LINE_LEN,stdin)) {
    // copy the line using strdup and make the buffer pointer number 'count' 
    // point to it
    buffer[count++] = strdup(line);
}
....
....
// once done using the memory you need to free it.
for(count=0;count<MAX_LINES;count++) {
     free(buffer[count]);
}
....
....
蒲公英的约定 2024-08-31 08:32:16

你的缓冲区将只保存一个指针。你需要类似的东西:

   char **buffer;
   char *pString;
   int linecount;

   buffer = (char **)malloc(sizeof(char *)*MAXIMUM_LINES);
   linecount = 0;

   while (linecount < MAXIMUM_LINES) {
      pString = fgets(...);
      buffer[linecount++] = strdup(pString);
   }

Your buffer will only hold one pointer. You need something like:

   char **buffer;
   char *pString;
   int linecount;

   buffer = (char **)malloc(sizeof(char *)*MAXIMUM_LINES);
   linecount = 0;

   while (linecount < MAXIMUM_LINES) {
      pString = fgets(...);
      buffer[linecount++] = strdup(pString);
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文