Arrays Char**函数在函数外部工作不正常

Arrays Char**函数在函数外部工作不正常,arrays,string,char,strtok,Arrays,String,Char,Strtok,因此,我创建了一个名为myargv的char**变量,它的作用应该类似于在每个索引处存储字符串的数组。我有一个函数,它向myargv返回一个字符**,当我从内部打印所有内容时,该函数似乎工作正常,但当我尝试从main打印myargv中的内容时,它不再工作了。。。。有人能帮我吗 char **findArgs(char *line) { int i = 0; char **temp, *tokTemp; char **myargv; tokTemp = strtok(line, " "); m

因此,我创建了一个名为myargv的char**变量,它的作用应该类似于在每个索引处存储字符串的数组。我有一个函数,它向myargv返回一个字符**,当我从内部打印所有内容时,该函数似乎工作正常,但当我尝试从main打印myargv中的内容时,它不再工作了。。。。有人能帮我吗

char **findArgs(char *line)
{
int i = 0;
char **temp, *tokTemp;
char **myargv;

tokTemp = strtok(line, " ");

myargv = malloc(sizeof(*myargv));

while (tokTemp != NULL)
{
if (strcmp(tokTemp, ">") == 0 || strcmp(tokTemp, "<") == 0 || strcmp(tokTemp, ">>") == 0)
{
    break;
}
else
{
    myargv[i] = malloc(strlen(tokTemp) + 1);
    //myargv[i] = malloc(sizeof(char)*strlen(tokTemp));
    strcpy(myargv[i], tokTemp);
    //printf("myargv[%d] = %s \n", i, myargv[i]);

    temp = realloc(myargv, (i+2)*sizeof(*myargv));    
    if (temp != NULL)
    {
        myargv = temp;
    }
    tokTemp = strtok(NULL, " ");
    i++;
}
}
myargv[i] = NULL;
//printf("myargv[0] = %s\n", myargv[0]);
return myargv;
}


int main(int argc, char *argv[], char *env[])
{
int cmdInt, pid, status, i = 0, ioNumber = 0;
char input[64], lineBUFFER[64], lineBUFFER2[64], lineBUFFER3[64], lineBUFFER4[64], homePath[64], fileName[64], cmdPathFINAL[64];
char *cmd;
char **myargv, **cmdPath; 
int myFile;

while(1)
{

 printf("command: ");
 gets(input);

strcpy(lineBUFFER, input);
strcpy(lineBUFFER2, input);
strcpy(lineBUFFER3, input);  
strcpy(lineBUFFER4, input);  

cmd = strtok(lineBUFFER3, " ");
cmdInt = findCommand(lineBUFFER2);

ioNumber = ioCheck(lineBUFFER4, fileName);
//printf("ioCheck = %d \n", ioNumber);
//printf("Filename: %s \n", fileName);

myargv = findArgs(lineBUFFER);
//printf("myargv[0] = %s\n", myargv[0]);

findHome(env, homePath);
//printf("Home path = %s\n", homePath);

switch(cmdInt)
      { 
          case 0 : 

            if (myargv[1] == NULL)
    {
        chdir(homePath);
    }
    else
    {
        //printf("1st argument: %s, 2nd argument: %s, 3rd argument: %s \n", myargv[1], myargv[2], myargv[3]);
        chdir(myargv[1]);
    }

              break;
          case 1 :

              exit(1);   

              break;
          default :

            pid = fork();
    if (pid == 0)
    {
    //printf("Parent %d waits for child %d to die. \n", getpid(), pid);
    pid = wait(&status);
    printf("dead child = %d, how = %04x \n", pid, status);
    exit(100);
    }
    else
    {
        ioNumber = ioCheck(lineBUFFER4, fileName);
            //printf("ioCheck = %d \n", ioNumber);
            //printf("Filename: %s \n", fileName);
        cmdPath = getPath2(env);
            //printf("cmdPath[0] = %s\n", cmdPath[0]);

            findPath(cmd, cmdPath, cmdPathFINAL);
            printf("Command Path = %s\n", cmdPathFINAL);

        if (ioNumber == 1)
        {
            close(0);
            myFile = open(fileName, O_RDONLY);
        }   
        else if (ioNumber == 2)
        {
            close(1);
            myFile = open(fileName, O_WRONLY|O_CREAT, 0644); 
        }
        else if (ioNumber == 3)
        {
            close(1);
            myFile = open(fileName, O_WRONLY|O_APPEND);
        }

        execve(cmdPathFINAL, myargv, env);
        //printf("child %d dies by exit () \n", getpid());
        exit(100);
    }

              break;
      }
}

if (ioNumber == 1 || ioNumber == 2 || ioNumber == 3)
{
close(myFile);
}   

return 0;
}
char**findArgs(char*line)
{
int i=0;
字符**温度,*温度;
字符**myargv;
TOKTTEMP=strtok(第“”行);
myargv=malloc(sizeof(*myargv));
while(tokTemp!=NULL)
{
如果(strcmp(tokTemp,“>”)==0 | | strcmp(tokTemp,“>”)==0)
{
打破
}
其他的
{
myargv[i]=malloc(strlen(tokTemp)+1);
//myargv[i]=malloc(sizeof(char)*strlen(tokTemp));
strcpy(myargv[i],tokTemp);
//printf(“myargv[%d]=%s\n”,i,myargv[i]);
temp=realloc(myargv,(i+2)*sizeof(*myargv));
如果(温度!=NULL)
{
myargv=温度;
}
TOKTTEMP=strtok(空,“”);
i++;
}
}
myargv[i]=NULL;
//printf(“myargv[0]=%s\n”,myargv[0]);
返回myargv;
}
int main(int argc,char*argv[],char*env[]
{
int-cmdInt,pid,status,i=0,ioNumber=0;
字符输入[64]、lineBUFFER[64]、lineBUFFER2[64]、lineBUFFER3[64]、lineBUFFER4[64]、homePath[64]、fileName[64]、cmdPathFINAL[64];
char*cmd;
字符**myargv,**cmdPath;
int-myFile;
而(1)
{
printf(“命令:”);
获取(输入);
strcpy(行缓冲区,输入);
strcpy(lineBUFFER2,输入);
strcpy(lineBUFFER3,输入);
strcpy(lineBUFFER4,输入);
cmd=strtok(lineBUFFER3,“”);
cmdInt=findCommand(lineBUFFER2);
ioNumber=ioCheck(lineBUFFER4,文件名);
//printf(“ioCheck=%d\n”,ioNumber);
//printf(“文件名:%s\n”,文件名);
myargv=findArgs(lineBUFFER);
//printf(“myargv[0]=%s\n”,myargv[0]);
findHome(环境、家庭路径);
//printf(“主路径=%s\n”,主路径);
交换机(cmdInt)
{ 
案例0:
if(myargv[1]==NULL)
{
chdir(homePath);
}
其他的
{
//printf(“第一个参数:%s,第二个参数:%s,第三个参数:%s\n”,myargv[1],myargv[2],myargv[3]);
chdir(myargv[1]);
}
打破
案例1:
出口(1);
打破
违约:
pid=fork();
如果(pid==0)
{
//printf(“父%d等待子%d死亡。\n”,getpid(),pid);
pid=等待(&状态);
printf(“死孩子=%d,如何=%04x\n”,pid,状态);
出口(100);
}
其他的
{
ioNumber=ioCheck(lineBUFFER4,文件名);
//printf(“ioCheck=%d\n”,ioNumber);
//printf(“文件名:%s\n”,文件名);
cmdPath=getPath2(env);
//printf(“cmdPath[0]=%s\n”,cmdPath[0]);
findPath(cmd、cmdPath、cmdPath-final);
printf(“命令路径=%s\n”,cmdPathFINAL);
如果(IonNumber==1)
{
关闭(0);
myFile=open(仅文件名);
}   
else if(IonNumber==2)
{
关闭(1);
myFile=open(文件名,O|u WRONLY | O|u CREAT,0644);
}
else if(ioNumber==3)
{
关闭(1);
myFile=open(文件名,O_WRONLY | O_APPEND);
}
execve(cmdPathFINAL、myargv、env);
//printf(“子%d通过exit()\n”,getpid())死亡;
出口(100);
}
打破
}
}
如果(ioNumber==1 | | ioNumber==2 | | ioNumber==3)
{
关闭(myFile);
}   
返回0;
}

以下内容适用于Windows和SuSE linux 10.4 x86_64内核2.6.16。解析器有两个版本:getArgs和getArgs2。虽然没有对代码进行彻底的检查,但希望它能对您有所帮助

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

int getArgs(const char *line, char *args[], int argc)
{
    int len, i = 0;
    const char* delims = " ";
    char *ltok = strdup(line);
    char *tok = strtok(ltok, delims);

    for (; tok != NULL; i++) {

        if (strchr("><|", *tok) != NULL) /* not sure why this test is needed for CLI */
            break;

        if (args != NULL && i < argc) {
            args[i] = strdup(tok);
        }

        tok = strtok(NULL, delims);
    }

    free(ltok);

    return i;
}

int getArgs2(const char *line, char **args, int argc)
{
    int len, i = 0;
    const char *p, *q;
    const char *delims = " ";

    p = line;
    while (strchr(delims, *p) != NULL && *p != '\0')
        p++;

    q = p;
    while (strchr(delims, *q) == NULL && *q != '\0')
        q++;

     for (; q > p; i++) {

        if (strchr("><|", *p) != NULL) /* not sure why this test is needed for CLI */
            break;

        len = q-p;

        if (args != NULL && i < argc) {
            args[i] = (char *)malloc(len + 1);
            strncpy(args[i], p, len)[len] = '\0';
        }

        p = q;
        while (strchr(delims, *p) != NULL && *p != '\0')
            p++;

        q = p;
        while (strchr(delims, *q) == NULL && *q != '\0')
            q++;

    }

    return i;
}


int main(int argc, char *argv[])
{
    char line[256], *q;
    char **args;
    int count, n;

    for(;;) {

        printf("command: ");
        if (fgets(line, sizeof(line)/sizeof(line[0]), stdin) == NULL)
            break;

        q = line;
        while (isspace(*q))
            q++;

        if (*q == '\0')
            break;

        while (*q != '\0' && *q != '\n' && *q != '\r')
            q++;

        *q = '\0';

        for(count = 0, q = line; *q != '\0'; q++)
            if (*q == ' ')
                count++;

        printf("Input command: %d character(s), %d space(s) for \"%s\"\n", strlen(line), count, line);

        count = getArgs(line, NULL, 0);

        if (count > 0) {
            args = (char **)malloc(sizeof(char *)*count);
            count = getArgs(line, args, count);
        } else {
            args = NULL;
        }

        printf("Parsed %d arg(s) from line\n", count);
        printf("--------------------------------------\n");

        for (n = 0; n < count; n++) {
            printf("%d: %s\n", n+1, args[n]);
            free(args[n]);
        }

        free(args);

    }

    return 0;

}
#包括
#包括
#包括
#包括
int getArgs(常量字符*行,字符*args[],int argc)
{
int len,i=0;
const char*delims=“”;
char*ltok=strdup(行);
char*tok=strtok(ltok,delims);
for(;tok!=NULL;i++){

如果(strchr(“>define
在您的情况下不起作用
。请尝试在while循环后添加myargv[i]=NULL;解析对我有效,但最后一个指针解析为一个不明确的值。您可能还希望在while循环的开头检查管道。当我尝试打印f时(“myargv[0]=%s,myargv[1]=%s\n”,myargv[0],myargv[1]);在函数内部,它似乎工作得很好……但是当我在main函数外部尝试相同的printf时,我得到了一个“核心转储”。检查管道意味着什么?我添加了myargv[I]=NULL;在while循环之后,我仍然得到分段错误(核心转储)那么我的代码出了什么问题?我只是没有正确分配内存还是怎么了?我无法运行你的代码,所以我提供了一个替代方案供你尝试。我希望它能有所帮助。