Recreating string array from the same variable?

This is a discussion on Recreating string array from the same variable? within the C Programming forums, part of the General Programming Boards category; What's the easiest way to reuse the string array? I don't want to keep creating a different string array :( ...

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Recreating string array from the same variable?

    What's the easiest way to reuse the string array? I don't want to keep creating a different string array :(
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
    	char *Choices[] = {
    		"Birds",
    		"Sing",
    		"And"
    	};
    	
    	/*Choices = {		<-- I guess it's harder than I thought :(
    		"Birds Sing",
    		"Wind blows",
    		"Suns bright"
    	};*/
    	
    	return 0;
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    string arrays

    Scroll down until you see Quzah's post. I think it will shed some light on your problem.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    If I didn't come to these forums, I would've hit a brick wall.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define CHOICE_LENGTH 20
    #define TRUE 1
    #define FALSE 0
    
    char Choices[5][CHOICE_LENGTH];
    int changeValues(char *szOne, char *szTwo, char *szThree, char *szFour, char *szFive);
    void showValues();
    
    int main(void) {
    
    	if(changeValues(	"Birds Singing", 
    							"Happiness Everywhere",
    							"All colors",
    							"Rainbow clouds",
    							"OMG A ZOMBIE")) 
    	{
    		showValues();
    	}
    	
    	return 0;
    }
    
    int changeValues(char *szOne, char *szTwo, char *szThree, char *szFour, char *szFive) {
    	if( strlen(szOne) > CHOICE_LENGTH ||
    		 strlen(szTwo) > CHOICE_LENGTH ||
    		 strlen(szThree) > CHOICE_LENGTH ||
    		 strlen(szFour) > CHOICE_LENGTH ||
    		 strlen(szFive) > CHOICE_LENGTH) 
    	{
    		return FALSE;
    	}
    	strcpy(Choices[0], szOne);
    	strcpy(Choices[1], szTwo);
    	strcpy(Choices[2], szThree);
    	strcpy(Choices[3], szFour);
    	strcpy(Choices[4], szFive);
    	return TRUE;
    }
    
    void showValues() {
    	int i;
    	for(i=0; i<5; i++)
    		printf("%s\n", Choices[i]);
    }
    Thank you :)

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    arrays of ptrs to char can be a bit confusing in C. I assume you read the post the previous poster told you to read. In your case one thing to remember is that a
    Code:
    char *
    is interpreted as a pointer to a character. in C this is stored in ROM and though it can be accessed via array notation. the string is immutable meaning you can't change the individual elements, BUT, you can make each ptr point somwhere else.
    example
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        int ndx;
        char *addon[]= {"Birds sing", "Wind blows", "and Suns bright"};
        char *Choices[] = {
    		"Birds",
    		"Sing",
    		"And"
    	};
        
        
        puts("Originally choices contain this: ");
    	
    	for(ndx=0; ndx<3; ndx++)
          puts(Choices[ndx]);
          
          
       putchar('\n');
     
    	
       
    	
    	puts("Now choices point to these: ");
    	for(ndx=0; ndx<3; ndx++)
           Choices[ndx] = addon[ndx];
          
         /*show the contents of modified array */
         for(ndx=0; ndx<3; ndx++)
          puts(Choices[ndx]); 
    	getchar();
    	return 0;
    }
    I think this might be along the lines of what you were looking for.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    That shed some pretty good light on what I needed. But I'm going to go with the previous solution because the game that I need this on can wait for that kind of structure, thanks though. You did help me make the concept of pointers more clearer, I'll keep that in mind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Replies: 1
    Last Post: 07-24-2002, 06:33 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21