【问题标题】:Trying to assign a char pointer from another char pointer. Why is it segfaulting?试图从另一个 char 指针分配一个 char 指针。为什么会出现段错误?
【发布时间】:2020-08-11 17:39:43
【问题描述】:

已编辑:这修复了原始段错误。但现在我不确定为什么 text2 只是存储 NULL

char* text = "some text";    
char* text2 = malloc(sizeof(text2) * MAX_WORD_LEN);

while (*text != '\0'){
      *text2++ = *text;
      if(isspace(*text)){
           while (isspace(*text)){ 
                  *text++; 
           }
      }
      else{ *text++; }
      
}

为什么会出现这个段错误?我该怎么做?

【问题讨论】:

  • char* text2 = malloc(sizeof(char*) * MAX_WORD_LEN); 应该是 char* text2 = malloc(sizeof(char) * MAX_WORD_LEN);,或者更好的是 char* text2 = malloc(sizeof(*text2) * MAX_WORD_LEN);
  • 也就是说,内部while 看起来是if 的候选对象。
  • @SouravGhosh 谢谢你们!这两个建议都修复了段错误。该字符串现在存储空值,但至少向前迈进了一步
  • @cdpp 循环没有意义。如果你描述你正在尝试做的事情会更好。
  • 为什么在创建char数组时要乘以指针的大小?

标签: c segmentation-fault character whitespace c99


【解决方案1】:

您的代码有几个问题。我在下面列出它:

  1. char* text2 = malloc(sizeof(text2) * MAX_WORD_LEN);sizeof(text2)是什么意思,不应该是sizeof(char)
  2. 你在增加text2 pointer,那么你怎么能打印text2,如果你不知道text2的开始,因为你增加指针text2只会指向'\0 '。
  3. 你的while循环也有问题...while (isspace(*text)),如果你到达*text = '\0' or not,你没有在这里检查...

好的,让我给出正确版本的代码:

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

#define MAX_WORD_LEN 1024


/**
 * Note: this could also handle if spaces are the end
 *
*/


int main() {
    char* text = "some text  ";    
    char* text2 = malloc(sizeof(char) * MAX_WORD_LEN); // note: sizeof(char), cause string

    char *itr = text; // its better to put a seperate iterator to iterate over strings in c
    char *itr2 = text2; // declare a iterator to iterate over text2

    while (*itr != '\0') {
          *itr2 = *itr;

          // increase both iterator
          itr++;
          itr2++;

          // there's no need to provide a if here, just run while loop
          // also notice, previously by increasing "text" pointer you may reach the end
          // but, you're not checking it
          // but, you should check it
          while(*itr != '\0' && isspace(*itr)){ 
              itr++; // while iterating 
          }
     
          // there's no need to increase "itr" here
    }

    // finally at a null char to the end of text2
    *itr2 = '\0';

    printf("text: %s\n\n", text);
    printf("text2: %s\n", text2);

    // at last free allocated memory
    free(text2);

    return 0;
}

[P.S.]:提醒一下,我提供给你的程序,它也处理最后的空格。但是,如果您想要更好的东西,那么您必须处理前面、中间和末尾的空格...如果所有地方...它都适合您...

【讨论】:

    【解决方案2】:

    您没有存储空值。但是,由于您每次将text2 存储到其中时都会递增,因此您不再有指向原始text2 字符串开头的指针。在循环结束时,text2 指向刚刚复制的最后一个字符。

    复制text2 以在循环中使用,这样text2 将继续指向开头。或者使用数组索引而不是递增指针。

    char* text = "some text";    
    char* text2 = malloc(MAX_WORD_LEN);
    char *p = text2;
    
    while (*text != '\0'){
        *p++ = *text;
        if(isspace(*text)){
            while (isspace(*text)){ 
                *text++; 
            }
        } else{ *text++; } 
    }
    *p = '\0'; // add null terminator
    printf("result: %s\n", text2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2014-04-02
      • 2015-02-04
      • 2015-05-21
      • 1970-01-01
      • 2011-08-10
      • 2015-09-10
      相关资源
      最近更新 更多