1

I am trying to print the value of an array using a pointer to that array's struct but I always get a segmenation fault.

thanks.

#include <stdio.h>

typedef struct{
    int x;
    int y;
}POINTS;


int main() {

    POINTS *pointsmain;

    pointsmain->x = 10;
    printf("%d",pointsmain->x);
    return 0;
}

1 Answer 1

1

You get the Segmentation Fault because your pointsmain pointer points to nothing. You just declare a variable of type pointer to POINTS. You must allocate memory on the heap for that. This is done by

    POINTS *pointsmain = (POINTS*)malloc(sizeof(POINTS)); 
Sign up to request clarification or add additional context in comments.

Comments

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.