Home Ask Login Register

Developers Planet

Your answer is one click away!

Gatonito February 2016

Simple code regarding code to read from a struct pointer

I'm trying to understand pointers so I did this code:

#include<iostream>

using namespace std;

struct teste{
  int a;
  bool b;
};

void say (struct teste* a) {
  cout << (*a).a << (*a).b << "\n";
}

int main() {
  teste* e;
  (*e).a=2;
  (*e).b=0;
  say(e);
}

which gives me Segmentation Fault

but the following:

#include<iostream>

using namespace std;

struct teste{
  int a;
  bool b;
};

void say (struct teste* a) {
  cout << (*a).a << (*a).b << "\n";
}

int main() {
  teste e;
  e.a=2;
  e.b=0;
  say(&e);
}

I know that the second one is prefered but why the first one will not work? I think I did everything all right.

Answers


CoryKramer February 2016

You never actually allocated any memory for e. You can either declare it on the stack, then pass it as a pointer by taking it's address using operator &

int main() {
  teste e;
  e.a=2;
  e.b=0;
  say(&e);
}

or declare it on the heap using new, then delete it when you're done.

int main() {
  teste* e = new teste;
  e->a=2;
  e->b=0;
  say(e);
  delete e;
}


Giorgi Moniava February 2016

teste* e; 
(*e).a=2;   
(*e).b=0;

This gives you segmentation fault because pointer e is not initialized - it doesn't point to valid memory.

When you initialize pointer with some address say y - and then apply dereference operator, you tell it to retrieve value from the memory address y which you assigned to it. In your case, no address has been assigned to it, so you can't dereference it.

Make it point to a teste object, then you can dereference it.


Hans Lepoeter February 2016

In the first example you declare a pointer, but that pointer points to nothing. So you get the seg. fault. You should assing a value to that pointer, being an address of an instance of teste, either declared or created in memory using new() or malloc()


kfsone February 2016

Imagine you ask me "where is the airport"? I offer to write the address of it on a sticky note for you.

On the note I write:

airport

This is not very helpful, is it?

teste* e;

this says "declare a variable, e, such that it holds the address of an instance of teste in memory".

But you haven't provided an actual instance of teste for it to point to; you didn't actually assign the address of something to it.

int main() {
    teste instance;
    teste* e = &instance;
    e->a = 2;
    (*e).b = 0;  // equivalent to e->a
    say(e);
}

The line

teste* e = &instance;

says "declare a variable, e, such that it holds the address in memory of a teste struct, and let that address be the address-of instance (&instance)".

We could also have written

teste* e;
e = &instance;

But it's better practice to try and always initialize your variables at the time of declaration, if you can.

The -> operator is more-or-less syntactic sugar for (*e). -- it accesses through (dereferences) the pointer.

e->a
// is equivalent to
(*e).a

Where . is the member-of -> is the member-through.

Note that . and -> are distinct operators, which will become important later in your understanding of the language.

In this example, instance is a local variable and so is created on the stack. This means that when the scope ends it will be destroyed/go away.

If you need instance to stick around longer, or it is very large, you can allocate it from the "heap".

int main() {
    teste* e = new teste;
    e->a = 2;
    e->b = 0;
    say(e);
    delete e; 

Post Status

Asked in February 2016
Viewed 3,120 times
Voted 11
Answered 4 times

Search




Leave an answer


Quote of the day: live life