I am new to C programming and was attempting to write a code in which values of variables x and y are taken from the user in the main function of File1.c. All other functions in the file use the value of these variables. I have used 'extern' keyword to declare it in Header file Header.h and this header file also makes use of the values of x and y entered by the user. Another File2.c also makes use of these variables. Both File1.c and File2.c include Header.h
I have defined variables x and y as global variables in File1.c but I am constantly getting segmentation faults. How should I proceed?
EDIT: Here is the code:
#include<stdio.h>
#include<Header.h>
int x,y;
int main()
{
uint16_t *Buffer_1 = (uint16_t *)malloc(sizeof(uint16_t) *x*y*256);
uint16_t *slice = (uint16_t *)malloc(sizeof(uint16_t)*x*y);
printf("Enter value of x: );
scanf("%d",&x);
printf("Enter value of y: );
scanf("%d",&y);
memcpy(slice,Buffer_1,x*y*sizeof(uint16_t));
}
I get segmentation fault at memcpy. I have declared variables x,y in Header.h as follows:
#include <stdio.h>
extern int x;
extern int y;