#include <stdio.h>
int main()
{
char str[] = "hello";
scanf("%c", str[0]);
return;
}
This code is failing. "hello"
is literal and cannot be accessed, but I want to change the information of str
by saving it separately in an array called str
, but I don't know why because of an error..
#include <stdio.h>
int main()
{
char str[] = "hello";
scanf("%s", str);
return;
}
So when I received it with %s
, it worked well,
#include <stdio.h>
int main()
{
char str[] = "hello";
str[0] = 'c';
return;
}
This code also works well. What's the reason...?
scanf literals
①
----
scanf("%c", str[0]);
------
②
For scanf to store inputs, you must know where to store them, i.e. the memory address. So I get a memory address with a parameter.
You specified that you would like to receive text from 에서. This means that the data type in 의 should be char *
, which is the memory address where characters can be entered.
However, the parameter you entered in 에 is str[0]
which is the first value of the array str
and is not a memory address but a value char
.
So in this case, scanf will think of str[0]
or 'h'
as the memory address and try to put a value in it.
Because I tried to access the wrong place called 104
, if I am debugging, the debugger stops me by saying "You have an access error" and in release mode, the operating system kills the app and throws it away .
If you want to correct it correctly, you can give me an address, not a value
scanf("%c", &( str[0] ) );
char str[] = "hello";
Literal "hello"
is located in the data area of memory. It can be read, but it cannot be modified.
str
is in the correctable area of memory, so it is free to read and write. The location on memory with (str
) is called the call stack call stack.)
The above code is the code that reads the uncorrectable "hello"
and initializes it to the correctable str
.
Therefore, all of the codes below do not change literal "hello"
but change copy str
.
char str[] = "hello";
scanf("%s", str); // ③
str[0] = 'c'; // ④
To save scanf to the %s
shape of 의, you need the memory address char *
where the string starts. The value of the array name is the address char *
of the starting point of the array. It went in well.
는 str[0]
is a reference to the first position of str
which is a modifiable array (see char
. There's no problem putting the price in there.
To see for yourself that literals can't be changed, you can bring the literal's address and try to change it.
To do that, you can read the string literal with a pointer.
// E0144 Cannot initialize an entity in the form "char*" using a value in the form "const char*".
char* str = "hello";
str[0] = 'c';
Of course, they don't do that. The string literal is a constant, so the data type is const char *
rather than char *
. Let's force him to change it.
// Exception thrown: write access violation.
// str was 0x7FF6B1D39BB0.
Force type conversion to char* str = (char*) "hello"; // (char*).
str[0] = 'c';
This will prevent the debugger from doing so. It's a place that can't be modified.
571 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
572 Who developed the "avformat-59.dll" that comes with FFmpeg?
910 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
609 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.