2

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]);
    }
5
  • where do you think the new string "<childproces> 1" is going to be stored. strcat_s failed becuase you tried to add characters to the buffer - check the return value of srcat_s Commented Nov 16, 2016 at 23:30
  • @Y.E.S. It is unclear what you are going to get in the array lpCommandLine. Show its result content. Commented Nov 16, 2016 at 23:44
  • I want it to be stored in lpCommandLine. In child_names array 0th string is "ChildProj1.exe" and i want lpCommandLine[0] to be "ChildProj1.exe 1". So do you suggest to allocate memory for each child_names indexes for 16 chars which means ChildProj1.exe + 3 (for blank and 1 and \0) Commented Nov 16, 2016 at 23:46
  • @vlad-from-moscow it is not necessary to know what i am trying to do with lpCommandLine array. All i want is concatenate " 1" with strings of array child_names and copy each string to lpCommandLine array. Commented Nov 16, 2016 at 23:48
  • @Y.E.S. It seems I have understood what you want. See my answer.:) Commented Nov 17, 2016 at 0:21

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of char * child_names[] did you mean something like char[][] child_names, char[] * child_names, or char ** child_names?

2 Comments

I want to store strings in that array, already tried char[ ][ ], char** but didn't work.
with char* child_names[ ], i tried to print each element and it printed correctly with this code printf("%s", child_names[i]);. It prints each string like this but when i want to do concatenating things go crazy.
0

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");

2 Comments

your issue about childname[0] = 'C' is a red herring. YOu just need to display it as a string rather than a char in the debugger
I just tried it now and in buff there is only "\0" it says. If you have time you can try it with your compiler, i wonder if there is a problem with my compiler now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.