Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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,在下面的代码中,行: *end = *front; 给出了一个分段错误。我问了一个类似的问题,但我不确定这是否是因为我有两份num。请解释为什么它是seg故障。多谢各位 #include <stdio.h> #include <stdlib.h> #include <string.h> char* getPalin(char* num); int main() { char* num = (char*)malloc(100); num

在下面的代码中,行:

*end = *front;
给出了一个分段错误。我问了一个类似的问题,但我不确定这是否是因为我有两份num。请解释为什么它是seg故障。多谢各位

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

char* getPalin(char* num);

int main()
{
    char* num = (char*)malloc(100);

    num = "123456";

    printf("%s\n", getPalin(num) );

    return 0;
}

char* getPalin(char* num)
{
    int length = strlen(num);

    if ( length % 2 == 0 )
    {
        char* front = num;
        char* end = num + strlen(num) - 1;  //pointer to end

        while( front != num + (length/2) )  //pointers not middle yet
        {
            *end = *front;

            printf("%c", *end);

            front++;
            end--;
        }
    }

    return num;
}
#包括
#包括
#包括
char*getPalin(char*num);
int main()
{
char*num=(char*)malloc(100);
num=“123456”;
printf(“%s\n”,getPalin(num));
返回0;
}
char*getPalin(char*num)
{
int length=strlen(num);
如果(长度%2==0)
{
char*front=num;
char*end=num+strlen(num)-1;//指向end的指针
while(front!=num+(length/2))//指针尚未居中
{
*结束=*前面;
printf(“%c”,*end);
前端++;
结束--;
}
}
返回num;
}
使用

而不是

num = "123456";
这两条线:

char* num = (char*)malloc(100);
num = "123456";
具有以下效果

第一个分配100个字节,并将
num
设置为指向这些字节

第二个更改num以指向字符串“123456”,该字符串几乎肯定位于只读内存中

任何更改只读内存内容的尝试都将导致分段冲突。您需要先将字符串复制到
malloc
'd
num
中,然后再尝试更改它,方法是:

strcpy (num, "123456");
这是您当前应具备的条件:

num = "123456";

根据康斯坦丁的回答

您已经使用malloc语句为num分配了内存

如果你没有,那么你可以逃脱:

char* num = "123456";
它可以动态定义和分配内存,但很可能是作为常量分配的,因此是只读的

使用strncpy而不是strcpy复制“123456”将确保字符串结尾null终止符之外的任何额外空间也初始化为null,只要您将n指定为100(例如)。否则,如果不将malloc分配的内存初始化为null(memset(num,0100)),那么可以想象您可能会跳出字符串的末尾


哦,差点忘了。建议使用strcpy\u或strncpy\u,因为它们更安全,尽管对于您的代码来说这无关紧要。

错误的原因是:

  char* num = (char*)malloc(100);
在这一行中,您将num声明为指向数组的指针或指向其第一个元素的指针,而不是字符串

 num = "123456";
这一行在声明为字符串时使用了num。这违反了分段,因此出现seg故障。代码的首选(正确)语法为:

   char num[100];
   strcpy(num,"123456"); //Even if you use num="123456"; here it would still be wrong


所有这些都可以完成您的工作。

对不起,不是sizeof(num),而是分配内存的大小,即100。即strncpy(num,“123456”,100);OP要求解释,但你只是给出了一个解决方案。永远不要忘记在strncpy之后手动添加null终止符,因为不能保证strncpy会添加它。我知道它在本例中不相关,因为“123456”小于100字节,但如果您决定使用strncpy而不是strcpy,请正确使用它-您避免了一个潜在问题(缓冲区溢出),但引入了另一个问题(未终止的字符串).@qrdl-只要strncpy中指定的目标大小大于字符串长度,strncpy将自动用空字符填充。只有当目标大小指定为小于要复制的字符串长度时,才会丢失空终止符。另一点是记住strncpy不会检查目标内存中是否有足够的空间。所以我认为strncpy(num,“123456”,sizeof(num))更正确。num是一个“char*”,所以sizeof(num)将是4(或者8,如果你的硬件比我好的话)。@ChrisBD-请仔细阅读我的评论。我写道,我知道这在这种情况下并不重要。在这一情况下,FY-SIZEOF(NUM)是SIZOF(CHAR),因为Num在参数列表中被声明为char。我实际上认为这两个SO()函数对于不知道如何编码防御的人来说是一个拐杖(即,先检查先决条件)-我甚至从来没有发现需要使用StRcCyPy(),而我的代码仍然是“安全的”。与任何使用_s()变量的方法一样]。最重要的是,它们还不是标准的一部分。因此,我说它们是可取的。有多少商业软件会让自己面临缓冲区溢出攻击,这让我一直感到惊讶。
   char num[100];
   strcpy(num,"123456"); //Even if you use num="123456"; here it would still be wrong
  char* num = (char*)malloc(100);
  strcpy(num,"123456");
  char num[100]={'1','2','3','4','5','6'};