I don't know what happens when I refer to char str[] with a pointer.

Asked 1 years ago, Updated 1 years ago, 309 views

There are two programs that I would like to solve for debugging programs.
The environment is visual studio 2019 Windows 10 64bit.

Question: Why do you only write "str==NULL" in str[] during debugging?
char str[]="str==NULL?":str"; I thought str[] contained "str==NULL?":str", but when I debugged it, I found out that it was not. I don't know why the string str[] is not "str==NULL?" (NULL) but "str==NULL". In the first loop, the contents are str==NULL? "(NULL)": str, in the second loop, str==NULL
In the third loop, str==NULL is somehow different from what I imagined.

For the other program, the string str[] is
Char*p=str[] should have entered the address of p, but when I debugged it, the string str[] was not "abcde" and
The contents of char str[]={"abcde"}; were abcde, but bbcde in the first loop, bccde in the second loop, and bcdde in the third bcdde
It will be different from the 4th bcdee.I don't know why char*p contains the string str[] but changes to the string str[].

First Code

#include<stdio.h>
# include <string.h>

int main(void){
    char str[] = "str==NULL?\"(NULL)\":str";
    char*p,*q;
    intch;
    p=str;
    for(;;){
        for(q=p;!(*q=='?'||*q==':'||*q==0);q++);
        ch=*q;
        *q = 0;
        printf("|%s|\n",p);
        if(ch==0)break;
        p = q+1;
    }
}

Second Code

#include<stdio.h>
int main(void){
    char str [ ] = "abcde";

    for (char*p=str;*p;++p)
    {
        ++(*p);
        printf("%s\n", &(*p));
    }
}

c

2022-09-30 21:55

2 Answers

Keywords
Memory. Pointer. See.Terminating character. NULL.

#include<stdio.h>
# include <string.h>

int main(void){
    /* Keep the following string and NULL at the end of the memory: */
    char str[] = "str==NULL?\"(NULL)\":str";

    /* Accessing the memory address secured by str*/
    /* Provide two pointers for use.*/
    char*p,*q;
    /* Determine if str has reached the end of the reserved memory address */
    /* Variables for the .This will help you decide to stop the infinite loop.
    intch;
    /* Set pointer p to the first address secured by str. */
    p=str;

    /* It's an infinite loop.It won't stop until it breaks.*/
    for(;;){
        /* Here's another loop */
        /* Look at the addresses secured by the str indicated by p in order */
        /* If the contents are not ? or : or termination NULL, the loop continues. */
        /* That is, the address that q points to goes one by one to ?:NULL for the contents that can be referenced by str. */
        for(q=p;!(*q=='?'||*q==':'||*q==0);q++);
        /* I have saved the contents of *q once in ch.Only 0 breaks later and exits the infinite loop.*/
        ch=*q;
        /* Replace *q with 0.This will rewrite the contents of str*/
        *q = 0;
        /* *p (starting with str) to NULL.*/
        /* In the latter half of the process, the address of p is in progress */
        printf("|%s|\n",p);
        /* The process of exiting an infinite loop.*/
        /* If this is not written correctly, the program will not stop in an infinite loop.*/
        if(ch==0)break;
        /* Rewrite the address of p that indicated the beginning of str to q+1 and proceed with the reference */
        /* This will also proceed with the memory address shown in the printf above */
        p = q+1;
    }
}


#include<stdio.h>
int main(void){
    /*str[ ] ensures that there are 5 consecutive characters of addresses in memory and the contents are abcde.*/
    char str [ ] = "abcde";

    /* Let's look at the first memory address saved in str one by one.
    /* The loop doesn't end until NULL comes out. */
    /* From the first character to the terminating NULL in this loop.Displays the second character to the terminating NULL in order */
    for (char*p=str;*p;p++)
    {
    /* The pointer*p provides direct manipulation of the contents of the address on the memory reserved by str[].*/
    /* The pointer allows you to directly manipulate the memory addresses secured by str[].*/
        /* Take the value of the memory address indicated by *p and add 1 to it */
        /* It is substituted for the address indicated by *p.The contents of the memory saved by str are rewritten.*/
        (*p) = (*p) + 1;
        /* Displays the address indicated by *p up to the end of NULL.*/
        printf("*p:%s\n", &(*p));
        /* Also displays the contents of str.*/
        printf("str:%s\n", str);
    }
}


