下面的代碼以下列方式作業:它基本上從stdin使用一個名為 的函式讀取每個字符_getchar,將它們分配到一個陣列中,最終回傳它 if c =! EOF。
我只想知道(*lineptr)[n_read] = '\0';下面代碼中的陳述句在做什么:
#include <unistd.h>
#include <stdlib.h>
int _getchar(void)
{
int rd;
char buff[2];
rd = read(STDIN_FILENO, buff, 1);
if (rd == 0)
return (EOF);
if (rd == -1)
exit(99);
return (*buff);
}
ssize_t _getline(char **lineptr, size_t *n, FILE *stream)
{
char *temp;
const size_t n_alloc = 120;
size_t n_read = 0;
size_t n_realloc;
int c;
if (lineptr == NULL || n == NULL || stream == NULL)
return (-1);
if (*lineptr == NULL)
{
*lineptr = malloc(n_alloc);
if (*lineptr == NULL)
return (-1);
*n = n_alloc;
}
while ((c = _getchar()) != EOF)
{
if (n_read >= *n)
{
n_realloc = *n n_alloc;
temp = realloc(*lineptr, n_realloc 1);
if (temp == NULL)
return (-1);
*lineptr = temp;
*n = n_realloc;
}
n_read ;
(*lineptr)[n_read - 1] = (char) c;
if (c == '\n')
break;
}
if (c == EOF)
return (-1);
(*lineptr)[n_read] = '\0';
return ((ssize_t) n_read);
}
uj5u.com熱心網友回復:
char **lineptr手段lineptr包含一個char指標的地址。
指標是包含地址的變數。所以通過寫*lineptr你得到那個地址。
在你的情況下,**lineptr< *(*lineptr)=> <=>(*lineptr)[0]
編輯:順便說一句,我沒有回答這個問題......指令(*lineptr)[n_read] = '\0'意味著你正在結束你的字串('\0'是 EOF(行尾)字符)。
uj5u.com熱心網友回復:
它們看起來一樣,但又不同。
拳頭一:
int (*ptr)[something];
定義指向int something元素陣列的指標
后者
(*ptr1)[something] //witohut type in front of it.
表示ptr1解參考指標,然后用 added 解參考先前解參考的結果something。
它相當于
*((*ptr1) something)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/372359.html
上一篇:為什么我的C函式不列印任何東西?
