George Waat
George Waat

Reputation: 177

Pointers to structs and printing

I've been learning structs and I've come to the pointers to structs, where I'm currently struggling with this.

I've got this piece of code:

struct point {
    int x;
    int y;
} *ptr;

ptr->x = 8;
ptr->y = 8;

Running this gives a segmentation error. What I want to do is assign the value of 8 to x /y, to which, as far as I understand it, ptr is pointing to.

Upvotes: 2

Views: 577

Answers (5)

M. Zheng
M. Zheng

Reputation: 175

You need to assign a valid memory location to ptr, which is a pointer to struct point. You could either use malloc, or declare a variable with struct point and assign its address to ptr (like the above answers).

Btw, if ptr has file scope, then it is initialized with zero, i.e., the NULL pointer; otherwise, evaluating ptr is undefined behavior (you could get any value). In both cases, trying to dereference ptr is undefined behavior and thus you could get the segfault (in some implementation you might modified some memory location unintendedly).

Upvotes: 1

Anish Sharma
Anish Sharma

Reputation: 505

Let me explain you the simple way.

1.

Variables hold the data(values to want them to hold) and pointers hold only the memory address of the variables(which are just a section of memory). Pointers are used to just hold the address of the variables so they cannot hold any user data. We can although create a pointer to a variable and manipulate that variable using the specific pointer. We first need to create the variable, then we create a pointer which references that variable, then we do anything with the pointer and the variable will be manipulated.

struct point 
{
    int x;
    int y;
};
struct point var1;      //declare a variable
struct point *ptr=&var1;// declare a pointer to that variable
ptr->x = 8;             // manipulate the variable
ptr->y = 8;

Or

2.

If you insist on using pointers only, then you need to allocate memory dynamically and then assign the base address of the allocated memory to the pointer.

 struct point 
{
    int x;
    int y;
} *ptr;

ptr=(struct point*)malloc(sizeof(struct point)); // this will allocate memory required to hold your structure              
ptr->x = 8;      //manipulate the memory content pointed by your pointer.       
ptr->y = 8;

Upvotes: 1

fluter
fluter

Reputation: 13786

The problem is that the pointer ptr does not point to any valid memory buffer, assign it to a allocated or automatic storage instead:

// way 1
struct point p;
ptr = &p;
ptr->x = 8;
ptr->y = 8;

// way 2
ptr = malloc(sizeof *ptr);
ptr->x = 8;
ptr->y = 8;
// when you are done remember to release the allocated memory
free(ptr);

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249133

You have created a pointer called ptr but not initialized it. When you dereference it with ptr-> (or *ptr), you invoke undefined behavior which in your case is crashing the program (but could do anything).

This would be better:

struct point {
    int x;
    int y;
};
struct point sp = {0,0};
struct point *ptr = &sp;

sp.x = 8;
ptr->y = 8;

Upvotes: 3

DevSolar
DevSolar

Reputation: 70263

Your ptr is a pointer to struct point.

However, there is no struct point it is pointing to.

struct point {
    int x;
    int y;
} *ptr;

struct point pt; // add this...

ptr = &pt;       // ...and this.

ptr->x = 8;
ptr->y = 8;

Upvotes: 5

Related Questions