The line
strcpy(closure, res[0]);
will copy the string in res[0]
to closure
.
You probably want to do the opposite. You want to copy the string in closure
to res[0]
. In that case, you should swap the two arguments, by changing the line to the following:
strcpy( res[0], closure );
Another problem is that using the function strcpy
requires the second argument to actually be a string (i.e. a sequence of characters terminated by a null character). If it is not, then your program will be invoking undefined behavior (i.e. your program may crash).
The content of closure
is not a string, because the line
scanf(" %c", closure);
will only write a single character to closure
. It won't write a terminating null character afterwards.
If you want to make closure
a valid string, then all you have to do is add a terminating null character after the character, like this:
closure[1] = '\0';
On the other hand, if you want to read a whole line of input as a string, then you could use the function fgets
instead:
fgets( closure, sizeof closure, stdin );
Note that the function fgets
will also read the newline character at the end of the line, and store it as part of the string. If you do not want this, see the following question for different possibilities on how to remove this newline character:
Removing trailing newline character from fgets() input
Another problem in your code is that the following loop is wrong:
for (i = 0; i < fdcount; i++) {
printf(" %s", res[i]);
}
When using printf
with the %s
conversion format specifier, the argument must be a valid null-terminated string. Since you are calling printf
in a loop, the contents of res[0]
up to res[fdcount-1]
must contain valid strings. However, even after making the fixes mentioned above, only res[0]
will be a valid string with a null terminating character. The content of res[1]
to res[fdcount-1]
will still contain garbage data. Therefore, you should not attempt to treat this garbage data as strings, because doing so will invoke undefined behavior.
Here is a short demonstration program which reads a line of input into closure
, copies it, and then prints the copy:
#include <stdio.h>
#include <string.h>
int main( void )
{
int fdcount = 20;
char closure[100];
char res[fdcount][100];
//prompt user for input
printf( "Please enter a line of input: " );
//read one line of user input
fgets( closure, sizeof closure, stdin );
//remove newline character from input
closure[strcspn(closure,"\n")] = '\0';
//copy input to res[0]
strcpy( res[0], closure );
//print copy
printf( "Contents of res[0]: %s\n", res[0] );
}
This program has the following behavior:
Please enter a line of input: This is a test.
Contents of res[0]: This is a test.
Of course, it does not make much sense to create an array of strings, if you are only using one string.
Here is the same program with full error checking:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
int fdcount = 20;
char closure[100];
char res[fdcount][100];
char *p;
//prompt user for input
printf( "Please enter a line of input: " );
//attempt to read one line of user input
if ( fgets( closure, sizeof closure, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//verify that input buffer was large enough to store entire line
p = strchr( closure, '\n' );
if ( p == NULL )
{
if ( !feof( stdin ) )
{
fprintf( stderr, "Line too long to fit into buffer!\n" );
exit( EXIT_FAILURE );
}
}
else
{
//remove newline character from input
*p = '\0';
}
//copy input to res[0]
strcpy( res[0], closure );
//print copy
printf( "Contents of res[0]: %s\n", res[0] );
}
Please ignore the rest of the scuffed code.
The rest of your code is hard to ignore, because it is also invoking undefined behavior, so that the behavior of your entire program is undefined (i.e. your program may crash).
The lines
strcpy(left[i], temp);
and
strcpy(right[i], temp);
require temp
to be a string, which it is not, because it is not terminated by a null character.