T
tuchka
Hi, guys!
I am very new here and just started to learn C. I have previous java
exp. however.
I'm abs. stuck on pointers and i'm unable comprehend algorithm of
simple program that reverses chars in string no matter how long I'm
staring at it.
This is the method(I printed out some lines for better
understanding(by me)):
char *strrev(char *string) {
char *original = string;
char *forward = string;
char temp;
printf("\nInside strrev method:");
printf("\n*string %c", *string);
printf("\n*original %c", *original);
printf("\n*forward %c", *forward);
printf("\nstring %s", string);
printf("\noriginal %s", original);
printf("\nforward %s\n", forward);
printf("\nFirst while:");
while(*string){
printf("\n*string %c ", *string);
printf("\nstring %s ", string);
string++;
}
printf("\nSecond while:");
while(forward<string) {
printf("\n*string %c", *string);
printf("\n*original %c", *original);
printf("\n*forward %c", *forward);
printf("\nstring %s", string);
printf("\noriginal %s", original);
printf("\nforward %s\n\n", forward);
temp=*(--string);
*string=*forward;
*forward++=temp;
}
return (original);
}
Results:
//////////////
First while:
*string h
string hello
*string e
string ello
*string l
string llo
*string l
string lo
*string o
string o
Second while:
*string
*original h
*forward h
string
original hello
forward hello
*string h
*original o
*forward e
string h
original oellh
forward ellh
*string e
*original o
*forward l
string eh
original olleh
forward leh
Reverse hello is olleh
////////////////////////////
I CANNOT UNDERSTAND IT. PLEASE,PLEASE, KINDLY, IF YOU HAVE TIME, WHAT
IS GOING ON????
Why i need first loop?
Where 'string' dissapeared?
how come the original is being modified?
Maybe it is very abbreviated version?maybe there is more detailed
version?
i'm dumb.
I am very new here and just started to learn C. I have previous java
exp. however.
I'm abs. stuck on pointers and i'm unable comprehend algorithm of
simple program that reverses chars in string no matter how long I'm
staring at it.
This is the method(I printed out some lines for better
understanding(by me)):
char *strrev(char *string) {
char *original = string;
char *forward = string;
char temp;
printf("\nInside strrev method:");
printf("\n*string %c", *string);
printf("\n*original %c", *original);
printf("\n*forward %c", *forward);
printf("\nstring %s", string);
printf("\noriginal %s", original);
printf("\nforward %s\n", forward);
printf("\nFirst while:");
while(*string){
printf("\n*string %c ", *string);
printf("\nstring %s ", string);
string++;
}
printf("\nSecond while:");
while(forward<string) {
printf("\n*string %c", *string);
printf("\n*original %c", *original);
printf("\n*forward %c", *forward);
printf("\nstring %s", string);
printf("\noriginal %s", original);
printf("\nforward %s\n\n", forward);
temp=*(--string);
*string=*forward;
*forward++=temp;
}
return (original);
}
Results:
//////////////
First while:
*string h
string hello
*string e
string ello
*string l
string llo
*string l
string lo
*string o
string o
Second while:
*string
*original h
*forward h
string
original hello
forward hello
*string h
*original o
*forward e
string h
original oellh
forward ellh
*string e
*original o
*forward l
string eh
original olleh
forward leh
Reverse hello is olleh
////////////////////////////
I CANNOT UNDERSTAND IT. PLEASE,PLEASE, KINDLY, IF YOU HAVE TIME, WHAT
IS GOING ON????
Why i need first loop?
Where 'string' dissapeared?
how come the original is being modified?
Maybe it is very abbreviated version?maybe there is more detailed
version?
i'm dumb.