Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
When I call the following function, only the 2nd printf will print the expected output while the first printf only print a character (not necessarily related to the initial string). However, if I comment out statements relating to *s2, quite bizarrely the first printf gives me the required output.

For instance:
char a[]="HELLO WORLD";
char b[]="MY NAME IS JOHN";
calling afunction(a,b) will produce something like this:
n
my name is john

but commenting out fields relating to *s2 will give:
hello world

Why the hell is that? Thanks.

What I have tried:

int afunction(char *s1, char *s2){
	char *a,*b,*c,*d;
	c=a;
	d=b;
	while (*s1!='\0'){
		*a++=tolower(*s1++);
	}
	*a='\0';
	
	while (*s2!='\0'){
		*b++=tolower(*s2);
		s2++;
	}
	*b='\0';	
	printf("%s\n",c);
	printf("%s\n",d);
	return 0;
}
Posted
Updated 2-Mar-17 17:07pm
Comments
PIEBALDconsult 2-Mar-17 19:10pm    
Because you told it to.
I'm surprised you don't get a crash, which is what I get (and expect).
What are you actually trying to do?
Graeme_Grant 2-Mar-17 21:39pm    
Easiest way to remember ... pointers need to point at something. ;)
PIEBALDconsult 3-Mar-17 20:12pm    
And they always do...
Graeme_Grant 3-Mar-17 20:18pm    
;)

That is some funny lines of code .. you forget how baffling things are for beginners :-)

Okay big tip you really don't need a, b, c and d and you already have s1 and s2, that is six pointers. You have got lost in the shear complexity all of your own making by having so many pointers. Part of your learning to code is to distill the problem down to the bare minimum.

So your problem is the first 3 lines inside the function
char *a,*b,*c,*d;   // You define 4 pointers they don't point anywhere and are just junk so far
c=a;  // here you set c to have the same junk value as a
d=b;  // here you set d to have the same junk value as b

So what everyone else is telling you is you might want to point a and b somewhere ... probably s1 and s2 values and this gets back to the problem ... why so many pointers.

I am guessing you need only 1 extra pointer, which is the temporary one you move along. You set it to to s1 move it along doing your case conversion as you go. Then you set it to s2 move it along doing your case conversion on each character as you go. Then you display s1 and s2 and job done. I am not sure you want to be moving s1 and s2 that is the pointer to your actual text buffers and you loose them and you are dead in the water.
 
Share this answer
 
Comments
PIEBALDconsult 2-Mar-17 23:49pm    
Well, he hasn't even clarified whether he wants to do in-place lower-casing or not.
leon de boer 3-Mar-17 1:06am    
It basically has to be in place because he hasn't got access to anything else unless you allow pure stupidity and are going to set the pointer to a global. One would hope they still teach setting a char* pointer to a global is beyond dangerous it is actually stupid and reckless. You would be fired for doing that in my company as it's basic pointer safety 101, because putting an extra pointer on the interface for output is ever so hard.

So I guess if we follow your lead he is doing it all wrong he should have s1 and s2 as globals, heck who needs a local interface its just a waste of time and we end up writing something that looks like GW Basic :-)
PIEBALDconsult 3-Mar-17 8:58am    
Right, so if he _isn't_ trying to do in-place we need to know, and he hasn't responded.
leon de boer 3-Mar-17 12:24pm    
If he isn't doing in place he needs to change his interface so he doesn't commit a C crime against humanity.

int afunction(char *s1, char *s2, char* outbuf)
OR
int afunction(char *s1, char *s2, char* outs1, char* outs2)

Then he still doesn't need the number of pointers he has because the interface defines 4 and he still needs just 1 inside the function to move around as his temporary (I would accept two for a beginner). That is the whole point of the local interface it forces him to set the pointers or it won't compile and he wouldn't be here :-)

That is why this is pretty black and white, once students start playing around with local functions they need to know why that local interface exists. It's not there for good looks, it's there to protect you from doing dangerous things.
Member 13013165 3-Mar-17 14:08pm    
Just back from work. So, I was actually attempting the K&R exercise 5.15 and I want to write a function to fold the characters before strcmp them. Hence the clumsy code. Cheers. I'll have a shower first and be back.
You forgot to initialize *a and *b to a place to homd the lowercase strings.
The result depend on compiler and can be anywhere from correct results, partial results, to crash or segment fault.

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.
 
Share this answer
 

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