@freddix
Quote:
freddix wrote:
Imagine I have an array defined with this :
char WindowTitle[] = "MyArray";
and I want during execution for example, to change it by a new string
char NewTitle[] = "TitleEnteredByUser";
and I want to put NewTitle[] -> WindowTitle[]
Does someone have an idea ?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(VOID)
{
char *WindowTitle = strdup("mytitle");
char NewTitle[] = "TitleEnteredByUser";
free(WindowTitle);
WindowTitle = strdup(NewTitle);
printf("Title is: %s\n", WindowTitle);
free(WindowTitle);
return 0;
}
The strdup() function doesn't check for NULL pointers so you might want to create a NULL safe function like:
char *mystrdup(char *dupstring)
{
if (dupstring) return strdup(dupstring);
return NULL;
}