C programming error

Go4Expert Member
29May2009,07:21   #1
CrazyGal's Avatar
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
~ Б0ЯИ Τ0 С0δЭ ~
29May2009,07:50   #2
SaswatPadhi's Avatar
Seems like this is your first post.
So, welcome to the forum

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.
Go4Expert Member
29May2009,08:31   #3
CrazyGal's Avatar
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
~ Б0ЯИ Τ0 С0δЭ ~
29May2009,09:23   #4
SaswatPadhi's Avatar
My pleasure !

[ I use gcc (MinGW) too ! ]
Go4Expert Member
29May2009,15:39   #5
it career's Avatar
Put additional bracket
while(*(s++) = *(t++));
Now it should be clear.
~ Б0ЯИ Τ0 С0δЭ ~
29May2009,16:59   #6
SaswatPadhi's Avatar
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
Go4Expert Member
29May2009,19:10   #7
CrazyGal's Avatar
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
~ Б0ЯИ Τ0 С0δЭ ~
29May2009,20:11   #8
SaswatPadhi's Avatar
I don't see any reason why that version should not work ! :
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.
Go4Expert Member
29May2009,22:00   #9
CrazyGal's Avatar
Umm..My mistake again...That's what I meant but I got confused with the brackets. Its crystal clear now

Thanks!
Tina
~ Б0ЯИ Τ0 С0δЭ ~
29May2009,22:04   #10
SaswatPadhi's Avatar
My pleasure !