-1

Why can't I initialize the integer variable num with the value of the number field of the Strct structure?

#include <iostream>

struct Strct
{
    float number = 16.0f;
};

int main()
{
    Strct* strct;
    int num = strct->number;
    return 0;
}

Error List: C4700 uninitialized local variable 'strct' used

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 9
    `Strct* strct;` -- `strct` needs to point somewhere valid, and right now it points to ...? Also, why are you using pointers for such simple code? Just `Strct strct;` is all you need, and change `->` to `.`. – PaulMcKenzie Dec 07 '21 at 17:32
  • I thought my strct points to the Strct structure, that is, to its type – Islam Aliev Dec 07 '21 at 17:34
  • 3
    No. Pointers must point to valid entities, objects, live instances, not types. If that were the case, then code like this: `char *c; *c = 'x';` would be ok, when it isn't. – PaulMcKenzie Dec 07 '21 at 17:35
  • You probably want to not use a pointer and replace `Strct* strct;` with `Strct strct;` and `int num = strct->number;` with `int num = strct.number;` – drescherjm Dec 07 '21 at 17:38
  • then can I define a member function that returns the address of this structure? – Islam Aliev Dec 07 '21 at 17:39
  • Currently there is no structure object to take an address of. – drescherjm Dec 07 '21 at 17:39
  • 1
    A *pointer* is just a value, like an `int`. It's value is an *address*. A *pointer* can point to its type (in this case, a `Strct`). Or it can have the value `nullptr`. Or it can be uninitialized — which is the case here (and has the same **undefined behavior** problem that uninitialized things have in C++). Or it can be a dangling pointer. Or can be a wild pointer. – Eljay Dec 07 '21 at 17:39
  • 1
    Remove the `*`, use `.` instead of `->`, problem solved. `Strct strct; int num = strct.number;` – Andrej Podzimek Dec 07 '21 at 17:40
  • Sounds like you need more info: [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242) – 001 Dec 07 '21 at 17:40
  • that is, the type of this object provides only data processing, but does not indicate it? – Islam Aliev Dec 07 '21 at 17:42
  • Huh? No. A pointer is a small integral data type, something like an `uint64_t` in most cases (except member pointers, which may be 12 B long or so). A pointer represents a memory address. If you don’t initialize an address, what does it point at? At a random location that happened to be left on the stack where the pointer is stored when the stack frame got extended there. If you insist on using a pointer, initialize it: `Strct strct; Strct *strct_ptr{&strct}; int num = strct_ptr->number;`. Or you could point at a heap-allocated object from `new` — avoid it unless you *really* need it. – Andrej Podzimek Dec 07 '21 at 17:56

1 Answers1

3

Why can't I initialize the integer variable num with the value of the number field of the Strct structure?

Because the pointer is uninitialised, and thus it doesn't point to any object. Indirecting through the pointer, or even reading the value of the pointer result in undefined behaviour.

I thought my strct points to the Strct structure, that is, to its type

No. Pointers don't point to types. Object pointers point to objects. Types are not objects in C++.

16.0f is not the value of number. 16.0f is the default member initialiser of that member. If you create an object of type Strct, and you don't provide an initialiser for that member, then the default member initialiser will be used to initialise the member of the object in question.

then can I define a member function that returns the address of this structure?

A structure is a type. A type isn't stored in an address. There is no such thing as "address of a type".


Here is an example of how to create a variable that names an instance of the class Strct:

Strct strct;

You can access the member of this variable using the member access operator:

int num = strct.number;

Here is an example of how to create an instance that doesn't use the default member initialiser:

Strct strct = {
    .number = 4.2f,
};
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    books that had fallen from the shelf began to return to their places. Thank you for the explanation, although elementary, but unfortunately my brain is so arranged – Islam Aliev Dec 07 '21 at 17:51