0

Why can't I assign a value to qtd_lines ?

typedef struct{
  int x;
  int y;
  char z;
}struct_c;

typedef struct{
   int qtd_lines;
   struct_c *vector;
}struct_a;

int main(){
 struct_a *pointer;

//This gives me Segmentation Fault.
 pointer->qtd_lines = 10;
 pointer->vetor = (struct_c *)  malloc(pointer->qtd_lines * sizeof(struct_contas));

 return 0;
}

Ok, I have a vector in my struct_c, but the field qtd_lines is not a vector, right ? So Why am I getting this error ?

5
  • 2
    pointer is uninitialize. Commented Mar 18, 2015 at 3:51
  • 2
    Come on, this is really basic. Pointers have to point somewhere before you can use them. Commented Mar 18, 2015 at 3:53
  • @MattMcNabb My vector points to another struct, every vector is a pointer. OK. But do I have to point somewhere to use the qtd_lines ? It's just an integer. Commented Mar 18, 2015 at 3:54
  • 1
    pointer is not pointing anywhere. There is no integer. There is no pointer->qtd_lines, until you make pointer point to some allocated memory. This is nothing to do with vector. Commented Mar 18, 2015 at 4:00
  • 2
    In case you are unaware, you do not need to use dynamic allocation; you can write struct_a foo; foo.qtd_lines = 10; foo.vector = malloc.... Commented Mar 18, 2015 at 4:02

3 Answers 3

2

Pointers store memory addresses only.

struct_a *pointer;

This just declares a variable that holds some memory address. At that memory address there might be a struct_a object, or there might not be.

Then how do you know whether pointer points to an actual object or not? You have to assign a memory address to the pointer.


You can either dynamically allocate a block of memory to hold the object, and assign the address of that memory to pointer:

pointer = malloc(sizeof(struct_a));

Or you can allocate the object on the stack, and then store the memory address of that object in the pointer:

struct_a foo;
pointer = &foo;

But as it stands, in your code pointer doesn't point to anything, but you use the -> operator to access the memory that the pointer points to.

And in C you actually can do that, but you get undefined behavior. Like a segmentation fault. Or possibly just weird behavior. Anything is possible.

So, just make the pointer point to something before using it.

Sign up to request clarification or add additional context in comments.

Comments

1

pointer->qtd_lines = 10; is writing at unallocated location. You need to initialize pointer.

 struct_a *pointer = malloc(struct_a);

Comments

0

In your case only structure pointer is declared. No memory is allocated for that.

Assign this like so:

struct_a* pointer = (struct_a*)malloc(sizeof(struct_a));

Also once you done your things please don't forget to free that memory since it was taken from the Heap memory segment.

free(pointer);

Hope it helps.

1 Comment

even if using your phone to code, please remember indenting your code correctly to mark it as such, and also not to let EVERY line start with an uppercase letter, the function is called free, not Free ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.