Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Meet and Greet (http://www.go4expert.com/community/meet-and-greet/)
-   -   C programming error (http://www.go4expert.com/community/c-programming-error-t17734/)

CrazyGal 29May2009 07:21

C programming error
 
Hi there,

Could someone tell me why this wont work?

#include <stdio.h>
int main()
{
char p[10] = "Success";
char *t = p;
while (*t++ != '\0'){
printf("%c ", *t);
*t = 'i';
}
return 0;
}

If I move the increment part from while to the assignment statement, it works fine.
ie., *t++ = 'i'; It prints Success. The code above doesn't print anything.

Let me know if I'm missing something here.

Thanks a lot.
Tina

SaswatPadhi 29May2009 07:50

Re: C programming error
 
Seems like this is your first post.
So, welcome to the forum :clap::clap:

Now, about your code ... hmmm

First of all, using while(*t++ != '\0'){ is bad because it will NEVER print the first letter as t is incremented immediately.

Next, I don't think your code will print NOTHING. Which compiler do you use ? I think it will print "u c c e s s" and then some garbage. On my machine it shows :

u c c e s s " ╥ ‼ @ l ‼ @ ≡ ⌂ ░ * " K ↕ @ ☺ H ? > └
, > @ @ * " * * * * * " O └ , > ≡ ⌂ └
* " ↕ @ ☺ ≡ * " g p | x ┤ ≈ " ≡ ⌂ ╕ ╢ T ╚
* " ═ ╥ * * * * └ | p p | ↕ @ A



Now why does this happen ?

Let's analyze step-by-step :
Loop 1 : Step 1 : *t is checked for '\0' and returns false as *t = 'S'.
.............Step 2 : t is incremented and points to 2nd char = 'u'.
.............Step 3 : *t is printed with a space after it.
.............Step 3 : *t is changed to 'i'.

Loop 2 : Step 1 : *t is checked for '\0' and returns false as *t = 'i' (look at last step of Loop1).
.............Step 2 : t is incremented and points to 3nd char = 'c'.
.............Step 3 : *t is printed with a space after it.
.............Step 3 : *t is changed to 'i'.

Loop 3 : Step 1 : *t is checked for '\0' and returns false as *t = 'i' (look at last step of Loop1).
.............Step 2 : t is incremented and points to 4th char = 'c'.
.............Step 3 : *t is printed with a space after it.
.............Step 3 : *t is changed to 'i'.

Do you see what is going on ?
Whenever *t is checked for '\0', it contains 'i'. So, the program falls into an infinite loop and terminates when an exception occurs.

t should be incremented after changing *t to 'i'.
But you, increment t first and then change it to 'i'. So, it's useless !

Hope this will help.
Best of luck.

CrazyGal 29May2009 08:31

Re: C programming error
 
Hi,

Thanks for responding!
I see the error now.

I got confused with the strcpy version :
void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}

So, the point is when you've increment in while loop, then you shouldn't use that
operand in the function body.

Btw, I used gcc compiler and I dint see any print on the screen.

Thanks again,
Tina

SaswatPadhi 29May2009 09:23

Re: C programming error
 
My pleasure ! :biggrin:

[ I use gcc (MinGW) too ! ]

it career 29May2009 15:39

Re: C programming error
 
Put additional bracket
while(*(s++) = *(t++));
Now it should be clear.

SaswatPadhi 29May2009 16:59

Re: C programming error
 
Anyway, *s++ and *t++ are interpreted as *(s++) and *(t++) because of higher precedence of ++ than *.
See here : http://www.cppreference.com/wiki/operator_precedence

CrazyGal 29May2009 19:10

Re: C programming error
 
Ummm....Doesn't this fetch the character first and then increment? Like (*s)++. If it increments the pointer first, this version of strcpy wont work. Is that right?

Tina

SaswatPadhi 29May2009 20:11

Re: C programming error
 
I don't see any reason why that version should not work ! :surprised:
It works fine on my machine.
Adding brackets, as suggested by it_career is unnecessary as the compiler automatically interprets *s++ as *(s++).

Look, I think you don't get it properly.

Observe at the loop : while(*s++ = *t++);
What this does is :
(1) Assign *t to *s.
(2) Increase t and increase s.
(3) As nothing to do inside loop body, so go to step (1).

It does not increment the pointer first ! It's post-fix ++.

In the case you mentioned in post #1, first the while condition was executed, then t was increased and then the body was executed. So, the first char was missed out.
But here, nothing is done in the loop-body.

And one more thing, never do anything as crazy as (*s)++ because that will change your string by increasing each character by 1.

Hope I made it clear to you.

Best of luck.

CrazyGal 29May2009 22:00

Re: C programming error
 
Umm..My mistake again...That's what I meant but I got confused with the brackets. Its crystal clear now :)

Thanks!
Tina

SaswatPadhi 29May2009 22:04

Re: C programming error
 
My pleasure ! :biggrin:


All times are GMT +5.5. The time now is 15:56.