"->" Program Not Working

I'm having a difficult time understanding the "->" in C++. I can't understand why the below program, which I created, keeps crashing. Shouldn't my program point to structure abc.def, returning a value of 5? I thought abc->def is the same as (*abc).def, which means it should point to the above structure.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstddef>
#include <iostream>
using namespace std;
struct abc
{
int def=5;	
};
int main()
{
	abc *abc;
	cout<<abc->def;
}
Shouldn't my program point to structure abc.def, returning a value of 5?

The problem is that you have a pointer (*abc) that you never allocated memory for.

abc *a is a pointer to the abc type, but it does not actually point to an abc object yet. It starts with an arbitrary value. That's why you can't just go ahead and dereference it. You need to allocate an object (or array of objects). You do that with new.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

struct Person {
    int age = 5;
};

int main() {
	Person *p = new Person;
	cout << p->age << '\n';
	delete p;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <cstddef>
#include <iostream>

using namespace std;

struct abc
{
    static int def ;	
};

int abc::def = 5 ;

int main()
{
	abc *abc;

	cout<< abc->def ;
}



5 
Exit code: 0 (normal program termination)
Last edited on
@ratfus:
* Reusing typename as object-name is confusing. Note: names are case-sensitive.

@ar2007:
* That ain't nice. The static member exists even without any object instances. Your uninitialized pointer is unnecesary and misleading. There is a better syntax for that:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

struct ABC
{
  static int def ;	
};

int abc::def = 5 ;

int main()
{
  std::cout << ABC::def ;
}


A pointer can point to local object too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

struct ABC
{
  int def {7};	
};

int main()
{
  constexpr size_t N {3};
  ABC foo[N];
  for ( ABC* p=foo; p<foo+N; ++p ) {
    std::cout << p->def << '\n';
  }
}

That ain't nice. The static member exists even without any object instances. Your uninitialized pointer is unnecesary and misleading. There is a better syntax for that:


thank's for the suggestion
Hey guys, thanks for the help. This is just something that's really confusing for me; i'm probably just dumb.

abc *a is a pointer to the abc type, but it does not actually point to an abc object yet. It starts with an arbitrary value. That's why you can't just go ahead and dereference it. You need to allocate an object (or array of objects). You do that with new.


Doesn't it point to def in my structure with the "->" function or must I initialize all pointers right after the "int *variable" line? I always thought that setting the pointer eqaul to a variable, even after the "int *variable" initialized it? At least my book says it doesn't get more difficcult than this-hopefully they're right.

For example, I can write...
[code]
#include <iostream.h>
using namespace std;
int main()
{
int *pointer, variable; \\I didn't need to put *pointer=new variable or anything else;
pointer=&variable; \\Now I initialize it
variable=3;
cout<<*pointer; \\it returns 3 without a hiccup
return 1;
}

Last edited on
I don't see your point. You initialized pointer. That's why it works. You don't have to initialize it at the point of its definition, although it's considered good style to do that if possible. In your previous example you never initialized the pointer. That's why it crashed. Simply saying p->def doesn't magically make p actually point to an abc object. It needs to already be pointing to one before you say p->def.
Basically, I was wondering if there was a way to point to a data element in a structure without creating a new structure for it? Lets say I want to point to an element in the structure, without actually creating a new one. Each time main() currently runs, i'm creating a new structure-at least I think, by using "new."

Haven't figured out what foo means either.
A structure is not an object. It's a type. It is just a description of an object. It doesn't really exist until you instantiate a variable of that type.

Simply saying
1
2
3
4
struct Point {
    int x;
    int y;
};

doesn't create a Point. It creates absolutely nothing. It is just a description of something that you can instantiate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
    Point a;       // Point object allocated on the stack
    a.x = 1;       // can be used as is.
    a.y = 2;

    Point *p;      // only allocates a pointer, not a Point struct.
    p = new Point; // allocate a Point object on the heap
    p->x = 1;      // now you can use it
    p->y = 2;

    delete p;      // remember to delete dynamic objects

    return 0;
}

Topic archived. No new replies allowed.