Passing values ​​through files in C ++

I'm still pretty new to C ++, and I have been successful in making my programs not look like cluster confusion.

I finally got rid of various error messages, but now the application crashes and I don’t know where to start. The debugger just throws a random hex location.

Thanks in advance.

#include <iostream>

using namespace std;

struct Value{
public:
    int Val;
}*pc;

#include "header.h"

int main () {
    cout << "Enter a value: ";
    cin >> pc->Val;
    cout << "\nYour value is " << pc->Val << ". ";
    system ("pause");
    return 0;
}
+3
source share
5 answers

In your program, pc is not a structure - it is a pointer to a structure (due to *). You do not initialize it with anything - it points to some kind of fictitious location. So either initialize it in the first line of main ():

pc = new Value();

, * . → .

+8

pc - , . .

:

struct Value{
public:
    int Val;
} c;

    ...
    cin >> c.Val;
    cout << c.Val;

- . - new delete :

int main()
{
    pc = new Value;

    ...

    delete pc;
}
+3

'pc'. undefined.

+3

pc - pointer Value

int main(),

pc = new Value();

pc = NULL;. PS. ++ , , , :

delete pc; //It frees memory....

, .

0
source
#include <iostream>

using namespace std;

struct Value{
public:
    int Val;
}*pc;



int main () {


    pc = new Value();

    cout << "Enter a value: ";
    cin >> pc->Val;
    cout << "\nYour value is " << pc->Val << ". ";

    delete pc;

    system ("pause");
    return 0;
}
0
source

Source: https://habr.com/ru/post/1736580/


All Articles