gpt4 book ai didi

c - C 中的锯齿状数组 - 一种更有效的方法?

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:31 25 4
gpt4 key购买 nike

我有一个函数,它从标准输入中获取一行并创建一个锯齿状数组,我将把它用于我正在构建的 shell。这个想法是我提示输入(如 shell)并读取输入的行(以及参数),然后我将调用 execvp(,) 使用 fork 来执行从标准输入输入的命令。我已经编写了一个函数来解析输入的字符串并将其分解为锯齿状数组,我正在寻找一个类似的函数来进行比较,或者寻求有关如何使该函数更高效的建议。这是代码:

int makearg( char *s, char ***args )
{
int numArgs = 0, i=0, j=0, rows=0, cols=0;

numArgs = getNumArgs( s );

// Allocate memory according to number of arguments +1
(*args) = (char**)calloc( (numArgs+1), sizeof(char*) );

while( *(s+i) != '\0' ) {
// if a space is found, the end of a word is found: copy to ragged array
if( *(s+i) == ' ' || *(s+i) == '\t' ) {
// Allocate memory for word
(*args)[rows] = (char*)malloc( (j+1)*sizeof( char ) );
// set j-index to the beginning of the word
j = i-j;
// continue until the end of the word ( i ) is reached
while( j < i ) {// copy characters one at a time
(*args)[rows][cols] = *(s+j);
j++;
cols++;
}
// add a \0 terminator to the word
(*args)[rows][cols] = '\0';
// reset j - tracks word length
j = 0;
// move to next row
rows++;
// move to beginning column of that row
cols = 0;
while( *(s+i+1) == ' ' )
i++;
}
else {
// if a word end was reached we dont want j incremented
j++;
}
// continue moving throught the string
i++;
}
// When the while loop above finds the \0 at the end of the
// string it exits the loop, with one more word to be added.
// Add the final word to the ragged array here - same logic.
(*args)[rows] = (char*)calloc( (j+1), sizeof( char ) );
j = i-j;
while( j < i ) {
(*args)[rows][cols] = s[j];
j++;
cols++;
}
(*args)[rows][cols] = '\0';
rows++;
cols = 0;
// here, we add the single element to the final row of
// the ragged array. It contains only a \0.
(*args)[rows] = (char*)calloc( 1, sizeof( char ) );
(*args)[rows][0] = '\0';

// return the number of arguments.
return numArgs;
}

最佳答案

如果您利用 strtok(),它会更容易理解,您也可以消除 getNumArgs

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int makearg( char *s, char ***args ) // modifies s in place
{
int n = 0;
char *copy = strdup(s);
for(char *p = strtok(copy, " \t"); p; p = strtok(0, " \t")) {
n++;
}
free(copy);

char **ap = calloc(n+1, sizeof(char *));

if(n) {
ap[0] = strtok(s, " \t");
for(int i = 1; i < n; i++) {
ap[i] = strtok(0, " \t");
}
}
*args = ap;
return n;
}

关于c - C 中的锯齿状数组 - 一种更有效的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com