C++ Char Questions

Asked 2 years ago, Updated 2 years ago, 59 views

#include <iostream>

using namespace std;

int main(void)
{
    char ID[10];

    cin >> ID;
    cout << ID << endl;

    return 0;
}

I wrote it as above, but I set the size of the character array called ID to 10, but I set a string with more than 10 in sin I typed it, but it's going in.

What exactly does that [*] mean?

c++ char

2022-09-20 21:48

1 Answers

The programmer should pay attention not to buffer over the number of arrays.

If you look at the example below.

#include <iostream>

using namespace std;

int main(void)
{
    char a = 'a';
    char ID[10];
    char b = 'b';

    sprintf(ID, "123456789AB");
    cout << ID << endl;
    cout << a << endl;
    cout << b << endl;

    return 0;
}

As it is written beyond the number of IDs, the variables around that memory are affected.


2022-09-20 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.