Szerző: Edward Bartolo Dátum: Címzett: dng Tárgy: Re: [DNG] Studying C as told. (For help)
Hi,
I am now studying pointers my "weakest" part of the language. The
following is a program I wrote as an exercise to mimic what strcat
does in a very rudimentary way. Please, be aware this is only to serve
as an exercise and NOT to reinvent the standard functions.
#include <stdio.h>
#include <ctype.h>
/* assume target string has enough free space */
void strcat1(char* s, char *t) {
/* find terminating null */
/*
The for loop increments pointer t after every iteration.
Loop starts at t. The final value of t is the position of
/0 character.
*/
for (; *t; t++);
/*
The while loop uses C's bastardized version of an assignment
treating it also as a statement. At each iteration, first the
char at t is assigned the char at s. After this, both t and s
are incremented.
*t++ means first increment the pointer, then dereference it,
otherwise this wouldn't work.
*/
while(*t++ = *s++);
}
int main() {
/*
Here I am assuming both source and target are automatically
appended by a \0 character. Otherwise strcat1 would go into
an uncontrolled memory corrupting frenzy only to be stopped
by a segmentation fault.
However this didn't happen.
*/
char source[] = "Appended end of string\n";
char target[1024] = "Target string\n";
strcat1(source, target);
printf(target);
return 0;
}
I need a memory helper to remember how things like *ch++ is evaluate
and to read its meaning. As it is, it is definitely ambiguous as it
can be interpreted to mean
a) increment the pointer then dereference it
b) dereference the pointer then increment its data.
I am taking *ch++ as an expression. Some rules must exist that help
one correctly interpret the meaning of these expressions.
I know there is operator priority and rules that govern how
expressions are evaluated. Do I need to know these at the tips of my
fingers?