gpt4 book ai didi

c - 内存分配,输出垃圾

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:53 25 4
gpt4 key购买 nike

为什么这会输出垃圾而不是 arrayLetter 中的 char 值?当我将 for 循环打印在方法调用上方的 arrayLetter 时,它起作用了;当我把它放在它下面时,它不起作用。我没有在方法或 main 中更改 arrayLetter。这是一个简单的程序,可以移动索引并输出移动后的字母。

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

int* getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample);
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[]);
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample);

int main(void) {

char arrayLetter[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

int key[] = { 21, 4, 2, 19, 14, 17};

char plainTextExample[] = { 'h', 'e', 'r', 'e',
'i', 's',
'h', 'o', 'w',
'i', 't',
'w', 'o', 'r', 'k', 's'};

int sizeArrayLetter = sizeof(arrayLetter)/sizeof(arrayLetter[0]);
int sizePlainTextExample = sizeof(plainTextExample)/sizeof(plainTextExample[0]);
int sizeKey = sizeof(key)/sizeof(key[0]);

int *arrayMatchAtIndex;
int *encryptArray;
char *cipherArray;

arrayMatchAtIndex = getMatchedIndex(arrayLetter, plainTextExample, sizeArrayLetter, sizePlainTextExample);
encryptArray = vigenereCipherEncryptIndex(key, sizeKey, sizeArrayLetter, sizePlainTextExample, arrayMatchAtIndex);
cipherArray = convertToNewEncryptedArray(arrayLetter, encryptArray, sizeArrayLetter, sizePlainTextExample);

for(char i=0; i < sizeArrayLetter; i++)
{
//printf("%s%i%s", "Indexes of Encryption: ", encryptArray[i], " \n");
printf("%s", arrayLetter);

}


free(arrayMatchAtIndex);
free(encryptArray);
free(cipherArray);

return EXIT_SUCCESS;
}

//compare arrays and store index into array
int *getMatchedIndex(char arrayLetter[], char plainTextExample[],int sizeArrayLetter, int sizePlainTextExample)
{

int* arrayMatchAtIndex = malloc(sizePlainTextExample* sizeof(int));

//loop plainTextExample
for(int i=0; i < sizePlainTextExample ; i++)
{
//loop arrayLetter
for(int j=0; j < sizeArrayLetter ; j++)
{
//compare -> match then store arrayLetter index into arrayMatchAtIndex
if(plainTextExample[i] == arrayLetter[j])
{
arrayMatchAtIndex[i] = j;
}
}

}
return arrayMatchAtIndex;
}

//encrypt array by adding 'key' indexes into array
int* vigenereCipherEncryptIndex(int key[], int sizeKey, int sizeArrayLetter, int sizePlainTextExample, int arrayMatchAtIndex[])
{
//make new array
int* encryptArray = malloc(sizePlainTextExample* sizeof(int));

int j=0;

//encrypt element -> (add elements of arrayIndexes and key) mod 26
for(int i=0; i < sizePlainTextExample; i++)
{

if(i >= sizeKey)
{
j = (i % sizeKey);
key[i] = key[j];
}

encryptArray[i] = (arrayMatchAtIndex[i]+key[i]) % 26;
// printf("%s%d%s%d%s", "Match Element: ", arrayMatchAtIndex[i], " key ", key[i], " ");
// printf("%s%d%s", "encryptElement: ", encryptArray[i], " \n");
}

return encryptArray;
}

//convert encrypt array index to its elements
char* convertToNewEncryptedArray(char arrayLetter[], int encryptArray[], int sizeArrayLetter, int sizePlainTextExample)
{
char* cipherArray = malloc(sizePlainTextExample* sizeof(int));

//encrypted index - > convert elements to new index
for(int i=0; i < sizePlainTextExample ; i++)
{
for(int j=0; j< sizeArrayLetter; j++)
{
//find index->match then put Encrypted index into letter element
if(encryptArray[i] == j)
{
cipherArray[i] = arrayLetter[j];
printf("%c", arrayLetter[2]);
}

}
}

return cipherArray;
}

最佳答案

正如 Jonathan Leffler 所说的那样,您的数组只是一个数组,而不是 C 字符串(以 nul 结尾的字符序列)。仅将 %s 与 C 字符串一起使用。

将循环更改为:

for (char i=0; i<sizeArrayLetter; i++) {
putchar(arrayletter[i]);
}
putchar('\n');

这将单独打印数组中的所有字符和一个额外的行尾。

关于c - 内存分配,输出垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49271385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com