Reputation: 764
I am trying to create processes for my project. I will pas arguments to child process from parent and the argument will change in time, so i wanted to make a try first with passing 1 to the child. The string format should be like this "childname.exe c" where c represents random character (in this case it is 1 for just trial).
I created a childname array and and all i wanted was concatenate the new string with childname string and copy it to another string array(lpCommandLine variable). When i debugged the code below i saw that child_name[0] (when i is equal to 0) returns only 'C' although i expected it to return "ChildProj1.exe". Is there a point that i missed or how to do it in c?
here there is a image of what i getin debugger: 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]);
}
Upvotes: 2
Views: 1106
Reputation: 311068
From your description it follows that you want to get strings like this
"childname.exe c"
However this loop
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]);
}
does not do what you want.
This loop has undefined behavior because in this statement
strcat_s(child_names[i], strlen(child_names[i]), " 1");
there is an attempt to modify a string literal. You may not change string literals neither in C nor in C++.
Moreover in this statement
strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]);
this call
strlen(lpCommandLine[i])
also has undefined behavior because array pointed to by this pointer lpCommandLine[i]
does not has the terminating zero.
There is no any need to use the functions strcat_s
and strcpy_s
. It is much better to use standard function strcat
and strcpy
.
What you is the following that is shown in this demonstrative program.
#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;
}
The program output is
ChildProj1.exe 1
ChildProj2.exe 1
ChildProj3.exe 1
Upvotes: 1
Reputation: 50190
to do the string concat do
size_t sz = strlen(child_names[i]) + 3; // space, '1' and \0
char *buff = malloc(sz);
strcat_s(buff,sz,child_names[i]);
strcat_s(buff,sz," 1");
Upvotes: 0
Reputation: 662
Instead of char * child_names[]
did you mean something like char[][] child_names
, char[] * child_names
, or char ** child_names
?
Upvotes: 0