我有一个定义好的数组示例:
char *arguments[] = {"test-1","test-2","test-3"};
我正在尝试添加命令行给出的参数输入。我尝试了 strcpy
函数并通过数组元素传递它,例如arguments[num+1] = argv[1]
但还是没有成功。
我知道这是一个非常简单的问题,但我不是经验丰富的程序员,我的所有经验都来自高级编程语言(PHP、Perl)。
我在网上找到的最接近的工作样本是 C program to insert an element in an array和 C program to delete an element from an array .但并不是我正在寻找的东西,他们使用的是 intigers 而不是我需要的字符。
我的目标是找到一种在动态数组中添加和删除字符串的方法,该数组可以根据脚本的进程增长和收缩。
感谢大家花时间和精力帮助我。
工作代码示例如下:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* Set as minimum parameters 2 */
#define MIN_REQUIRED 2
#define MAX_CHARACTERS 46
/* Usage Instructions */
int help() {
printf("Usage: test.c [-s <arg0>]\n");
printf("\t-s: a string program name <arg0>\n");
printf("\t-s: a string sample name <arg1>\n");
return (1);
}
int main(int argc, char *argv[]) {
if ( argc < MIN_REQUIRED ) {
printf ("Please follow the instructions: not less than %i argument inputs\n",MIN_REQUIRED);
return help();
}
else if ( argc > MIN_REQUIRED ) {
printf ("Please follow the instructions: not more than %i argument inputs\n",MIN_REQUIRED);
return help();
}
else {
int size, realsize;
char *input = NULL;
char *arguments[] = {"test-1","test-2","test-3"};
int num = sizeof(arguments) / sizeof(arguments[0]);
printf("This is the number of elements before: %i\n",num);
int i;
for (i=0; i<num; i++) {
printf("This is the arguments before: [%i]: %s\n",i,arguments[i]);
}
printf("This is the input argument: %s\n",argv[1]);
printf("This is the array element: %i\n",num+1);
input = (char *)malloc(MAX_CHARACTERS);
if (input == NULL) {
printf("malloc_in failled\n");
exit(0);
}
memset ( input , '\0' , MAX_CHARACTERS);
int length_before = strlen(input);
printf("This is the length before: %i\n",length_before);
strcpy(input , argv[1]);
int length_after = strlen(input);
printf("This is the length after: %i\n",length_after);
//arguments[num+1] = input;
strcpy(arguments[num+1],input);
int num_2 = sizeof(arguments) / sizeof(arguments[0]);
printf("This is the number of elements after: %i\n",num);
for (i=0; i<num_2; i++) {
printf("This is the arguments after [%i]: %s\n",i,arguments[i]);
}
} // End of else condition
return 0;
} // Enf of int main ()
最佳答案
“我的目标是找到一种在动态数组中添加和删除字符串的方法”:
char *arguments[] = {...}
是静态分配的,因此不能用作“动态数组”。strcpy(参数[num+1],输入)
:当此数组只有
num
个条目时,您无法访问arguments[num+1]
。
建议的修复 - 根据 argc
的值动态分配和初始化 arguments
:
char* strings[] = {"test-1","test-2","test-3"};
int i, num = sizeof(strings) / sizeof(*strings);
char** arguments = malloc((num+argc-1)*sizeof(char*));
if (arguments == NULL)
; // Exit with a failure
for (i=0; i<num; i++)
{
arguments[i] = malloc(strlen(strings[i])+1);
if (arguments[i] == NULL)
; // Deallocate what's already been allocated, and exit with a failure
strcpy(arguments[i],strings[i]);
}
for (i=0; i<argc-1; i++)
{
arguments[num+i] = malloc(strlen(argv[i+1])+1);
if (arguments[num+i] == NULL)
; // Deallocate what's already been allocated, and exit with a failure
strcpy(arguments[num+i],argv[i+1]);
}
...
// Deallocate everything before ending the program
关于c - c语言如何在动态数组中添加/删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961146/