我一直在試圖從書"The Practice of Programming"的章節接口中理解C語言中修訂後的csvgetline函數。我已經強調了對我來說沒有意義的代碼。無法理解從K&P書中的csvgetline.c函數中解釋的char **編程實踐
#include "csv.h"
enum { NOMEM = -2 }; /* out of memory signal */
static char* line = NULL; /* input chars */
static char* sline = NULL; /* line copy used by split */
static int maxline = 0; /* size of line[] and sline[] */
static char** field = NULL; /* field pointers */ // This is the problem!
static int maxfield = 0; /* size of field[] */
static int nfield = 0; /* number of fields in field[] */
static char fieldsep[] = ","; /* field separator chars */
static char* advquoted(char *);
static int split(void);
/* endofline: check for and consume \r, \n, \r\n, or EOF */
static int endofline(FILE *fin, int c)
{ … }
/* reset: set variables back to starting values */
static void reset(void)
{ … }
/* csvgetline: get one line, grow as needed */
/* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */
char* csvgetline(FILE *fin)
{
int i, c;
char *newl, *news;
if (line == NULL) { /* allocate on first call */
maxline = maxfield = 1;
line = (char *) malloc(maxline);
sline = (char *) malloc(maxline);
field = (char **) malloc(maxfield*sizeof(field)); // This is the problem!
if (line == NULL || sline == NULL || field == NULL) {
reset();
return NULL; /* out of memory */
}
}
for (i=0; (c=getc(fin))!=EOF && !endofline(fin,c); i++) {
if (i >= maxline-1) { /* grow line */
maxline *= 2; /* double current size */
newl = (char *) realloc(line, maxline);
if (newl == NULL) {
reset();
return NULL;
}
line = newl;
news = (char *) realloc(sline, maxline);
if (news == NULL) {
reset();
return NULL;
}
sline = news;
}
line[i] = c;
}
line[i] = '\0';
if (split() == NOMEM) {
reset();
return NULL; /* out of memory */
}
return (c == EOF && i == 0) ? NULL : line;
}
/* split: split line into fields */
static int split(void)
{
char *p, **newf;
char *sepp; /* pointer to temporary separator character */
int sepc; /* temporary separator character */
nfield = 0;
if (line[0] == '\0')
return 0;
strcpy(sline, line);
p = sline;
do {
if (nfield >= maxfield) {
maxfield *= 2; /* double current size */
newf = (char **) realloc(field, maxfield * sizeof(field[0]));
if (newf == NULL)
return NOMEM;
field = newf;
}
if (*p == '"')
sepp = advquoted(++p); /* skip initial quote */
else
sepp = p + strcspn(p, fieldsep);
sepc = sepp[0];
printf("%d", sepp[0]); // Debug
sepp[0] = '\0'; /* terminate field */
field[nfield++] = p;
p = sepp + 1;
} while (sepc == ',');
return nfield;
}
/* advquoted: quoted field; return pointer to next separator */
static char *advquoted(char *p)
{ … }
/* csvfield: return pointer to n-th field */
char* csvfield(int n)
{
if (n < 0 || n >= nfield)
return NULL;
return field[n]; // This is the problem!
}
/* csvnfield: return number of fields */
int csvnfield(void)
{
return nfield;
}
/* csvtest main: test CSV library */
int main(void)
{
int i;
char *line;
while ((line = csvgetline(stdin)) != NULL) {
printf("line = `%s'\n", line);
for (i = 0; i < csvnfield(); i++)
printf("field[%d] = `%s'\n", i, csvfield(i)); // This line is a problem
}
return 0;
}
我不能找出什麼是char**
字段我相信這是一個指向指向字符串/字符數組/字段的數組。 因此field[n]
應該包含指向字符串的指針,即字符串的地址,而不是字符串本身。但是,從printf()
聲明中訪問它時,函數csvfield(i)
返回值而不是指向第n個字段的指針,這似乎不是這種情況。
我瀏覽過很多試圖瞭解char **
的網站,但信息非常非常有限。一個網站說char *
是一個指向字符數組的指針,但是char**
是指向char的指針。另一個網站談到char **
類型是一個標量類型。但我仍然對這char **
模糊不清。
請問您能解釋一下嗎?
一本很棒的書,可以閱讀! –
所以......你問爲什麼'printf(「%s \ n」,x);'打印字符串,當'x'是'char *'時?相當肯定這是在書中更早的涵蓋:) –