I have a question when I see 's answer that I don't know how to behave when I refer to charstr[] with a pointer.
Why shouldn't I write printf("str:%s\n",p);
?
*p
calls the character code for the address in memory.So I thought p
would be fine, but the debug will not work even if there is no error.If I were to use *p
, how would I change the printf
operator?
Also, why is &(*p)
good?Is it because the operator can only receive the character code from the address?In printf("*p:%s\n", &(*p))
, I tried printf("*p:%s\n", &p)
, but it didn't work.Why is &(*p)
of printf("*p:%s\n", &(*p))
OK?Or is it possible to change the operator %s
of printf("*p:%s\n", &p)
with &p
?
#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);
}
}
Why shouldn't I write printf("str:%s\n",p);
What made you think so?I think there is no problem with the code.
printf("*p:%s\n", &(*p))
is good and printf("str:%s\n",p);
is not bad.
If anything, printf("*p:%s\n", &(*p))
is more problematic.
&(*p))
may be intended to indicate that it is an address, but it is not usually written in this way.
In char*p;
, &(*p)
and p
are the same.By the way, &(*(&(*p)))
is the same as p
.
&Area means the address of the area, and *Address is the content of the area that the address points to.
If p contains a char-type area address, contains zero or more character codes in that area, and is zero-terminated, printf("str:%s\n", &(*p))
works fine as well as printf("str:%s\n",p);
.
I don't remember saying no, but if you want to take it and solve it yourself, check it out yourself until you realize there's no problem.
Is there an environment where I can check it?
The printf is also detailed in Wikipedia.
If you feel like it, you can check the definition files of various operating systems.
I think you can check the definition of the header.
© 2024 OneMinuteCode. All rights reserved.