Thread: Variable String Arguments

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Variable String Arguments

    Ok here is the problem I need to sent to a function a number for example 2. This is the amount of strings I wish to enter into an array of a struct.

    The struct for example may have 3 char* like this.

    typedef struct
    {
    char *word[3];
    }Example

    Now I wanna be able to say 2 and it go into a function that takes that amount of variables(I believe with stdarg).

    Example help;

    thewordsiwant(&help, 2, "hello", "goodbye");

    How would I go about implementing this to make hello and goodbye go into char*word?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, you'd want to look up va_list, va_start, va_arg, etc. Then you'd prototype your function something like this:
    Code:
    void foo( struct mystruct *bar, int count, ... );
    Then you'd use the above correctly, and combine it with a nice little loop, doing your malloc / strcpy, or whatever it is you're intending on doing to get your strings where you want them.

    Finally, when posting code, use [code] tags!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Here is my suggestion:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
    #include <stdlib.h>
    
    #define MAX 10
    
    typedef struct
    {
    	char *word[MAX];
    }Example;
    
    void thewordsiwant(Example*,int,const char*,...);
    int main()
    {
    	int i;
    	Example help;
    	thewordsiwant(&help,2,"First","Second");
    	for(i=0;i<2;i++)
    		printf("%s\n",help.word[i]);
    	return 0;
    }
    void thewordsiwant(Example* ex,int num,const char* szFirst,...)
    {	
    	int count=0,i=1;
    	va_list marker;
    	va_start(marker,szFirst );  
    	ex->word[0]=(char*)malloc(strlen(szFirst)+1);
    	strcpy(ex->word[0],szFirst);
    	count++;
    	while( count != num )
       {
          count++;
          szFirst = va_arg( marker, char*);
    	ex->word[i]=(char*)malloc(strlen(szFirst)+1);
    	strcpy(ex->word[i],szFirst);
    	i++;
       }
    
    va_end( marker );              
    	
    }
    This is mybe wrong but it seems to works!
    I wrote this with best intentions, so pleas don't give me bad reputation if you don't understand or there is some bug in the code like some people do, after all we're all learning here!
    Last edited by Micko; 09-11-2004 at 03:39 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The function would be much better implemented as a means to just grow the array. Initialization of it should be handled before the function is ever called. That way you could call it over and over again to keep adding to it:

    addwords(&help, 2, "hello", "goodbye");
    addwords(&help, 4, "add", "these", "words", "also");

    And then the array would have all 6 words in it.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Oh well. Since you've already seen one way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    
    struct strings_s
    {
      int n_strings;
      char **strings;
    };
    
    void add_strings(struct strings_s *strs, int count, ...)
    {
      va_list ap;
      int i;
      char *s;
    
      if(count < 1)
        return;
    
      if(!strs->n_strings)
        strs->strings = NULL;
    
      if(!(strs->strings =
        realloc(strs->strings, sizeof(char *)*(strs->n_strings+count))))
      {
        puts("Memory allocation failure");
        exit(EXIT_FAILURE);
      }
    
      va_start(ap, count);
    
      for(i = 0;i < count;++i)
      {
        s = va_arg(ap, char *);
        if(!(strs->strings[strs->n_strings] = malloc(strlen(s)+1)))
        {
          puts("Memory allocation failure");
          exit(EXIT_FAILURE);
        }
        strcpy(strs->strings[strs->n_strings], s);
        strs->n_strings++;
      }
    
      va_end(ap);
    }
    
    void show_strings(struct strings_s *strs)
    {
      int i;
    
      for(i = 0;i < strs->n_strings;++i)
        puts(strs->strings[i]);
    }
    
    void free_strings(struct strings_s *strs)
    {
      int i;
    
      for(i = 0;i < strs->n_strings;++i)
        free(strs->strings[i]);
      free(strs->strings);
    }
    
    int main(void)
    {
      struct strings_s strs = { 0 };
    
      add_strings(&strs, 2, "hello", "goodbye");
      add_strings(&strs, 4, "add", "these", "words", "also");
    
      show_strings(&strs);
    
      free_strings(&strs);
    
      return EXIT_SUCCESS;
    }
    itsme@dreams:~/C$ ./addstrings
    hello
    goodbye
    add
    these
    words
    also
    itsme@dreams:~/C$
    Last edited by itsme86; 09-11-2004 at 08:43 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM