Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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
ceasar密码在strcpy函数返回分段错误时中断的程序_C - Fatal编程技术网

ceasar密码在strcpy函数返回分段错误时中断的程序

ceasar密码在strcpy函数返回分段错误时中断的程序,c,C,我用C写了一个程序来探索剖宫产密码,但在运行它时,我总是遇到分段错误。代码如下: #include <stdio.h> #include <string.h> #include <stdlib.h> char ceaserEncrypt(char string[1000000],int key); int main(int *argc,char *argv[]) { char str[1000000] = "Hi there I am fine"; i

我用C写了一个程序来探索剖宫产密码,但在运行它时,我总是遇到分段错误。代码如下:

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

char ceaserEncrypt(char string[1000000],int key);


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

char str[1000000] = "Hi there I am fine";
int key = 20;
char val[1000000];
strcpy(val,ceaserEncrypt(str,key));
printf("%s",val);

return 0;
}




char ceaserEncrypt(char string[1000000],int key)
{

 int len = strlen(string);
 char encryptlis[len];

if(key < 1 || key > 26)
{
    fprintf(stderr,"Key provided is invalid");
    exit(-1);
}

 for(int i = 0; i < len; i++)
 {
    if(string[i] = ' '){
        encryptlis[i] = ' ';
        continue;
    }
    int charcode = string[i] + key;
    if(charcode > 122)
    {
        encryptlis[i] = (char) (charcode - 25);
    }

    encryptlis[i] = (char) charcode;

 }


return encryptlis;
}

我知道分割错误。但是在这个程序中,我没有看到我在哪里访问了非法的内存位置,因为我为encrpylis和val数组提供了足够的大小。
顺便说一下,我是C的新手。欢迎提出任何建议。已经谢谢你了

分段错误的主要原因是您试图访问未初始化的内存位置

字符串
encryptlis
未初始化

函数
ceaserEncrypt
的返回类型不正确

此外,还应遵循将字符串作为参数传递给函数的约定:将C字符串传递给函数的公认约定是使用指针

请查看以下工作实现:

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

char* ceaserEncrypt(char* string,int key);


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

    char str[100000] = "Hi there I am fine";
    int key = 20;
    char val[100000];

    strcpy(val,ceaserEncrypt(str,key));

    printf("%s",val);

    return 0;
}

char* ceaserEncrypt(char* string,int key)
{

    int len = strlen(string);
    char* encryptlis = (char*)(malloc((len+1) * sizeof(char)));


    if(key < 1 || key > 26)
    {
        fprintf(stderr,"Key provided is invalid");
        exit(-1);
    }

    for(int i = 0; i < len; i++)
    {
        if(string[i] == ' '){
            encryptlis[i] = ' ';
            continue;
        }

        int charcode = string[i] + key;
        if(charcode > 122)
        {
            encryptlis[i] = (char) (charcode - 25);
        }
        else{
            encryptlis[i] = (char) charcode;
        }
    }
    encryptlis[len] = '\0';

    return encryptlis;
}
#包括
#包括
#包括
char*ceaserEncrypt(char*string,int键);
int main(int argc,char*argv[]){
char str[100000]=“你好,我很好”;
int键=20;
char val[100000];
strcpy(val,ceaserEncrypt(str,key));
printf(“%s”,val);
返回0;
}
char*ceaserEncrypt(char*string,int键)
{
int len=strlen(字符串);
char*encryptlis=(char*)(malloc((len+1)*sizeof(char));
如果(键<1 | |键>26)
{
fprintf(stderr,“提供的密钥无效”);
出口(-1);
}
对于(int i=0;i122)
{
encryptlis[i]=(字符)(字符码-25);
}
否则{
encryptlis[i]=(char)字符码;
}
}
encryptlis[len]='\0';
返回加密LIS;
}

分段故障的主要原因是您试图访问未初始化的内存位置

字符串
encryptlis
未初始化

函数
ceaserEncrypt
的返回类型不正确

此外,还应遵循将字符串作为参数传递给函数的约定:将C字符串传递给函数的公认约定是使用指针

请查看以下工作实现:

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

char* ceaserEncrypt(char* string,int key);


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

    char str[100000] = "Hi there I am fine";
    int key = 20;
    char val[100000];

    strcpy(val,ceaserEncrypt(str,key));

    printf("%s",val);

    return 0;
}

char* ceaserEncrypt(char* string,int key)
{

    int len = strlen(string);
    char* encryptlis = (char*)(malloc((len+1) * sizeof(char)));


    if(key < 1 || key > 26)
    {
        fprintf(stderr,"Key provided is invalid");
        exit(-1);
    }

    for(int i = 0; i < len; i++)
    {
        if(string[i] == ' '){
            encryptlis[i] = ' ';
            continue;
        }

        int charcode = string[i] + key;
        if(charcode > 122)
        {
            encryptlis[i] = (char) (charcode - 25);
        }
        else{
            encryptlis[i] = (char) charcode;
        }
    }
    encryptlis[len] = '\0';

    return encryptlis;
}
#包括
#包括
#包括
char*ceaserEncrypt(char*string,int键);
int main(int argc,char*argv[]){
char str[100000]=“你好,我很好”;
int键=20;
char val[100000];
strcpy(val,ceaserEncrypt(str,key));
printf(“%s”,val);
返回0;
}
char*ceaserEncrypt(char*string,int键)
{
int len=strlen(字符串);
char*encryptlis=(char*)(malloc((len+1)*sizeof(char));
如果(键<1 | |键>26)
{
fprintf(stderr,“提供的密钥无效”);
出口(-1);
}
对于(int i=0;i122)
{
encryptlis[i]=(字符)(字符码-25);
}
否则{
encryptlis[i]=(char)字符码;
}
}
encryptlis[len]='\0';
返回加密LIS;
}

难道你不听编译器的警告吗?对于
gcc
,我使用
-Wall-Wextra-pedantic
。它确定了四个问题,包括最终导致SIGSEGV的问题。当使用
-Wall-Werror
标志编译代码时,您的代码会出现很多错误。你可以跟随他们来解决你的问题。我看不出我在哪里访问了非法内存位置,不,您正在访问非法内存位置。提示:
const char*string
将是一个更好的参数(该更改不需要进行其他更改),在堆栈上放置百万字符数组只是自找麻烦。现在典型的堆栈大小只有几兆字节。在示例代码中,没有理由将
str
val
设置为一百万字节。在从文件读取字符串的实代码中,您应该使用
malloc
realloc
为字符串分配堆内存。您拼写的Caesar不正确,您没有听到编译器的警告吗?对于
gcc
,我使用
-Wall-Wextra-pedantic
。它确定了四个问题,包括最终导致SIGSEGV的问题。当使用
-Wall-Werror
标志编译代码时,您的代码会出现很多错误。你可以跟随他们来解决你的问题。我看不出我在哪里访问了非法内存位置,不,您正在访问非法内存位置。提示:
const char*string
将是一个更好的参数(该更改不需要进行其他更改),在堆栈上放置百万字符数组只是自找麻烦。现在典型的堆栈大小只有几兆字节。在示例代码中,没有理由将
str
val
设置为一百万字节。在从文件读取字符串的实代码中,您应该使用
malloc
realloc
为字符串分配堆内存。您拼写的Caesar不正确