Click here to Skip to main content
14,691,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In coding the reverse function in K&R, I tried to assign a de-referenced pointer (s1) to another de-referenced pointer (s2). I have checked (with a lot of printf and putchar) that *s1 indeed hold the character I want but in the array that s2 points to the character changes. Why does this happen? Thanks!

What I have tried:

void reverse(char *s1){
	char a[100];
	char *s2;
	int i=0;
	s2=&a[0];
	while(*s1!='\0'){
		s1++;
		i++;		
	}
	s1--;
	i--;
	while(i>=0){
		*s2=*s1;
	        s2++;
		s1--;
		i--;
	}		
}
Posted
Updated 25-Feb-17 3:27am

If you try it:
void reverse(char *s1){
	char a[100];
	char *s2;
	int i=0;
	s2=&a[0];
	while(*s1!='\0'){
		s1++;
		i++;		
	}
	s1--;
	i--;
	while(i>=0){
		*s2=*s1;
	        s2++;
		s1--;
		i--;
	}
	printf("%s\n", a);
}



int main()
{
    char s[] = "Hello World!";
    printf("%s\n", s);
    reverse(s);
    return 0;
}
Then you get the result:
Hello World!                                                                                                                   
!dlroW olleH

But ... that's probably more by luck than by judgement. The problem is that you aren't copying the string termination character '\0' from your input to your output, as is easy to prove, by "pre filling" the array with identifiable rubbish:
char *s2;
	int i=0;
	for (i = 0; i < 100; i++)a[i] = 'X';
	i = 0;

Now when you print you get:
Hello World!                                                                                                                   
!dlroW olleHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX����l7n��

Because there is nothing in the output string to tell printf when to stop!
Either set a null at the end of the reversed string, or prefill "a" to all nulls.

I'd do this differently though, with just a single loop:
int *inp = s1;
int *outp = &(a[99]);
do
   {
   copy from inp to outp
   if (inp was a null or you run out of space) exit the loop
   inc inp, dec outp
   } while (true);
You then print the reversed string directly from outp

Be aware that this function is useless unless you return the reversed string - but you can;t return a or any pointer to a part of it - it's on the stack and will cause major problems if you do. You would need to use malloc to allocate the space, not declare the array on the stack - and then arrange to free it. It would be a better idea to pass both the string and the output area to the function.
   
To help you to understand the program, use the debugger, it will show you exactly what it is doing. You may have to switch to machine code level to see how C code is compiled.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   
v2

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