从c中的字符串数组中获取第一个字符串
Getting first string from string array in c
我正在尝试为我的项目创建流程。我将从父进程向子进程传递参数,并且参数会及时改变,所以我想先尝试将 1 传递给子进程。字符串格式应该像这样 "childname.exe c" 其中 c 代表随机字符(在这种情况下它是 1 只是为了试用)。
我创建了一个子名数组,我想要的只是将新字符串与子名字符串连接起来,并将其复制到另一个字符串数组(lpCommandLine 变量)。当我调试下面的代码时,我看到 child_name[0](当我等于 0 时)return 仅 'C',尽管我预计它会 return "ChildProj1.exe".有没有我漏掉的地方或如何在 c 中做到这一点?
这里有一张我在调试器中得到的图像:here stored values of in variables
#define NO_OF_PROCESS 3
char *child_names[]= {"ChildProj1.exe", "ChildProj2.exe", "ChildProj3.exe" };
char* lpCommandLine[NO_OF_PROCESS];
int i;
for (i = 0; i < NO_OF_PROCESS; i++)
lpCommandLine[i] = (char *)malloc(sizeof(char) * 16);
for (i = 0; i < NO_OF_PROCESS; i++)
{
strcat_s(child_names[i], strlen(child_names[i]), " 1");
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
}
而不是 char * child_names[]
,您的意思是 char[][] child_names
、char[] * child_names
还是 char ** child_names
?
做字符串连接做
size_t sz = strlen(child_names[i]) + 3; // space, '1' and [=10=]
char *buff = malloc(sz);
strcat_s(buff,sz,child_names[i]);
strcat_s(buff,sz," 1");
根据您的描述,您希望获得这样的字符串
"childname.exe c"
然而这个循环
for (i = 0; i < NO_OF_PROCESS; i++)
{
strcat_s(child_names[i], strlen(child_names[i]), " 1");
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
}
没有按照你的意愿去做。
这个循环有未定义的行为,因为在这个语句中
strcat_s(child_names[i], strlen(child_names[i]), " 1");
试图修改字符串文字。您不能在 C 或 C++ 中更改字符串文字。
另外在这个声明中
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
这次通话
strlen(lpCommandLine[i])
也有未定义的行为,因为此指针指向的数组 lpCommandLine[i]
没有终止零。
不需要使用函数strcat_s
和strcpy_s
。最好使用标准函数 strcat
和 strcpy
.
下面这个演示程序中显示的是你
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_OF_PROCESS 3
int main(void)
{
const char * child_names[]=
{
"ChildProj1.exe",
"ChildProj2.exe",
"ChildProj3.exe"
};
const char *s = " 1";
size_t n = strlen( s );
char* lpCommandLine[NO_OF_PROCESS];
for ( int i = 0; i < NO_OF_PROCESS; i++ )
{
lpCommandLine[i] = ( char * )malloc( strlen( child_names[i] ) + n + 1 );
}
for ( int i = 0; i < NO_OF_PROCESS; i++ )
{
strcpy( lpCommandLine[i], child_names[i] );
strcat( lpCommandLine[i], s );
}
for ( int i = 0; i < NO_OF_PROCESS; i++ ) puts( lpCommandLine[i] );
for ( int i = 0; i < NO_OF_PROCESS; i++ ) free( lpCommandLine[i] );
return 0;
}
程序输出为
ChildProj1.exe 1
ChildProj2.exe 1
ChildProj3.exe 1
我正在尝试为我的项目创建流程。我将从父进程向子进程传递参数,并且参数会及时改变,所以我想先尝试将 1 传递给子进程。字符串格式应该像这样 "childname.exe c" 其中 c 代表随机字符(在这种情况下它是 1 只是为了试用)。
我创建了一个子名数组,我想要的只是将新字符串与子名字符串连接起来,并将其复制到另一个字符串数组(lpCommandLine 变量)。当我调试下面的代码时,我看到 child_name[0](当我等于 0 时)return 仅 'C',尽管我预计它会 return "ChildProj1.exe".有没有我漏掉的地方或如何在 c 中做到这一点?
这里有一张我在调试器中得到的图像:here stored values of in variables
#define NO_OF_PROCESS 3
char *child_names[]= {"ChildProj1.exe", "ChildProj2.exe", "ChildProj3.exe" };
char* lpCommandLine[NO_OF_PROCESS];
int i;
for (i = 0; i < NO_OF_PROCESS; i++)
lpCommandLine[i] = (char *)malloc(sizeof(char) * 16);
for (i = 0; i < NO_OF_PROCESS; i++)
{
strcat_s(child_names[i], strlen(child_names[i]), " 1");
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
}
而不是 char * child_names[]
,您的意思是 char[][] child_names
、char[] * child_names
还是 char ** child_names
?
做字符串连接做
size_t sz = strlen(child_names[i]) + 3; // space, '1' and [=10=]
char *buff = malloc(sz);
strcat_s(buff,sz,child_names[i]);
strcat_s(buff,sz," 1");
根据您的描述,您希望获得这样的字符串
"childname.exe c"
然而这个循环
for (i = 0; i < NO_OF_PROCESS; i++)
{
strcat_s(child_names[i], strlen(child_names[i]), " 1");
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
}
没有按照你的意愿去做。
这个循环有未定义的行为,因为在这个语句中
strcat_s(child_names[i], strlen(child_names[i]), " 1");
试图修改字符串文字。您不能在 C 或 C++ 中更改字符串文字。
另外在这个声明中
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
这次通话
strlen(lpCommandLine[i])
也有未定义的行为,因为此指针指向的数组 lpCommandLine[i]
没有终止零。
不需要使用函数strcat_s
和strcpy_s
。最好使用标准函数 strcat
和 strcpy
.
下面这个演示程序中显示的是你
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_OF_PROCESS 3
int main(void)
{
const char * child_names[]=
{
"ChildProj1.exe",
"ChildProj2.exe",
"ChildProj3.exe"
};
const char *s = " 1";
size_t n = strlen( s );
char* lpCommandLine[NO_OF_PROCESS];
for ( int i = 0; i < NO_OF_PROCESS; i++ )
{
lpCommandLine[i] = ( char * )malloc( strlen( child_names[i] ) + n + 1 );
}
for ( int i = 0; i < NO_OF_PROCESS; i++ )
{
strcpy( lpCommandLine[i], child_names[i] );
strcat( lpCommandLine[i], s );
}
for ( int i = 0; i < NO_OF_PROCESS; i++ ) puts( lpCommandLine[i] );
for ( int i = 0; i < NO_OF_PROCESS; i++ ) free( lpCommandLine[i] );
return 0;
}
程序输出为
ChildProj1.exe 1
ChildProj2.exe 1
ChildProj3.exe 1