在C中获取指针数组的第一个元素?
Get the first element of pointer array in C?
我一直在寻找如何做到这一点。到目前为止,我发现的方法还没有取得成功。
在那个页面中,有一个名为 fileman.c 的示例程序,特别是这个函数:
/* Execute a command line. */
int
execute_line (line)
char *line;
{
register int i;
COMMAND *command;
char *word;
/* Isolate the command word. */
i = 0;
while (line[i] && whitespace (line[i]))
i++;
word = line + i;
while (line[i] && !whitespace (line[i]))
i++;
if (line[i])
line[i++] = '[=10=]';
command = find_command (word);
if (!command)
{
fprintf (stderr, "%s: No such command for FileMan.\n", word);
return (-1);
}
/* Get argument to command, if any. */
while (whitespace (line[i]))
i++;
word = line + i;
/* Call the function. */
return ((*(command->func)) (word));
}
我想了解如何以能够检查其元素的方式访问 char *word?例如 - 如果我想检查 char *word 的第一个元素是 '#' 并基于此执行某些逻辑而不是当单词以其他任何内容开头时?我是 C 的新手,发现我已经习惯了很多其他语言的东西;咳嗽Python;在 C 中本质上更难。
谢谢!
天哪,那个古老的函数参数语法!
要检查指针数组的第一个元素,只需使用 ptr_name[0]
,其中 "ptr_name" 是指针数组的 name/identifier。
如果您要查看特定字符是否为 hash/pound 符号。简单地做:
if( word[0] == '#' ) // run code
我能够使用分词器解决这个问题。
/*Tokenizer, splits the arguments and detects presence of i/o or piping.*/
void Tokenize(char *sentence, char** StorageArray, char * delimiter)
{
char *token;
char *next_token;
int counter = 1, position = 0;
token = strtok(sentence, delimiter);
next_token = token;
while (next_token != NULL){
TokenCount++;
StorageArray[position] = token;
/*Check for input / output markers*/
if(strcmp(StorageArray[position], "<") == 0){ flagInput = 1;}
if(strcmp(StorageArray[position], ">") == 0){ flagOutput = 1;}
/*Check for pipeline instructions.*/
if(strcmp(StorageArray[position], "|") == 0){ flagPipeInst = 1;}
position++;
if((next_token = strtok(NULL, delimiter))){
token = next_token;
}
counter++;
}
counter--;
}
我在将内容传递到执行命令之前获得了所有令牌。
我一直在寻找如何做到这一点。到目前为止,我发现的方法还没有取得成功。
在那个页面中,有一个名为 fileman.c 的示例程序,特别是这个函数:
/* Execute a command line. */
int
execute_line (line)
char *line;
{
register int i;
COMMAND *command;
char *word;
/* Isolate the command word. */
i = 0;
while (line[i] && whitespace (line[i]))
i++;
word = line + i;
while (line[i] && !whitespace (line[i]))
i++;
if (line[i])
line[i++] = '[=10=]';
command = find_command (word);
if (!command)
{
fprintf (stderr, "%s: No such command for FileMan.\n", word);
return (-1);
}
/* Get argument to command, if any. */
while (whitespace (line[i]))
i++;
word = line + i;
/* Call the function. */
return ((*(command->func)) (word));
}
我想了解如何以能够检查其元素的方式访问 char *word?例如 - 如果我想检查 char *word 的第一个元素是 '#' 并基于此执行某些逻辑而不是当单词以其他任何内容开头时?我是 C 的新手,发现我已经习惯了很多其他语言的东西;咳嗽Python;在 C 中本质上更难。
谢谢!
天哪,那个古老的函数参数语法!
要检查指针数组的第一个元素,只需使用 ptr_name[0]
,其中 "ptr_name" 是指针数组的 name/identifier。
如果您要查看特定字符是否为 hash/pound 符号。简单地做:
if( word[0] == '#' ) // run code
我能够使用分词器解决这个问题。
/*Tokenizer, splits the arguments and detects presence of i/o or piping.*/
void Tokenize(char *sentence, char** StorageArray, char * delimiter)
{
char *token;
char *next_token;
int counter = 1, position = 0;
token = strtok(sentence, delimiter);
next_token = token;
while (next_token != NULL){
TokenCount++;
StorageArray[position] = token;
/*Check for input / output markers*/
if(strcmp(StorageArray[position], "<") == 0){ flagInput = 1;}
if(strcmp(StorageArray[position], ">") == 0){ flagOutput = 1;}
/*Check for pipeline instructions.*/
if(strcmp(StorageArray[position], "|") == 0){ flagPipeInst = 1;}
position++;
if((next_token = strtok(NULL, delimiter))){
token = next_token;
}
counter++;
}
counter--;
}
我在将内容传递到执行命令之前获得了所有令牌。