0

I am running this code on Xcode version 11.2 . I have a matrix with 5 rows and 3 colomns and I want to calculate the average value of each row. Now here I tried to code it, but my matrix seems to be always null and I get this error Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) at s= s +aloc[i*5+j];. I don't know what I should modify at my code so it works. Can you help me?

#include  <stdio.h>
#include  <stdlib.h>

void  reads(float  *aloc);
void amount(float  *aloc);
void done(float *aloc);

int  main()
{

        float  *aloc  =  NULL;


    reads(aloc);
    amount(aloc);
    done(aloc);

        return  0;
}

void  reads(float  *aloc)
{
               if  ((aloc  =  (float*)malloc(5  *  sizeof(float))))
        {

                for  (int  i  =  0;  i  <  5;  i++)
                {
                     printf("\n  Enter  the values for the row no. %d  :  ",1+i);
                    for(int j=0;j<3;j++)
                        scanf("%f",  aloc  +  i*5+j);
                }
        }
}

void amount(float  *aloc)
{
    float s;
    int j,i;

           for(i=0;i<5;i++)
           { s=0;
         for(j=0;j<3;j++)

            s= s +aloc[i*5+j];

            printf("\n The average of the row no. %d is : %.2f ",i+1,s/3.);

           }
}

void done(float *aloc)
{
     free(aloc);
}

Lavi
  • 1
  • 1
  • 1
    `aloc + i*5+j`. You haven't allocated memory for the rows. – kaylum Dec 29 '19 at 20:59
  • Does this answer your question? [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – kaylum Dec 29 '19 at 21:01
  • What's the point of passing NULL in aloc to read(aloc)? This doesn't do what you think it does. The aloc in main() is a different one than the aloc argument in the called functions. – Jens Dec 29 '19 at 21:17
  • OT: for ease of readability and understanding: Please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Dec 29 '19 at 23:45
  • regarding: `for(j=0;j<3;j++)` This only loops the statement: `s= s +aloc[i*5+j];` Probably not what you want. – user3629249 Dec 29 '19 at 23:47
  • OT: regarding: `if ((aloc = (float*)malloc(5 * sizeof(float))))` in C, the returned type from the heap allocation functions: `malloc()`, `calloc()` and `realloc()` have type `void*` which can be assigned to any pointer. Casting just clutters the code. Suggest removing that cast. – user3629249 Dec 29 '19 at 23:50
  • DO NOT mix tabs and spaces when indenting code. The result is what you see in your question. – user3629249 Dec 29 '19 at 23:51
  • OT: the posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 3, 5. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using `#define` statements or a `enum` statement to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code. – user3629249 Dec 29 '19 at 23:57
  • when the call to `malloc()` in the function: `reads()` fails, the user is not informed AND the code continues on executing as if nothing were wrong. This needs to be corrected. Perhaps via a call to `perror()` to inform the user, followed by `exit( EXIT_FAILURE );` – user3629249 Dec 30 '19 at 00:00
  • OT: regarding: `scanf("%f", aloc + i*5+j);` When calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. Note: those functions return the number of successful 'input format conversion specifiers'. The current statement contains only 1 specifier so any returned value other than 1 indicates an error occurred. – user3629249 Dec 30 '19 at 00:05
  • to be able to change where a pointer in the calling function points, need to pass a pointer to the pointer to a called function. So this: `void reads(float *aloc) { if ((aloc = (float*)malloc(5 * sizeof(float))))` Should be: `void reads(float **aloc) { if ((*aloc = malloc(5 * sizeof(float))))` AND `reads(aloc);` in function: `main()` should be: `reads( &aloc );` – user3629249 Dec 30 '19 at 00:08

1 Answers1

2

reads() only allocates space for 5 floats -- which is only one column or row of your matrix.

Change:

aloc  =  (float*)malloc(5  *  sizeof(float))

to:

aloc = malloc( 5 * 3 * sizeof(float))
Ghii Velte
  • 125
  • 4