I need someone to explain to me why my Pointer to Pointer arithmatic is failing in my conditional statment

2588 views c

sm is a 2D array of character pointers allocated dynamically, need to understand why my pointer to pointer arithmetic is failing in conditional if in loop structure. 2nd column in sm is where the string is that I need to test with the grade key gk which is array of characters/string. s holds row size and q is column size for 2D array , hm is my heap memory counter for freeing function which is not importing for my question.

 double *cals(char **sm, char *gk, int s, int q, unsigned *hm) {
    int c = 0;
    double *savg = malloc(s * sizeof(double));
    *hm += 1;

    for (int i = 0; i < s; *(savg + i) = c / q * 100 , c = 0,  ++i) {
        for (int j = 0; j < q; ++j) {
            if (*(*(sm + i * STUDENTATT + 1) + j) == *(gk + j)) 
    return savg;

answered question

Show the declaration of the variable passed as sm. In fact, please provide A Minimal, Complete, and Verifiable Example (MCVE).

This: sm + i * STUDENTATT + 1 looks to be past the bounds of what was allocated to sm, assuming it was something like sm = malloc(s * sizeof(char *));

yes it was, char ** sm = malloc(s * STUDENTATT *sizeof(char * )); is the heap allocated statement I used. STUDENTATT is a macro I defined which as a value of 2. so its just a s * 2 two dimensional array.

1 Answer


You need to add (double*) before malloc

posted this

Have an answer?


Please login first before posting an answer.