how can I copy a string like *String to a string like String[]? and vice-versa?
thanks!
how can I copy a string like *String to a string like String[]? and vice-versa?
thanks!
strcpy
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
*String ---> String[]
String[] ---> *StringCode:#include<stdio.h> #include<string.h> main(void) { char *String1 = "Program"; int len = strlen(String1); char String2[len]; strcpy(String2,String1); printf("%s = %s", String1,String2); getchar(); return(0); }
Code:#include<stdio.h> #include<string.h> main(void) { char String1[] = "Program"; char *String2; strcpy(String2,String1); printf("%s = %s", String1,String2); getchar(); return(0); }
thanks... but I have a little problem here.
I will only know the *String after few lines of code... so I can't know the lenght of this when I declare the strings... is possible to change the size after declare? so how can I do that?
thanks again!
So use dynamic memory allocation method. Once you know the length of the array do
FYI: If you still need increase the size use realloc function . I case if you wanted to increase the size in the future.Code:char *str; str = malloc( sizeof char * len+1);
EDIT: You need one more byte for null as well.
ssharish2005
Last edited by ssharish2005; 06-18-2007 at 04:34 AM.
don't forget to check for failure and to free it when you're done.
but this is a pointer right?
I'm like this:
well, I need to read/copy char by char of this string to another, but seams that I can't do it like this:Code:char *String; char NewStr[]; do some lines of code than I get *String; len = strlen(String); NewStr[] = malloc( sizeof char * len); // can I do that?
so I think if *NewStr was a NewStr[] will work... or am I wrong?Code:char *String; //I can't change this! char *NewStr; while (String[j] != '}'){ NewStr[i] = String[j]; i++;j++; }
thanks again!
NewStr[] is an array with an undetermined number of elements. It won't take a value returned from malloc. You'll need a pointer for that.
hmmm... so how I get the same result as the loop below using pointers?
thanks again!Code:char *String; char *NewStr; while (String[j] != '}'){ NewStr[i] = String[j]; i++;j++; }
but you stilll need a malloc
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Your also not allocating enough room for the null terminator
ok, I need do a malloc... but about my loop? is right?
is just because I do not alloc/free mem that didn't work? or what I did isn't possible to do with pointers like I did? or BOTH???
thanks again!
remove everything, then I'm trying part by part... but I'm stuck in the first one.
any of what you post worked.
abort the game....Code:/* ================= CM_LoadEntityString ================= */ static void CM_LoadEntityString (const byte *data, const lump_t *l){ char *game = Cvar_VariableString("fs_game"); // declare game - Hajas FILE *f, *g; int i, j, Tam, TamNew; int PA, PB, PC, PD, PL, Num, Espaco, Aspas; char *p = NULL, *ori = NULL, *HStr = NULL, *NewStr = NULL, *FinalStr = NULL; char *PartA = NULL, *PartB = NULL, *PartC = NULL, *PartD = NULL; cm_numEntityChars = l->fileLen; if (cm_numEntityChars < 1) return; if (cm_numEntityChars > MAX_MAP_ENTSTRING) Com_Error(ERR_DROP, "CM_LoadMap: map '%s' has too large entity lump", cm_map); cm_entityString = Hunk_Alloc(cm_numEntityChars + 1); memcpy(cm_entityString, data + l->fileOfs, cm_numEntityChars); f = fopen ("entityORI.cfg", "wb"); fprintf(f, "%s\n", cm_entityString); fclose(f); //if (!Q_stricmp(game, "arena")){ // do string manipulation over cm_entityString *NewStr = 0; *FinalStr = 0; Tam = strlen(cm_entityString); HStr = malloc(sizeof cm_entityString); strcpy(HStr, cm_entityString); // copy the original string to mine g = fopen ("entityALT.cfg", "wb"); fprintf(g, "%s\n", HStr); fclose(g); free(HStr); //} }
I tryed these lines too:
nothing works... I'm doing nothing aside copy the string, why is not working?Code:HStr = malloc(sizeof (cm_entityString)); HStr = malloc(sizeof char * cm_entityString); HStr = malloc(Tam); HStr = Hunk_Alloc(sizeof(cm_entityString));
Code:ptr = malloc(sizeof(type) * elements);