Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的分段错误,子字符串_C_Segmentation Fault - Fatal编程技术网

C中的分段错误,子字符串

C中的分段错误,子字符串,c,segmentation-fault,C,Segmentation Fault,我得到了帮助,其中一个答案建议使用这种技术,但我一直在代码中遇到分段错误(内核转储) char *nam = array;//<-----(array is a string, we can use "test string" for it int i = 0; int length = strlen(array); int count = 1; printf("%i [%s]\n", length,nam); for(i; i < length; i++) { pu

我得到了帮助,其中一个答案建议使用这种技术,但我一直在代码中遇到分段错误(内核转储)

char *nam = array;//<-----(array is a string, we can use "test string" for it
int i = 0;
int length = strlen(array);
int count = 1;
printf("%i [%s]\n", length,nam);    
for(i; i < length; i++)
{
    puts(nam + i);
    nam[strlen(nam) - 1] = '\0';
}
希望这能澄清
这段代码在char[]=数组上给了我一个无效的初始值设定项;行

数组
是字符串文字,您不应该尝试更改它

改变

char *nam = array;

然后再试一次

顺便说一下,实际的代码应该是

char name[] = "test string";

更新:

版本1,需要C99 VLA

void pyramid(char array[])
{    
    int length = strlen(array);
    char nam[length+1];
    int i = 0;
    int count = 1;

    strcpy(nam, array);
    printf("%i [%s]\n", length, nam);   
    for(i; i < length; i++)
    {
        puts(nam + i);
        nam[strlen(nam) - 1] = '\0';
        printf("[%s]\n", nam);
    }
}
void棱锥体(字符数组[])
{    
int length=strlen(数组);
char-nam[length+1];
int i=0;
整数计数=1;
strcpy(nam,数组);
printf(“%i[%s]\n”,长度,nam);
对于(i;i
版本2,使用动态内存分配:

void pyramid(char array[])
{    
    char *nam;
    int i = 0;
    int length = strlen(array);
    int count = 1;

    if ((nam = malloc(length+1)) == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    strcpy(nam, array);

    printf("%i [%s]\n", length, nam);   
    for(i; i < length; i++)
    {
        puts(nam + i);
        nam[strlen(nam) - 1] = '\0';
        printf("[%s]\n", nam);
    }

    free(nam);
}
void棱锥体(字符数组[])
{    
char*nam;
int i=0;
int length=strlen(数组);
整数计数=1;
如果((nam=malloc(长度+1))==NULL){
佩罗尔(“马洛克”);
退出(退出失败);
}
strcpy(nam,数组);
printf(“%i[%s]\n”,长度,nam);
对于(i;i
char*nam=array// 字符串文本存储在只读内存段中。您应该将其视为<代码> const char */c>或不可变字符串.< /p>
如果编写函数是为了对字符串执行破坏性操作,那么最好还是复制它

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

void pyramid_demo(const char *input) {
    /* Copy input to buf */
    int len = strlen(input);
    char *buf = malloc(len + 1);
    strcpy(buf, input);

    puts(buf);
    buf[len - 1] = '\0';    /* Now you can write to buf */
    puts(buf + 1);
}

int main(void) {
    /* The literal string is immutable */
    pyramid_demo("Hello world");
}
  • char-nam[]=数组是无意义的别名。它不会复制输入

  • 避免重复调用
    strlen()
    ,因为每次调用都需要遍历整个字符串以查找
    \0
    终止符。您已经在开始时设置了
    length
    ,因此只需通过减小
    length
    来跟踪字符串长度即可


  • @StevenR实际代码应该类似于
    char name[]=“teststring”
    。我实际上更新了上面的代码,以便更清楚地说明我要做什么。哦,我明白了,现在它可以工作了,但我所做的是在主类中用实际字符串调用一个方法,这个方法应该获得名称,那么我该如何做呢?我将重新编辑我的原稿,这样你就知道我在说什么了。没有错误,我知道:(我只希望我的老师真的教了我们这个,他只是谈论现实世界的东西,而不是教学,谢谢你的帮助请通过投票选出有用的答案而不是评论来表达你的感激之情。你的投票让堆栈溢出起作用。噢,哇!忘了这么做!
    void pyramid(char array[])
    {    
        int length = strlen(array);
        char nam[length+1];
        int i = 0;
        int count = 1;
    
        strcpy(nam, array);
        printf("%i [%s]\n", length, nam);   
        for(i; i < length; i++)
        {
            puts(nam + i);
            nam[strlen(nam) - 1] = '\0';
            printf("[%s]\n", nam);
        }
    }
    
    void pyramid(char array[])
    {    
        char *nam;
        int i = 0;
        int length = strlen(array);
        int count = 1;
    
        if ((nam = malloc(length+1)) == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        strcpy(nam, array);
    
        printf("%i [%s]\n", length, nam);   
        for(i; i < length; i++)
        {
            puts(nam + i);
            nam[strlen(nam) - 1] = '\0';
            printf("[%s]\n", nam);
        }
    
        free(nam);
    }
    
    char *nam = array;//<-----(array is a string, we can use "test string" for it
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void pyramid_demo(const char *input) {
        /* Copy input to buf */
        int len = strlen(input);
        char *buf = malloc(len + 1);
        strcpy(buf, input);
    
        puts(buf);
        buf[len - 1] = '\0';    /* Now you can write to buf */
        puts(buf + 1);
    }
    
    int main(void) {
        /* The literal string is immutable */
        pyramid_demo("Hello world");
    }
    
    for (int i = 0; i < length; i++)