trying to implement this code in c

This is a discussion on trying to implement this code in c within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to implement this PHP code in C: Code: function showCombinations($string, $traits, $i) { if ($i >= ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    trying to implement this code in c

    Hi,

    I am trying to implement this PHP code in C:

    Code:
        function showCombinations($string, $traits, $i)
        {
            if ($i >= count($traits))
                echo trim($string) . "\n";
            else
            {
                foreach ($traits[$i] as $trait)
                    showCombinations("$string $trait", $traits, $i + 1);
            }
        }
    
        $traits = array
        (
            array('Happy', 'Sad', 'Angry', 'Hopeful'),
            array('Outgoing', 'Introverted'),
            array('Tall', 'Short', 'Medium'),
            array('Handsome', 'Plain', 'Ugly')
        );
    
        showCombinations('', $traits, 0);
    Having a real hard time of it. I'm a PHP programmer, and the way C handles strings is so much different. Could anyone offer some help?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It will be pretty different in C and I'm not going to write it all out for you. Why don't you post what you have so far so we can start from there?

    If you cannot even start, then maybe you need to ask a simpler question, such as "How do I create an array of strings in C?" or "How can I duplicate a foreach() style loop in C?"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Sure:

    Here is my code so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int printCombinations(char mystr[],int mystrIndex, char table[2][3], int tableLength, int currentIndex)
    {
      int i;
    
      if (currentIndex >= tableLength)
        {
          printf("%s",mystr);
          printf("\n");
        }
      else
        {
          for (i = 0; i < tableLength; i++)
    	{
    	  mystr[mystrIndex++] = table[currentIndex][i];
    	  currentIndex++;
    	  printCombinations(mystr,mystrIndex,table,tableLength,currentIndex);
    	}
        }
    }
    
    int main(void)
    {
      char mytable[2][3];
    
      mytable[0][0] = 'a';
      mytable[0][1] = 'b';
      mytable[0][2] = 'c';
      mytable[1][0] = '1';
      mytable[1][1] = '2';
      mytable[1][2] = '3';
      
      char mystr[1000];
      int mystrIndex=0;
    
      printCombinations(mystr,mystrIndex,mytable,3,0);
      return 0;
    }

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    In C, strings are simply arrays of chars, and can be passed around as pointers to the first char in that array. You could make your 2d array of type (char *) instead of (char) and initialize each one to the strings you had in your PHP. All the string functions in C expect a pointer like this, and will work properly when passed one of these pointers.

    Other than that - do you have any specific questions or specific topics you're lost on? It seems like you know enough C to do most of that.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Part of what sean is I think getting at is that this:
    Code:
    char mytable[2][3];
    
      mytable[0][0] = 'a';
      mytable[0][1] = 'b';
      mytable[0][2] = 'c';
      mytable[1][0] = '1';
      mytable[1][1] = '2';
      mytable[1][2] = '3';
    Is almost equivalent to this:
    Code:
    char mytable[2][4] = { "abc", "123" };
    Except I added a char so that these can be strings, since expressed this way they will get null terminated. You don't actually use any string functions on them, tho, and if that is intentional:
    Code:
    char mytable[2][3] = { {'a','b','c'}, {'1','2','3'} };
    is fine.

    I'm a little confused about why you don't just use the string model from your PHP tho:
    Code:
    char *traits[] = {
    	"Happy", "Sad", "Angry", "Hopeful",
    	"Outgoing", "Introverted",
    	"Tall", "Short", "Medium",
    	"Handsome", "Plain", "Ugly"
    };
    These are literals and cannot be changed but will work for this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rags_to_riches View Post
    Thanks for that. I don't actually know PHP and suddenly realized I failed to take the array of arrays into account:
    Code:
    	char traits[][4][20] = {
    		{"Happy", "Sad", "Angry", "Hopeful"},
    		{"Outgoing", "Introverted"},
    		{"Tall", "Short", "Medium"},
    		{"Handsome", "Plain", "Ugly"}
    	};
    Now it's not literals anymore.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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