I wrote this program and it compiled with no errors or warnings but when i went to run it i get a segmentation fault and i cant figure out what is causing it. Ive spent hours and hours on this . Anyone with some expertise willing to take a look?

#include <stdio.h>
#include "my.h"

int main(int argc, char* argv[])
{
int **a;
int rows, cols,j,k;
FILE* p;
int i;


fopen(argv[1], "r");
fscanf( p, "%d", &rows);
fscanf( p, "%d", &cols);
printf("works here");
a = (int**)calloc (rows, sizeof(int*));
for (i = 0; i <rows; i++)
{
        a[i] = (int*)calloc (cols, sizeof(int));

}
for (j=0;j<rows; j++)
{
        for(k = 0; k < cols; k++)
        {
                fscanf(p, "%d", &a[j][k]);
        }
}
printme(a,rows, cols);
oddvalues(a,rows, cols);
oddlocations(a, rows, cols);
countoddrows(a,rows,cols);
addrows(a,rows,cols);
findfirstsmall(a,rows,cols);
findlastlarge(a,rows,cols);
addcolumns(a,rows,cols);
averagecolumns(a,rows,cols);
free(a);
return 0 ;
}

HEADER:

#include <stdio.h>
#define my
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>

void printme(int** a, int rows, int cols);
void oddvalues(int** a, int rows, int cols);
void oddlocations(int** a, int rows, int cols);
void countoddrows(int** a, int rows, int cols);
void addrows(int** a, int rows, int cols);
void findfirstsmall(int** a, int rows, int cols);
void findlastlarge(int** a, int rows, int cols);
void addcolumns(int** a, int rows, int cols);
void averagecolumns(int** a, int rows, int cols);

PRINTME.c:

#include "my.h"
#include <stdio.h>

void printme(int** a, int rows, int cols)
{
int i;
int j;
printf("a. The contents of the array are the ints, printed row by row:..................");
for (i = 0; i<rows; i++)
{
        for(j=0; j<cols; j++)
        {
        printf("%3d", a[i][j]);
        }
        printf("\n");
}
return;
}

ODDVALUES.c
#include "my.h"
#include <stdio.h>

void oddvalues(int** a, int rows, int cols)
{
int i;
int j;
int even = 0;
printf("b. The zeroed elements and odds of the array are: ...................");

for(i = 0; i<rows; i++)
{
        for(j=0; j<cols; j++)
        {

        if(a[i][j]%2 !=0)
           {
            printf("%3d", a[i][j]);
           }
        else
           {
            printf("%3d", even);
           }
        }
}
return ;
}

ODDLOCATIONS:
#include "my.h"
#include <stdio.h>

void oddlocations(int** a, int rows, int cols)

{
printf("\n");
printf("c. The various rows of the array that contain at least one odd int are:.......................");
int i;
int j;

for(i = 0; i<rows; i++)
{
        for(j=0; j<cols; j++)
        {

        if(a[i][j]%2 !=0);
          {
              printf("%3d", a[i]);
          }
        }
}
return ;
}

COUNTODDROWS:
#include "my.h"
#include <stdio.h>

void countoddrows(int** a, int rows, int cols)
{


int i;
int j;
int count = 0;
printf("\n");
printf("d. The various number of odd numbers in each row is:..................");
for(i = 0; i<rows; i++)
{
        count = 0;
        for(j=0; j<cols; j++)
        {

        if(a[i][j]%2 !=0)
           {
            count++;
           }
        printf("%3d", count);
        }
}
return ;

}

ADDROWS:
#include "my.h"
#include <stdio.h>

void addrows(int** a , int rows, int cols)
{


int i;
int j;
printf("\n");
printf("e. The various row sums are:..................");

for(i = 0; i<rows; i++)
{
        int temp = 0;
        int sum = 0;
        for(j=0; j<cols; j++)
        {
            temp = a[i][j];
            sum = sum + temp;
        }
        printf("%3d", sum);
}
return ;

}

#include "my.h"
#include <stdio.h>

FINDFIRSTSMALL:

void findfirstsmall(int** a, int rows, int cols)
{


int i;
int j;
int small;
printf("\n");
printf("f. The various first indices of the smallest int for each and every row is:.............");

for(i = 0; i<rows; i++)
{
        small = INT_MAX;
        for(j=0; j<cols; j++)
        {
            if (a[i][j] < small)
            {
                small = j;
            }
        }
        printf("%3d", small);


}
return ;

}

FINDLASTLARGE:
#include "my.h"
#include <stdio.h>

void findlastlarge(int** a, int rows, int cols)
{


int i;
int j;
int large;
printf("\n");
printf("g. The various last indices  of the largest int for each and every row is...........");

for(i = 0; i<rows; i++)
{
        large = INT_MIN;
        for(j=0; j<cols; j++)
        {
            if (a[i][j] >= large)
            {
                large = j;
            }
        }
        printf("%3d", large);
}
return ;

}

ADDCOLUMNS:
#include "my.h"
#include <stdio.h>

void addcolumns(int** a, int rows, int cols)
{
int i;
int j;
int sum = 0;
int temp;
printf("\n");
printf("h. The various column sums are:");

for(i = 0; i<cols; i++)
{
        temp = 0;
        sum = 0;
        for(j=0; j<rows; j++)
        {
            temp = a[i][j];
            sum = sum + temp;
        }
        printf("%3d", sum);
}
return ;
}

AVERAGECOLUMNS:
#include "my.h"
#include <stdio.h>

void averagecolumns(int** a, int rows, int cols)
{
int i;
int j;
int sum = 0;
int temp;
double average;
printf("\n");
printf("i. The various column averages are:");

for(i = 0; i<cols; i++)
{
        temp = 0;
        average = 0;
        sum = 0;
        for(j=0; j<rows; j++)
        {
            temp = a[i][j];
            sum = sum + temp;
            average = sum / rows;
        }
        printf("%3.2lf", average);
}
return ;
}

It depends what is in the file you are trying to open.

Your custom function parameters need looking at ...

int ** a, a is a pointer to a pointer.
int ** a[], a is an array of pointers to pointers

later in the functions you call printf() with a[j] which is not an int **

if a = 0x00AA , a[j] will be a memory address, but any assignment to this memory address may be right into the middle of memory you don't want to change, or requesting stuff from it results in the error. When dealing with anything that could be a possibility of things such as the memory address here the outcome can be a possibility of many things too.

I suggest, you simplify the variables and work your way back up again with pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.