如何使用作为 argv[] 中的命令行参数传递的字符串填充新数组?
How can I populate a new array with strings passed as command line arguments in argv[]?
我对编码比较陌生,尤其是在 C 方面。我正在尝试创建一个简单的程序,旨在将短语或字符串列表作为填充 char* argv[] 的命令行参数。然后我想对每个字符执行简单的基于 ASCII 的更改(例如制作 a => b、b => c、... z => a 等),然后用更改后的字符串填充一个新数组。本质上,新对象将是字符数组的数组,就像我相信 argv[] 一样。
这是我的代码,虽然我被各种“指针不兼容的转换赋值 **char 到 string int char 变量......分段错误”风格的错误轰炸。
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* cipher[argc];
for (int i = 1; i < argc; i++)
{
int n = strlen(argv[i]);
for (int j = 0; j < n-1; j++)
{
cipher[i][j] = argv[i][j];
}
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
}
代码将通过命令行 运行 如下:
./secret the man in the middle is guilty
然后 return 这是一个乱序的形式。我省略了加扰算法,因为这不是我遇到的问题。
我知道我可以更轻松地创建代码来打印更改后的消息,但我希望能够将新消息存储在数组中以备进一步更改。我不明白为什么我不能只访问特定二维索引的字符值并用它们填充新的“密码”数组。
我哪里不明白?
一堆问题:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
// first issue: this is only allocating space for "pointers" to
// strings [argc many] but no space for actual strings so you have to
// allocate them.
// also this is only really workable with c99 (assuming that's the case)
char* cipher[argc];
// this is okay
for (int i = 1; i < argc; i++)
{
// this is where you would allocate space and
// perform the copy
int n = strlen(argv[i]);
for (int j = 0; j < n-1; j++)
{
cipher[i][j] = argv[i][j];
}
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
}
所以至少最低限度:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* cipher[argc];
// this is okay
for (int i = 1; i < argc; i++)
{
// strdup will return a newly allocated copy
// and since cipher[] is an array of pointers
// you are good to go.
cipher[i-1] = strdup(argv[i]);
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
// later on after you are finished with cipher
// assuming that you still have other things to do
// you can de-allocate the memory (although not sure if
// that will be required)
for(int i = 0; i < argc - 1; i++) {
free(cipher[i]); // strdup allocated memory cleanup
}
// you can no longer use cipher[i] here because it's contents
// have been destroyed by the free above.
}
我对编码比较陌生,尤其是在 C 方面。我正在尝试创建一个简单的程序,旨在将短语或字符串列表作为填充 char* argv[] 的命令行参数。然后我想对每个字符执行简单的基于 ASCII 的更改(例如制作 a => b、b => c、... z => a 等),然后用更改后的字符串填充一个新数组。本质上,新对象将是字符数组的数组,就像我相信 argv[] 一样。
这是我的代码,虽然我被各种“指针不兼容的转换赋值 **char 到 string int char 变量......分段错误”风格的错误轰炸。
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* cipher[argc];
for (int i = 1; i < argc; i++)
{
int n = strlen(argv[i]);
for (int j = 0; j < n-1; j++)
{
cipher[i][j] = argv[i][j];
}
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
}
代码将通过命令行 运行 如下:
./secret the man in the middle is guilty
然后 return 这是一个乱序的形式。我省略了加扰算法,因为这不是我遇到的问题。
我知道我可以更轻松地创建代码来打印更改后的消息,但我希望能够将新消息存储在数组中以备进一步更改。我不明白为什么我不能只访问特定二维索引的字符值并用它们填充新的“密码”数组。
我哪里不明白?
一堆问题:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
// first issue: this is only allocating space for "pointers" to
// strings [argc many] but no space for actual strings so you have to
// allocate them.
// also this is only really workable with c99 (assuming that's the case)
char* cipher[argc];
// this is okay
for (int i = 1; i < argc; i++)
{
// this is where you would allocate space and
// perform the copy
int n = strlen(argv[i]);
for (int j = 0; j < n-1; j++)
{
cipher[i][j] = argv[i][j];
}
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
}
所以至少最低限度:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* cipher[argc];
// this is okay
for (int i = 1; i < argc; i++)
{
// strdup will return a newly allocated copy
// and since cipher[] is an array of pointers
// you are good to go.
cipher[i-1] = strdup(argv[i]);
}
for (int i = 0; i<argc-1; i++)
{
printf("%s\n", cipher[i]);
}
// later on after you are finished with cipher
// assuming that you still have other things to do
// you can de-allocate the memory (although not sure if
// that will be required)
for(int i = 0; i < argc - 1; i++) {
free(cipher[i]); // strdup allocated memory cleanup
}
// you can no longer use cipher[i] here because it's contents
// have been destroyed by the free above.
}