Segmentation fault can';t strcpy分段故障

Segmentation fault can';t strcpy分段故障,segmentation-fault,Segmentation Fault,我有一个分段错误 所以在顶部我有typedef char*string 然后我有一个叫做spaces的变量,它被设置为5左右 for(i=0; i<=spaces; i++) { sepwords[i] = malloc(3000); } 所以这确实有效 然而,如果我这样做 while(str[i]!=' ') { char *temp[100]; *temp=str[i]; printf("%d\n", i); strcpy(sepwords

我有一个分段错误

所以在顶部我有typedef char*string

然后我有一个叫做spaces的变量,它被设置为5左右

for(i=0; i<=spaces; i++) {
    sepwords[i] = malloc(3000);
}
所以这确实有效

然而,如果我这样做

while(str[i]!=' ') {
    char *temp[100];
    *temp=str[i];


    printf("%d\n", i);
    strcpy(sepwords[i], *temp);
    i++;




}
它在这方面有缺陷

我不认为这是因为我使用了typedef字符串,因为我事先分配了内存

有什么想法吗

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 256

char *progn;

void usage(void) {
    fprintf(stderr, "Usage: %s pattern\n", progn);
}
typedef char * string;

int pattern_match(char *pattern, char *str) {






int i=0;
int spaces=0;

while(str[i]!=0) {
    if(str[i]==' ') {
        spaces++;
    }
    i++;
}
string sepwords[spaces];





for(i=0; i<=spaces; i++) {
    sepwords[i] = malloc(3000);
}


i=0;


while(str[i]!=' ') {
    char *temp[100];
    *temp=str[i];


    printf("%d\n", i);
    strcpy(sepwords[i], temp);
    i++;




}









//printf("%d\n", spaces);



//strs[0]="hiya boy";
//printf(strs[1]);





}

int main(int argc, char **argv) {

    char line[MAXLINE];
    char *pattern;
    progn = argv[0];

    if (argc != 2) {
        usage();
        return EXIT_FAILURE;
    }
    pattern = argv[1];

    while (!feof(stdin) && !ferror(stdin)) {
        if (!fgets(line, sizeof (line), stdin)) {
            break;
        }
        if (pattern_match(pattern, line)) {
            printf("%s", line);
        }
    }
    if (ferror(stdin)) {
        perror(progn);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#定义MAXLINE 256
char*progn;
无效用法(void){
fprintf(标准,“用法:%s模式\n”,程序);
}
typedef char*字符串;
int模式匹配(char*pattern,char*str){
int i=0;
int空间=0;
while(str[i]!=0){
如果(str[i]=''){
空格++;
}
i++;
}
字符串字[空格];
对于(i=0;i
  • 将char*定义为字符串,而不是将其定义为pchar。char*不是字符串,而是指向字符的指针,该字符可能是或不是字符的地址,可能在内存中后跟以0结尾的其他字符。这一点很重要,因为未定义的\u pchar值=分段错误。未定义的\u字符串值=“”“

  • fprintf(stderr,“用法:%s pattern\n”,progn);
    这会打印未定义的progn。它可能是非法内存,也可能是内存的随机收集。在任何一种情况下都是不正确的。因此,您应该在声明progn
    pchar progn=0
    时指出您理解此概念

  • sepwords是char*,sepwords[i]是char,如果说sepwords[i]=malloc(3000),即使它编译了,也会误用符号。使用pchar*来创建c样式字符串的列表


    对于(i=0;i您的代码中有两个语法错误。您的编译器一定已经警告过您了:

    *temp=str[i]

    temp是char*[100],*temp相当于temp[0],temp[0]是char*。但是str[i]是char。 所以你把一个字符(单字节)放入一个字符*(地址)

    strcpy(sepwords[i],temp)

    temp是100个字符*的完整数组的地址,因此您将像一个大字符串一样进行复制,直到找到某个零字节

    还有一个错误:

    字符串字[空格]


    对于(i=0;iOn)您是哪种平台的?请尝试内存调试器工具。我认为您还没有掌握C声明的语义和语法。一个有用的技巧是从标识符开始读取它们,右穿过任何括号,然后左穿过*s和限定符,因此char*temp[100]读作“temp是指向char的100个指针的数组"我建议你解释一下你真正想要实现的是什么。从你发布的代码片段来看,这并不明显。它应该打开一个文本文件,然后只打印满足用户输入给定条件的行,但首先我希望它将行中的每个字分为自己的字符串。这就是循环所做的,继续粗略地画出行中的每个字母,然后将其添加到字符串中。char*指针数组是一个意外,哎呀。我的代码中就有它。我刚刚开始理解您打算用代码做什么,因为它不是很简单。您只有一个索引(i)用于两个不同的事情(输入字符串中的索引-str-和拆分单词列表中的索引-sepwords)如果你仍然想使用你的代码,你必须有两个。即使是3个,一个用于输入POS,一个用于当前的Script,一个用于这个单词的位置。然后你必须在Script的末尾加上0。这是一个很好的东西来学习这些东西是如何工作的,但是你应该考虑StruChr(找到空间)或者Strutk。(为你做所有的工作).
    strncpy
    不是一个神奇版本的
    strcpy
    ,它有不同的语义,并且它本身不会修复错误。如果你不知道你要做什么,断言也没有帮助。如果你使用断言,你肯定知道你要做什么。如果没有,那么你就不能发布代码示例,你必须没有明确的意图,代码将无法工作,或者根本上存在缺陷。那么我如何绕过strcpy问题呢?
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXLINE 256
    
    char *progn;
    
    void usage(void) {
        fprintf(stderr, "Usage: %s pattern\n", progn);
    }
    typedef char * string;
    
    int pattern_match(char *pattern, char *str) {
    
    
    
    
    
    
    int i=0;
    int spaces=0;
    
    while(str[i]!=0) {
        if(str[i]==' ') {
            spaces++;
        }
        i++;
    }
    string sepwords[spaces];
    
    
    
    
    
    for(i=0; i<=spaces; i++) {
        sepwords[i] = malloc(3000);
    }
    
    
    i=0;
    
    
    while(str[i]!=' ') {
        char *temp[100];
        *temp=str[i];
    
    
        printf("%d\n", i);
        strcpy(sepwords[i], temp);
        i++;
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    //printf("%d\n", spaces);
    
    
    
    //strs[0]="hiya boy";
    //printf(strs[1]);
    
    
    
    
    
    }
    
    int main(int argc, char **argv) {
    
        char line[MAXLINE];
        char *pattern;
        progn = argv[0];
    
        if (argc != 2) {
            usage();
            return EXIT_FAILURE;
        }
        pattern = argv[1];
    
        while (!feof(stdin) && !ferror(stdin)) {
            if (!fgets(line, sizeof (line), stdin)) {
                break;
            }
            if (pattern_match(pattern, line)) {
                printf("%s", line);
            }
        }
        if (ferror(stdin)) {
            perror(progn);
            return EXIT_FAILURE;
        }
        return EXIT_SUCCESS;
    }
    
    sepwords[i] = malloc(3000);