system
Given the following post, respond as if you're commenting on Reddit:
human
Strange pointer behavior in C [ Segmentation fault(core dumped) ]

I made a program that lets you choose a minimum and a maximum number as limits to generate a random number, which then is used to create a matrix of those rows and columns, then you do the same thing for the elements of the matrix and in the end it prints the sum of the elements.  
It works with small numbers, but the problem is that if I input numbers such as min:5 and max:10 the pointer to the matrix gets a value which then changes when it goes in the sum function (I printed its value three times to track it along the execution) giving a "Segmentation fault (core dumped)".  
Why does this happen, obviously the pointer tries to access a "cell" that it can't use, but why does it change value and how can I solve this?

Sorry for the long code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void generate_m(int dim_tot, int col, int *n);
    
    int sum(int dim_tot, int *n);
    
    int main()
    {
    	srand(time(NULL));
    
    	int min_r_m, max_r_m, min_c_m, max_c_m;
    	//rows
    	printf("Insert the minimum of rows: ");
    	scanf("%d", &min_r_m);
    	while (min_r_m <= 0)
    	{
    		printf("Insert a number greater than 0: ");
    		scanf("%d", &min_r_m);
    	}
    	printf("Insert the maximum of rows: ");
    	scanf("%d", &max_r_m);
    	while (max_r_m <= min_r_m)
    	{
    		printf("Insert a maximum number greater than the minimum: ");
    		scanf("%d", &max_r_m);
    	}
    	//columns
    	printf("Insert the minimum of columns: ");
    	scanf("%d", &min_c_m);
    	while (min_c_m <= 0)
    	{
    		printf("Insert a number greater than 0: ");
    		scanf("%d", &min_c_m);
    	}
    	printf("Insert the maximum of columns: ");
    	scanf("%d", &max_c_m);
    	while (max_c_m <= min_c_m)
    	{
    		printf("Insert a maximum number greater than the minimum: ");
    		scanf("%d", &max_c_m);
    	}
    
    	const int DIM1=(rand() % (max_r_m-min_r_m+1)) + min_r_m;
    	const int DIM2=(rand() % (max_c_m-min_c_m+1)) + min_c_m;
    	const int DIM_TOT = DIM1*DIM2;
    	printf("\nrows: %d columns: %d area: %d \n", DIM1, DIM2, DIM_TOT);
    	int m[DIM1][DIM2];
    	int tot;
    	int *p_m = &m[DIM1][DIM2];
    
    	generate_m(DIM_TOT, DIM2, p_m);
    
    	tot=sum(DIM_TOT, p_m);
    	printf("\nThe sum of the elements in the matrix is: %d", tot);
    
    	return 0;
    }
    
    void generate_m(int dim_tot, int col, int *n)
    {
    	int i;
    	int max_n, min_n;
        printf("\npointer: %d\n", n);
    	printf("Insert the minimum number: ");
    	scanf("%d", &min_n);
    	printf("Insert the maximum number: ");
    	scanf("%d", &max_n);
    	while (max_n <= min_n)
    	{
    		printf("Insert a maximum number greater than the minimum: ");
    		scanf("%d", &max_n);
    	}
    
    	for (i=0; i < dim_tot; i++)
    	{
    		*(n+i) = (rand() % (max_n-min_n+1)) + min_n;
    		printf("%d ", *(n+i));
    		if((i+1)%col == 0 && i != 0)
    			printf("\n");
    	}
    	printf("\npointer: %d\n", n);
    
    }
    
    int sum(int dim_tot, int *n)
    {
    	int i;
    	int tot=0;
    	printf("\npointer: %d\n", n);
    
    	for (i=0; i < dim_tot; i++)
    		tot += *(n+i);
        printf("\npointer: %d\n", n);
    
    	return tot;
    }

Thank you for your time.
gpt
int *p_m = &m[DIM1][DIM2];

You're getting a pointer to the last elments of the array here, rather than the starting elements. This causes your other functions to read/write out of bounds of the array, messing up all kinds of other stuff.

Instead, you probably want:

    int *p_m = &m[0][0];

...to get the address of the start of the array.