简体   繁体   English

使用指针字符串会产生分段错误

[英]Using Pointer Strings gives segmentation fault

The task was mainly to use pointers to input a string and slice it at places where there is a '\\' character and output them in separate lines, using pointers. 任务主要是使用指针输入字符串,然后在有'\\'字符的位置对其进行切片,然后使用指针将它们输出到单独的行中。 The program runs fine when I use arrays instead of pointers. 当我使用数组而不是指针时,程序运行良好。 However using pointers to store strings give the message "Segmentation fault". 但是,使用指针存储字符串会给出消息“分段错误”。 The code is as follows : 代码如下:

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

int main() {
char *name;
char *sep[100];
int i = 0, j = 0, k = 0;
scanf("%[^\n]s", name);

for(i = 0; (*(name+i)) != '\0'; i++) {
    if((*(name+i)) == '\\') {
        *((*(sep+k))+j) = '\0';
        j = 0;
        k++;
    } else {
        *((*(sep+k))+j) = *(name+i);
        j++;
    }
}

for(i = 0; i <= k; i++) {
    printf("%s\n", *(sep+i));
}

return 0;
}

It would be awesome if you could point out what and where the problem is, instead of giving me an alternative solution. 如果您可以指出问题所在和出处,而不是给我替代解决方案,那就太好了。 TIA. TIA。

your pointers are null pointers.you are invoking undefined behavior by using them without assigning them to allocated memory.Allocate memory to them so that you can use them correctly and store words separated by \\.Also,you can use [] instead of * . 您的指针是空指针。您通过使用未定义的行为而没有将它们分配给已分配的内存。为它们分配内存,以便可以正确使用它们并存储用\\分隔的单词。此外,可以使用[]代替*

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

int main()
{
    char name[256];
    char *sep[100];
    for( int n = 0 ; n < 100 ; n++ )
    {
        sep[n] = malloc(30*sizeof(char));
    }
    int i = 0, j = 0, k = 0;
    scanf(" %255[^\n]s", name);

    for(i = 0; name[i] != '\0'; i++)
    {
        if( name[i] == '\\')
        {
            sep[k][j] = '\0';
            j = 0;
            k++;
        }
        else
        {
            sep[k][j] = name[i];
            j++;
        }
    }

    sep[k][j] = '\0';
    for(i = 0; i <= k ; i++)
    {
        printf("%s\n",sep[i]);
    }
    for( int n = 0 ; n < 100 ; n++ )
    {
        free(sep[n]);
    }
    return 0;
}

In your code, 在您的代码中

 scanf("%[^\n]s", name);

name is an unintialized pointer. name是未初始化的指针。 It does not point to any valid memory location. 它没有指向任何有效的内存位置。 You need to allocate memory before you can use it. 您需要先分配内存,然后才能使用它。

The same goes out for sep array, too. sep数组也是如此。

You can consider using an array for this purpose or see the man page of malloc() if you want to stick to a pointer. 您可以考虑为此目的使用数组,或者如果要坚持使用指针,请参见malloc()手册页

FWIW, using an unitialized pointer can lead to undefined behavior . FWIW,使用统一的指针可能导致未定义的行为

You must allocate space for your pointers to avoid undefined behaviour: you cannot use a pointer without initializing it. 您必须为指针分配空间,以避免未定义的行为:如果不初始化指针,则无法使用指针。

int main() {
   char *name = malloc(MAX_DIM_OF_NAME+1);
   char *sep[100];

   for (int i=0; i<100; i++)
      sep[i] = malloc(MAX_DIM_OF_NAME+1);

   ....

您使用未初始化的name调用scanf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM