Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This is the code i saw on a site and i made some changes in it . The code is supposed to give all the permutation of string using recursion . There on the site ,they used and array to take a string but i used pointer string initialization but it does not work .
Here is the code
C++
        #include<stdio.h>
        #include<stdlib.h>
        
        void swap(char* x, char* y)
        {
            	char temp ;
        	temp=*x ;
        	*x=*y  ;
        	*y=temp ;        
        }
        
        void  permute(char *string,int start , int end )
        {        
        	int j ;
        	if(start==end)
        		printf("%s\n",string) ;
        
        	else
        		{
        			for(j=start;j<=end;j++)
        			{
        				swap((string+start),(string+j)) ;
        				permute(string,start+1,end) ;
        				swap((string+start),(string+j)) ;
        
        			}				
        			
        		}     
        }
        
        
        int main()
        {	
        	char* check;
        	*check ="ABC" ;
        	permute(check,0,2) ;
        	
        
	return 0 ;
}
Posted
Updated 9-Aug-13 7:39am
v5
Comments
pasztorpisti 9-Aug-13 14:02pm    
This shouldn't even compile... Learn a bit of C programming before you start using it: http://www.cprogramming.com/
Member 10200096 9-Aug-13 14:19pm    
You should see this before commenting
http://cplus.about.com/od/learningc/ss/strings.htm
pasztorpisti 9-Aug-13 15:43pm    
Do you know what this statement does: *check ="ABC"? In C++ this shouldn't compile at all, maybe in C it does but even in that case the result is not what you want. You should check out a pointer tutorial too besides the string stuff you linked.

Just say
C++
check ="ABC";

instead of "*check = "ABC". That will probably do the job.
 
Share this answer
 
"ABC" is a constant string - you can't change its value, or the value of any of the characters it contains.

You need to allocate sufficient space, and copy the string into that. That is probably the reason they used an array in the original version...
 
Share this answer
 
Comments
Member 10200096 9-Aug-13 14:21pm    
But C allows Char pointer string intialisation i.e we don't need malloc,right?
OriginalGriff 9-Aug-13 14:41pm    
You can say
char* cp = "Hello World";
But that points cp at a constant string, not a variable one - and you should have to use
const char* cp = "hello world";
in a modern compiler anyway, or it will complain to avoid the kind of mistake you are making.


You need to allocate memory in RAM, not just use a pointer to a constant string - there is a difference. This means you either need to use malloc directly, or allocate enough space on the stack via an array, and then copy your string content in.

Think about it: If you could do what your code tries, then what would happen here:
char* cp = "Hello world";
ChangeString(cp);
printf(cp);
...
printf("Hello world");
Because the compiler will spot that both constant strings are identical, it will only create space for one in the actual application. Oops! :laugh:
pasztorpisti 10-Aug-13 17:32pm    
There is something at the back of my mind that the readonly property of these constant string literals can be changed with some compilers and the "string pooling" option can be used to control the "merging" of identical string literals by the compilers. Maybe the simplest solution in this case is: char buf[100] = "ABC". This way we don't speak of string literal modification and the buffer is filled up all with the string somehow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900