0

I am trying to learn C. I am new to C programming. I have the following function.

/*dA would be a pointer to a 2D array*/
    void normalizeCols(float* dMu, float* dSigma, float* dB, float* dA, int n){
       int col, row;
       for(col=0; col < n; col++)
           /*Step 1: calculating mean*/
            float tempMu = 0.0; 
            dMu = &tempMu;
            for (row=0; row < n; row++){
                /*I am adding all the elements of the column*/
                dMu += *(*(dA+row)+col); //ERROR: operand of * must be a pointer
            }
            /*dividing dMu by number of dimension(square matrix)*/              
            dMu /= (float) n; //ERROR: expression must have arithmetic or enum type
            //More code here
       }
}

I am trying to find the mean of a column. I get those two errors that I have commented in the above snippet. How should I fix this?

5
  • 1
    Define more clearly what you mean by dA += dA[row][col]? Commented Mar 10, 2014 at 15:38
  • Do what? What are you trying to do? Are you trying to shift pointer dA? Or are you trying to add something to the value of *dA? There's at least two completely different ways ways to "fix" your code. Nobody knows which one is the correct one until you explain what you are trying to do. Commented Mar 10, 2014 at 15:51
  • think he's trying to add all the elements in the 2d array and find the mean. Commented Mar 10, 2014 at 15:53
  • I have changed my question which was wrong initially. Commented Mar 10, 2014 at 15:53
  • All other things aside, no single answer is going to breach the inescapable; you need to spend more time in C-pointer tutorials and texts. The last thing you should do when not crystal-clear on their usage is throw more code at the problem. Commented Mar 10, 2014 at 16:01

4 Answers 4

1

If you know that the matrices are square (i.e. the row-length is n which is also the number of rows), just do the addressing manually.

The inner loop then becomes:

       /*Step 1: calculating mean*/
       float tempMu = 0;
       for (row=0; row < n; row++){
           /*I am adding all the elements of the column*/
           tempMu += dA[col * n + row];
       }
       /*dividing dMu by number of dimension(square matrix)*/              
       tempMu /= (float) n;

Also, make the input arguments const to make it clearer, and switch int to size_t.

Of course, make sure you do the accesses in the proper order (either row-major or column-major) otherwise you'll get horrible cache thrashing.

Sign up to request clarification or add additional context in comments.

Comments

1

On this line:

dMu += *(*(dA+row)+col); //ERROR: operand of * must be a pointer

Note that dA has type float*, therefore *(dA+row) is a float, col is promoted to a float in order to be added to this value, which is now in the outermost parentheses. When you dereference that with the leftmost *, you are attempting to dereference a float, which is the source of your error.

In order for that line to be type correct, dA would have to be float**, but you have other errors: dMu here, for example, is a pointer, which you are incrementing with +=, not a value. Did you mean *dMu += ... ?

Comments

1

(dA+row) is a pointer, that is moved from dA a distance of row times the size of the type dA points to.

*(dA+row) gives the value of the location pointed by (dA+row) the pointer

*(dA+row)+col adds that value to col

*(*(dA+row)+col) is illegal because you can only de-reference a pointer which this is not.

Your tempMu should be:

tempMu += *(dA + row * n + col)

1 Comment

dMu in this case is also a pointer, so that's not correct either.
0

Not very clear what do you want to do. From the code I see that your are trying to make some "dangerous" operations with pointers.

            /I am adding all the elements of the column**/
            dMu += *(*(dA+row)+col);

You are not adding all the elements in the column but you are moving dMU pointer to another memory location.

*dMU += dA[row][col]
....
*dMu /= (float) n;

it should be correct.

1 Comment

It never even gets that far because he's doing a double dereference on a single-level pointer. The error message is coming from the leftmost * dereferencing a float in the outermost parentheses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.