c - C 中的段冲突

标签 c pointers encryption

我正在编写一个加密引擎,当我尝试调用函数“generar_clave”时,gcc 说“段冲突”,我是 C 新手,需要帮助。

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

int clave[27];

typedef double t_clave;

t_clave claves[27][1000];
int clave[27];

char letras[]="abcdefghijklmnopqrstuvwxyz ";



void generar_clave(int);
void ingresar_clave(int []);
int comprobar_repetir_vector(int, int []);
int comprobar_repetir_matriz(int, t_clave [][1000]);
void arrancar_motor(int, int);
int suma(int);


void main() {
printf("pruebas de snakecode\n\n");

generar_clave(20);

}

int comprobar_repetir_matriz(int numero, t_clave matriz[][1000]) {

    int x;
    int y;
    for(x=0; x<1000; x++) {

        for(y=0;y<27;y++) {

            if (numero==matriz[x][y]) {

            return 1;   
            }
        }
    }
    return 0;

}

int comprobar_repetir_vector(int numero, int vector[]){

    int x;
    for(x=0; x<1000;x++) {

        if(numero==vector[x]) {

            return 1;

        }


    }

    return 0;



}


int suma(int numero) {

    int resultado=0;
    while (numero>0) {

        resultado+=numero%10;
        numero/=10;
    }
    return resultado;
}

void generar_clave(int numero_suma) {

    int maximo;
    int minimo;

    int max=120;
    int min=60;

    maximo=numero_suma*max/20;
    minimo=numero_suma*min/20;

    int x;

    int num_random;

    srand(time(NULL));

    printf("fuera del bucle");
    for(x=0;x<27;) {
        printf("dentro del bucle");
        num_random=minimo+rand() % (maximo+1-minimo);
        if (comprobar_repetir_vector(num_random, &clave[0])==1) {           
            continue;
        }
        clave[x]=num_random;
        x++;
    }

    int y;
    printf("se ha generado la clave: \n");
    for(y=0;x<27;y++) {

        printf("(%d)", clave[y]);

    }
    printf("\n");
}


void ingresar_clave(int array[]) {

int x;

for(x=0;x<27;x++) {

    clave[x]=array[x];
    }

}

void arrancar_motor(int numero_cifras, int cantidad_numeros){

    char max[1000];
    char min[1000]="1";

    int x;
    int y;
    int z;
    int h;
    int num_random;

    for(x=0; x<numero_cifras; x++) {

        strcat(max, "9");

    }

    for(y=0; y<numero_cifras-1; x++) {

        strcat(min, "0");

    }

    int maxi=atoi(max);
    int mini=atoi(min);


    for(z=0; z<27; z++) {
        printf("Inicializando letra %s", letras[z]);
        for(h=0; h<cantidad_numeros;) {
            num_random=mini+rand() % (maxi+1-mini);
            if ((suma(num_random)==clave[z]) && (comprobar_repetir_matriz(num_random, claves))==0) {

                claves[z][h]=num_random;
                printf("%d", num_random);
                h++;



            } else { continue;  }


        }
    }
}

我已经试过了,找不到可行的方法,也适用于引擎,除了这个失败,还有待完成,这是一个很简单的算法,我很早以前用python写过,想移植它到 C 学习 C。

最佳答案

char max[1000];
char min[1000]="1";

int x;
int y;
int z;
int h;
int num_random;

for(x=0; x<numero_cifras; x++) {

    strcat(max, "9");

strcat 将源字符串的副本附加到目标字符串,max 必须是 NUL 终止的字符串:

char max[1000] = {0};

在这一行中:

printf("Inicializando letra %s", letras[z]);

你想打印一个char(不是字符串),使用:

printf("Inicializando letra %c", letras[z]);

关于c - C 中的段冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699018/

相关文章:

c - 为什么会出现 'invalid or unsupported image format'错误?

c++ - 如何在动态矩阵中创建主键

c++ - 初始化指向 int 数组的指针

format - 这是哪种私钥格式?

c# - 如何使用 Rfc2898DeriveBytes 解密以下函数字符串?

Javascript加密文件上传

c - 通过C中的预取和缓存优化对阵列的线性访问

c++ - 尽可能快地渲染立方体? (OpenGL)

c - 如何在 Unix 中使用 wait() 作为非阻塞命令?

c++ - 派生类和基类之间指针到指针的转换?