function overlaying?

This is a discussion on function overlaying? within the C Programming forums, part of the General Programming Boards category; i ran into trouble with this code. Code: #include<string.h> #include<stdio.h> #include<stdlib.h> void swap(char *, int, int); void permut(char *string, int ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    function overlaying?

    i ran into trouble with this code.
    Code:
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    void swap(char *, int, int);
    void permut(char *string, int k); //k = number of chars
    
    int main ()
    {
        char a[] = "abc";
        
        permut(a, 0);
        
        return 0;
    }
    
    void permut(char *string, int k)
    {
        int i;
        if (k == strlen(string))
    	printf("%s\n", string);
        
        else
        {
    	for (i=k;i<strlen(string);i++)
    	{
    	    swap(string, k, i);
    	    permut(string, k+1);
    	}
        }
    }
    
    void swap(char *string, int k, int i)
    {
        char hold;
        hold = string[k];
        string[k] = string[i];
        string[i] = hold;
    }
    thats maybe because it isnt mine alone.
    the program works fine but i dont really understand the codeflow.

    the for statement in the permut function doesnt make much sense to me because the first time it is called, i is set to zero.(value of k right?). the swap function gets called by permut. but isnt the only thing swap is doing setting string[0] = string[0] and so forth....?
    before i forget about it, whats about that permut function?
    it calls itself a few times and terminates with printing the string.
    why is the string printed more than once, when actually it terminates itself after having printed the string once?
    is the function permut overlaying itself?

    help would be appreciated. thanks

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks that helped me out understanding this program a little.
    im sure there are different possibilities to write such a program right? if yes i will code my own different one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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