:: Re: [DNG] Studying C as told. (For …
Αρχική Σελίδα
Delete this message
Reply to this message
Συντάκτης: Edward Bartolo
Ημερομηνία:  
Προς: dng
Αντικείμενο: Re: [DNG] Studying C as told. (For help)
Hi,

Studying and Practicing Pointers: (exercise5-4)
------------------------------------
#include <stdio.h>
#include <ctype.h>

int strend(char* s, char* t) {
char* saved_t = t;

for (; *t; t++);
for (; *s; s++);

while (*--t == *--s);
return (t + 1 == saved_t);
}

int main() {
char a[] = "mouse, cat, dog\n";
char b[] = "cat, dog\n";
char c[] = "horse, cow\n";

  if (strend(a, b))
    printf("b found in a\n");
    else printf("b not found in a\n");


  if (strend(a, c))
    printf("c found in a\n");
    else printf("c not found in a\n");


return 0;
}
---------------------------------

On 02/07/2016, Edward Bartolo <edbarx@???> wrote:
> Hi,
>
> <<
> while(*t++ = *s++);
>>>
>
> Using the operator priority table, ++ have the highest priority and
> evaluation takes place left-to-right. This increments the pointers.
>
> Next, * is 'evaluated' right-to-left. This has the effect of
> dereferencing both pointers. Thirdly, the assignment operator '=' is
> executed which sets sets the referenced values to equal each other.
>
> The incremented pointers in the first step are not used for the
> assignment. The reason is ++ follows the pointers.
>
> Edward
>
> On 01/07/2016, Edward Bartolo <edbarx@???> wrote:
>> Hi,
>>
>> I mistakenly wrote:
>> <<
>> *t++ means first increment the pointer, then dereference it,
>> otherwise this wouldn't work.
>> */
>> while(*t++ = *s++);
>> }
>>>>
>>
>> *t++ means first dereference, then increment. Since we have an
>> assignment both incremented pointers are not used for the assignment
>> of the characters.
>>
>> Edward
>>
>> On 01/07/2016, Edward Bartolo <edbarx@???> wrote:
>>> 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?
>>>
>>> Edward
>>>
>>
>