为什么 argv[1] 打印大写字符?
Why is argv[1] printing uppercase characters?
我将第二个参数 argv[1] 转换为大写,以便检查是否有任何重复字符。为此,我将 argv[1] 中的所有内容放入一个名为“key”的字符串中,然后将“key”中的所有内容转换为大写。现在“key”中的所有内容都与 argv[1] 相同,但都是大写的。但是,当我打印 argv[1] 时,它也被转换为大写。我需要 argv[1] 中的所有内容保持不变。有谁知道我做错了什么?
另外请注意,我使用的是 cs50 库中的“字符串”。
谢谢。
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
const int key_length = 26;
int string_length = 0;
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Key must contain only alphabetic characters.\n");
return 2;
}
else
{
string_length += 1;
}
}
if (string_length != key_length)
{
printf("Key must contain 26 characters.\n");
return 3;
}
string key = argv[1]; // converting key letters here
for (int a = 0; a < strlen(key); a++)
{
if (!isupper(key[a]))
{
key[a] = toupper(key[a]);
}
}
for (int l = 0; l < strlen(key); l++)
{
for (int h = l + 1; h < strlen(key); h++)
{
if (key[l] == key[h])
{
printf("No duplicates in key: %c\n", key[l]);
return 4;
}
}
}
}
string key = argv[1];
不会将argv[1]
指向的字符复制到key
.
类型 string
被定义为 char *
,指向 char
的指针。 argv[1]
也是指向 char
的指针。 string key = argv[1];
创建一个名为 key
的新指针并将其设置为指向 argv[1]
指向的相同位置。
然后,当您将 key
指向的字符更改为大写时,您也会更改 argv[1]
指向的字符,因为它们是相同的字符。
要使用角色副本,您必须自己制作副本:
string key = malloc(strlen(argv[1] + 1); // Allocate space for characters in argument plus null terminator.
if (!key) // Test whether allocation succeeded.
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
strcpy(key, argv[1]); // Copy characters.
我不明白为什么 CS50 作者会做这种可怕的事情。
string
是一个简单的 char *
.
string key = argv[1];
您的问题是将指针隐藏在 typedef 后面的效果。
key
引用与 argv
相同的内存位置
您需要在 key
中分配对应于 argv[1]
长度的内存(+1 表示字符串终止符):
string key = malloc(strlen(argv[1]) + 1);
/* check if the allocation was OK - I will skip it for the call clarity */
strcpy(key, argv[1]);
我将第二个参数 argv[1] 转换为大写,以便检查是否有任何重复字符。为此,我将 argv[1] 中的所有内容放入一个名为“key”的字符串中,然后将“key”中的所有内容转换为大写。现在“key”中的所有内容都与 argv[1] 相同,但都是大写的。但是,当我打印 argv[1] 时,它也被转换为大写。我需要 argv[1] 中的所有内容保持不变。有谁知道我做错了什么? 另外请注意,我使用的是 cs50 库中的“字符串”。 谢谢。
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
const int key_length = 26;
int string_length = 0;
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Key must contain only alphabetic characters.\n");
return 2;
}
else
{
string_length += 1;
}
}
if (string_length != key_length)
{
printf("Key must contain 26 characters.\n");
return 3;
}
string key = argv[1]; // converting key letters here
for (int a = 0; a < strlen(key); a++)
{
if (!isupper(key[a]))
{
key[a] = toupper(key[a]);
}
}
for (int l = 0; l < strlen(key); l++)
{
for (int h = l + 1; h < strlen(key); h++)
{
if (key[l] == key[h])
{
printf("No duplicates in key: %c\n", key[l]);
return 4;
}
}
}
}
string key = argv[1];
不会将argv[1]
指向的字符复制到key
.
类型 string
被定义为 char *
,指向 char
的指针。 argv[1]
也是指向 char
的指针。 string key = argv[1];
创建一个名为 key
的新指针并将其设置为指向 argv[1]
指向的相同位置。
然后,当您将 key
指向的字符更改为大写时,您也会更改 argv[1]
指向的字符,因为它们是相同的字符。
要使用角色副本,您必须自己制作副本:
string key = malloc(strlen(argv[1] + 1); // Allocate space for characters in argument plus null terminator.
if (!key) // Test whether allocation succeeded.
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
strcpy(key, argv[1]); // Copy characters.
我不明白为什么 CS50 作者会做这种可怕的事情。
string
是一个简单的 char *
.
string key = argv[1];
您的问题是将指针隐藏在 typedef 后面的效果。
key
引用与 argv
您需要在 key
中分配对应于 argv[1]
长度的内存(+1 表示字符串终止符):
string key = malloc(strlen(argv[1]) + 1);
/* check if the allocation was OK - I will skip it for the call clarity */
strcpy(key, argv[1]);