Thread: *String to String[] to *String

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question *String to String[] to *String

    how can I copy a string like *String to a string like String[]? and vice-versa?

    thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    *String ---> String[]

    Code:
    #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);
             
    }
    String[] ---> *String

    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);
             
    }

  4. #4
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    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!

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So use dynamic memory allocation method. Once you know the length of the array do

    Code:
    char *str;
    
    str = malloc( sizeof char * len+1);
    FYI: If you still need increase the size use realloc function . I case if you wanted to increase the size in the future.

    EDIT: You need one more byte for null as well.
    ssharish2005
    Last edited by ssharish2005; 06-18-2007 at 04:34 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    don't forget to check for failure and to free it when you're done.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question

    but this is a pointer right?

    I'm 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?
    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; //I can't change this!
    char *NewStr;
    		   while (String[j] != '}'){ 
    			   NewStr[i] = String[j];
    			   i++;j++;
    		   }
    so I think if *NewStr was a NewStr[] will work... or am I wrong?

    thanks again!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question

    hmmm... so how I get the same result as the loop below using pointers?

    Code:
    char *String; 
    char *NewStr;
    		   while (String[j] != '}'){ 
    			   NewStr[i] = String[j];
    			   i++;j++;
    		   }
    thanks again!

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  11. #11

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your also not allocating enough room for the null terminator

  13. #13
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    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!

  14. #14
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question I can't even copy the string...

    remove everything, then I'm trying part by part... but I'm stuck in the first one.

    any of what you post worked.

    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);
    	//}
    }
    abort the game....

    I tryed these lines too:

    Code:
    HStr = malloc(sizeof (cm_entityString));
    HStr = malloc(sizeof char * cm_entityString);
    HStr = malloc(Tam);
    HStr  = Hunk_Alloc(sizeof(cm_entityString));
    nothing works... I'm doing nothing aside copy the string, why is not working?

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    ptr = malloc(sizeof(type) * elements);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive remove duplicates from a string
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-20-2009, 02:02 AM
  2. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  3. Help creating function - 2005
    By cx323 in forum C# Programming
    Replies: 1
    Last Post: 06-10-2006, 09:31 PM
  4. Java: Why isn't this allowed?
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-14-2005, 03:28 PM