C 我的程序可以';在某些情况下,我们似乎不能把单词分开

C 我的程序可以';在某些情况下,我们似乎不能把单词分开,c,string,pointers,char,double-pointer,C,String,Pointers,Char,Double Pointer,我最关心的是查找和分离函数。当我在另一个程序中重用它们时,我发现它们并不总是有效,有时会出现分段错误。我尽我所能做了一切,但都没有成功 #include <stdio.h> #include<stdlib.h> #include <string.h> int find(char *argument) { int count = 0; int i = 0; int state = 0; while (argument[i] !=

我最关心的是查找和分离函数。当我在另一个程序中重用它们时,我发现它们并不总是有效,有时会出现分段错误。我尽我所能做了一切,但都没有成功

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

int find(char *argument)
{
    int count = 0;
    int i = 0;
    int state = 0;
    while (argument[i] != '\0')
    {
        if (argument[i] == ' ' || argument[i + 1] == '\0')
        {
            state = 0;
        }
        else if (state == 0)
        {

            state = 1;
            count++;
        }
        i++;
    }
    return count;
}

char **sepration(int args, char str[])
{
    int i = 0;
    char **argv = (char **)malloc(args);

    int k = 0;
    int len = strlen(str);
    while (i < len)
    {

        if (str[i] != ' ')
        {
            char *st = &str[i];
            argv[k] = st;
            k++;
            while (str[i] != ' ' && str[i] != '\0' && i < len)
            {
                i++;
            }
            str[i] = '\0';
        }
        i++;
    }

    return argv;
}

int main()
{
    char argument[100];
    scanf("%[^\n]s", &argument);
    //finds the no of words in the given argument
    int args = find(argument);
    char **argv = (char **)malloc(args + 1);
    //stores the word separately in **char pointer array
    argv = sepration(args, argument);
    //adds the arguments whichs are numbers
    add(args, argv);
}
#包括
#包括
#包括
int find(字符*参数)
{
整数计数=0;
int i=0;
int state=0;
while(参数[i]!='\0')
{
if(参数[i]=''| |参数[i+1]='\0')
{
状态=0;
}
else if(state==0)
{
状态=1;
计数++;
}
i++;
}
返回计数;
}
字符**分隔(int-args,char-str[])
{
int i=0;
char**argv=(char**)malloc(args);
int k=0;
int len=strlen(str);
而(我
解决方案的问题在于没有正确分配内存
双指针argv应该作为字符串数组使用。因此,
argv
应该有足够的内存分配给它。然后下一个问题是如何为相同的内存分配足够的内存。由于
argv
应该包含
argc
个字符串,因此正确的内存分配将是
char**argv=malloc(args*sizeof(char*)或,
char*argv[argc]

修改后的代码看起来像

char **sepration(int args, char str[])
{
    int i = 0;
    char **argv = malloc(args * sizeof(char *));

    int k = 0;
    int len = strlen(str);
    while (i < len)
    {
        if (str[i] != ' ')
        {
            char *st = &str[i];
            argv[k] = st;
            k++;
            while (str[i] != ' ' && str[i] != '\0' && i < len)
            {
                i++;
            }
            str[i] = '\0';
        }
        i++;
    }
    return argv;
}

int main()
{
    char argument[100];
    scanf("%[^\n]", argument);
    //finds the no of words in the given argument
    int args = find(argument);
    char **argv = malloc(args * sizeof(char *));
    //stores the word separately in **char pointer array
    argv = sepration(args, argument);
    for(int i = 0; i < args; i++)
        printf("%s\n", argv[i]);
}
char**separation(int-args,char-str[])
{
int i=0;
char**argv=malloc(args*sizeof(char*);
int k=0;
int len=strlen(str);
而(我

而不是
char**argv=malloc(args*sizeof(char*))
您可以使用
char*argv[argc]也是。这也将产生相同的结果。

此外,在
main()
中存在内存泄漏。您argv=malloc(…);`但是,您在下一行重新分配了
argv
。发布的代码不会编译!编译时,始终启用警告,然后修复警告。(对于
gcc
,至少使用:
-Wall-Wextra-Wconversion-pedantic-std=gnu17
)注意:其他编译器有不同的选项来获得相同的内容,函数在哪里:
add()
?调用任何堆分配函数时,它既不在源代码中也不在C中包含的任何头文件中列出:
malloc
calloc
realloc
1)返回的类型为
void*
,可以分配给任何指针。强制转换只会使代码变得混乱,使其更难理解、调试等。。2) 始终检查(!=NULL)返回值以确保操作成功
char**argv=(char**)malloc(args)未分配正确数量的内存。应该是:
char**argv=malloc(args*sizeof(char*))–我不相信
scanf(“%[^\n]s”,&argument)正确,请参阅前面的注释。可能是
scanf(“%[^\n]”,参数)@风向标是的,你是正确的,不需要
s
。更新了答案。请注意,
&
也被删除,尽管之前没有对其进行评论。数组将衰减传递给指针。