You can try something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[] )
{
char *path;
const char *home = getenv("HOME");
path = malloc(strlen(home) + 1);
if (!path) {
printf("Error\n");
return 0;
}
strcpy(path, home);
printf("path = %s\n", path);
// if you want to have argv[1] concatenated with path
if (argc >= 2) {
path = malloc(strlen(home) + strlen(argv[1]) + 1);
strcpy(path, argv[1]);
strcat(path, home);
printf("%s\n", path);
}
// if you want an array of strings, each containing path, argv[1]...
char **array;
int i;
array = malloc(argc * sizeof(char*));
array[0] = malloc(strlen(home) + 1);
strcpy(array[0], home);
printf("array[0] = %s\n", array[0]);
for (i = 1; i < argc; i++) {
array[i] = malloc(strlen(argv[i]) + 1);
strcpy(array[i], argv[i]);
printf("array[%d] = %s\n", i, array[i]);
}
// now array[i] will hold path and all the argv strings
return 0;
}
Just as above, your path[51] is a string while path[1] is only a character, so you can't use strcpy for that.
Bookmarks