Basically I'm trying to make a function of the sort:
Code:
/* changeto() is the function I'm interested in. */
void changeto(type to, const char *from, size_t len)
{
if ((to == malloc(len+1)) == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
strncpy(to, from, len);
}
int main(int argc, char **argv)
{
char *buf;
if (argc != 2) {
printf("Usage: %s <char string>\n", argv[0]);
return EXIT_FAILURE;
}
changeto(buf, argv[1]);
printf("%s\n", buf);
return EXIT_SUCCESS;
}
What is "changeto()" supposed to do? Allocate memory, initialize it and make buf point to that portion of memory. However I can't figure out the "type" of "to".
Normally, if I'd want a function to modify an int, I'd so something like this:
Code:
void change(int *px)
{
*px = 3;
}
int main(int argc, char **argv)
{
int x;
change(&x);
return EXIT_SUCCESS;
}
but how would I go about modifying a pointer to char? [And make it point to a memory portion allocated and initialized by another function]
Any ideas?