Problems with pointers encountered in chapter 6.12 of "C and pointers"

this section gives an example for pointers: look for the existence of a character in a data structure.
the following is what I wrote routinely:

-sharpinclude <stdio.h>

-sharpdefine TRUE 1
-sharpdefine FALSE 0

int find_str(char **, char);
int find_str_1(char **, char);

int main()
{
    char *strings[] = {
        "there is a dog.",
        "no,that"s a cat.",
        NULL
    };
    int ret;

    ret = find_str(strings, "m");   //strings
    //ret = find_str_1(strings, "m"); //strings
    printf("%d\n", ret);
    
    return 0;
}

int find_str(char **strings, char value)
{
    while (*strings != NULL) {
        while (**strings != "\0") {    //1
            if (*(*strings)PP == value) {
                return TRUE;
            }
        }
        stringsPP;    //"there is a dog."1strings:stringsfind_str_1()
    }
    return FALSE;
}

int find_str_1(char **strings, char value)
{
    char *line;

    while ((line = *strings) != NULL) {
        while(*line != "\0"){
            if (*linePP == value) {
                return TRUE;
            }
        }
        stringsPP;
    }

    return FALSE;
}

what puzzles me is that
find_str () changes the passed-in parameters, while find_str_1 (does not change the passed-in parameters.
and when I GDB debug find_str (), breakpoint 1 is set to detect "there is a dog." The strings value (which is an address) remains the same during the process of whether the target character"m"is included in the string. So in find_str (), which sentence causes the incoming pointer strings to be modified?

Mar.29,2021

expand if (* (* strings) PP = = value) in find_str () into two segments, as follows

1  int find_str(char **strings, char value)
2  {
3      while (*strings != NULL) {
4          while (**strings != '\0') {
5              int match = **strings == value;
6              (*strings)PP;
7              if (match) {
8                  return TRUE;
9              }
10         }
11         stringsPP;
12     }
13     return FALSE;
14 }

there are only two places where the variable value will be modified

    Line 11 of
  1. modifies the local variable strings , which does not affect the content of the original parameter.
  2. Line 6 of
  3. modifies the element value of the parameter strings , which affects the content of the original parameter.

you can understand the second modification in this way

strings[i] = strings[i] + 1;

and strings [I] is a string pointer, so each execution causes the string pointer to move backward,
until the target character is found, or the end of the string.

< hr >

as an example, calculate the value of each variable after the following code has been run

char *s0 = "123";
char *s1 = "abc";
char *s2 = NULL;
char *strings[] = {s0, s1, s2};

(*strings)PP;

the answer is that only the first element of strings has been changed from S0 to S0 + 1 .


should be self-increasing at if

Menu