2022-09-30 21:55

epsilon phoenix's Answer seemed to be not enough to understand, so as a supplement.

I tried to embed printf into the first code.

#include<stdio.h>
# include <string.h>

int main(void){
  char str[] = "str==NULL?\"(NULL)\":str";
  char*p,*q;
  intch;
  p=str;
  printf("before loop\n";
  printf("str=|%s|\n", str);
  printf("p=|%s|\n",p);
  for(;;){
    printf("loop start\n";
    printf("--before inner loop\n";
    printf("str=|%s|\n", str);
    printf("p=|%s|\n",p);
    for(q=p;!(*q=='?'||*q==':'||*q==0);q++)
      ;
    printf("--after inner loop\n";
    printf("str=|%s|\n", str);
    printf("p=|%s|\n",p);
    printf("q=|%s|\n",q);
    ch=*q;
    *q = 0;
    printf("--after*q=0\n";
    printf("str=|%s|\n", str);
    printf("p=|%s|\n",p);
    printf("q=|%s|\n",q);
    printf("|%s|\n",p);
    if(ch==0)
      break;
    p = q+1;
    printf("--afterp=q+1\n";
    printf("str=|%s|\n", str);
    printf("p=|%s|\n",p);
    printf("q=|%s|\n",q);
    printf("loop end\n\n";
  }
  printf("after loop\n";
  printf("str=|%s|\n", str);
  printf("p=|%s|\n",p);
  printf("q=|%s|\n",q);
}

The results are as follows:

before loop
  str = | str == NULL ? " (NULL)" : str |
  p=|str==NULL?"(NULL)":str|
loop start
--before inner loop
  str = | str == NULL ? " (NULL)" : str |
  p=|str==NULL?"(NULL)":str|
-- after inner loop
  str = | str == NULL ? " (NULL)" : str |
  p=|str==NULL?"(NULL)":str|
  q=|?"(NULL)"—str|
--after*q = 0
  str = | str == NULL |
  p=|str==NULL|
  q=||
| str == NULL |
-- afterp = q+1
  str = | str == NULL |
  p=|"(NULL)":str|
  q=||
loop end

loop start
--before inner loop
  str = | str == NULL |
  p=|"(NULL)":str|
-- after inner loop
  str = | str == NULL |
  p=|"(NULL)":str|
  q = | —str |
--after*q = 0
  str = | str == NULL |
  p="(NULL)"|
  q=||
| "(NULL)" |
-- afterp = q+1
  str = | str == NULL |
  p=|str|
  q=||
loop end

loop start
--before inner loop
  str = | str == NULL |
  p=|str|
-- after inner loop
  str = | str == NULL |
  p=|str|
  q=||
--after*q = 0
  str = | str == NULL |
  p=|str|
  q=||
| str|
after loop
  str = | str == NULL |
  p=|str|
  q=||

The following answers are not necessarily correct for simplicity, but I think they are generally correct.

Assume.

  • char array terminates at NULL (\0) where char str[]=" strr/" character above is "\nULL?
  • str contains the first character of this string, s.
  • p or q do not copy or receive strings str==NULL?\":str\0 from str.Think of it as a place to place the above string.

Before entering the loop for(;;), both str and p can still be replaced by str.
Both str and p point to the s position at the beginning of the string above, and there is no \0 in the middle of the string.

Look at the results above.
At the end of the first inner loop, q points to the first ? position where str==NULL?\"(NULL)\":str\0.
So when you try to retrieve the string, you get ? to \0?"(NULL)":str
.
On the other hand, neither str nor p have changed since before entering the loop, so you can retrieve the first string as it is now.

Then *q=0 causes the letter ? to be the letter \0.
*q is treated as a character in the q position. Accept the question of \0 when 0 was substituted.
Since *q=0 changed the string to str==NULL\0"(NULL)":str\0, both str and p are taken as strings from s to \0.
q has changed from ? to \0, so the string to be extracted is empty.

In p=q+1, p points to p (starting to take out the string) one step ahead of q.The first " half-width blank.
If you take out the string, it will be "(NULL)":str.

From now on, if you look at the results above, you will see how str, p, and q change.

If this answer is helpful, please try to follow the action of the second program by embedding printf in detail by yourself.


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.