Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
116 user(s) are online (107 user(s) are browsing Forums)

Members: 1
Guests: 115

thellier, more...

Support us!

Headlines

 
  Register To Post  

« 1 (2)
Re: Resize an array in C
Home away from home
Home away from home


See User information
@thomas

Quote:

thomas wrote:
@freddix
In order to keep it as simple as possible I would do something like this:



char WindowTitle[256] = "MyTitle";
.
.
.
strcpy (WindowTitle,"TitleEnteredByUser");



Just make sure that the user cannot enter more than 255 characters.


Sure that might save you a few hours now, but it's really easy for subtle bugs to be created this way, and it's not generic. Spending a few hours creating a string ADT with appropriate manipulation functions can save a lot of heart-ache later. Plus, it's more generic, so you can use it everywhere that you use strings.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: Resize an array in C
Just can't stay away
Just can't stay away


See User information
@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;
}

Go to top

  Register To Post
« 1 (2)

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project