Pointer of a struct with vector - Segmentation Fault

PlayHardGoPro

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 ?

emlai

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Struct pointer array segmentation fault

From Dev

Segmentation fault and pointer to non existing struct is not null

From Dev

Segmentation fault after returning a pointer to a struct

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault passing struct pointer to function

From Dev

Segmentation Fault when deleting a pointer in vector

From Dev

Segmentation fault: 11 when returning stuct via function pointer in struct

From Dev

Segmentation fault while trying to print parts of a pointer struct

From Dev

C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

From Dev

segmentation fault (core dumped) pointer struct data type

From Dev

Segmentation fault when accessing a pointer's member function in a vector

From Dev

Struct Array Segmentation Fault

From Dev

C - Segmentation fault with struct

From Dev

Segmentation fault on a null struct?

From Dev

Malloc to struct; segmentation fault

From Dev

Struct causing segmentation fault

From Dev

Segmentation Fault, pointer issue?

From Dev

Segmentation fault on pointer

From Dev

Segmentation fault, pointer issue

From Dev

segmentation fault on vector

From Dev

Vector Iterator (Segmentation fault)

From Dev

Seg. Fault on exit caused by pointer to vector of pointers to struct

From Dev

Enter a string into a struct: segmentation fault

From Dev

segmentation fault MPI create struct

From Dev

Multidimensional array of struct - Segmentation fault

From Dev

Segmentation fault with malloc on char* in struct

From Dev

C struct array segmentation fault

From Dev

Segmentation Fault in using a node struct

Related Related

HotTag

Archive