unable to convert contents in argv[] into float[][] in C
Asked Answered
W

4

5

I am doing a program where I'm multiplying matricies, but my big issue is converting from the input into the two arrays that I'll eventually be multiplying. The following is my code for conversion including the declaration of the arrays. (I removed validation that the input is 8 valid floats as I've been debugging it).

    //declare the arrays
float a[2][2];
float b[2][2];
float c[2][2];

int main (int argc, char *argv[])
{
    int i,j,k,l;

    i=0;
    l=4;
// declare and initialize arrays
   for( j =0; j<2; j++)
   {
       for(k=0;k<2; k++)
       {
           a[j][k]=atof[argv[i]];
           b[j][k]=atof[argv[l]];
           i++;
           l++;
       }
   }
......

I get an error when using atof at compilation that says: "subscripted value is neither array nor pointer" I've been looking up the error, but haven't figured out what it means in my case.

Wenonawenonah answered 9/3, 2011 at 5:54 Comment(3)
have you tried to dimension argv at all? are you passing it in as a parameter?Liss
did you mean atof(argv[i]);?Ternary
Please consider the strtof() function over atof(). The strto*() family has the advantage of error checking, which the ato*() family lacks.Bridgetbridgetown
S
5

I think what you want is the following:

a[j][k]=atof(argv[i]);

Note the use of () rather than [] around argv[i] - atof is a function, not an array.

Scabby answered 9/3, 2011 at 5:58 Comment(0)
M
2

atof is a function - you can call functions using (), not the subscript operator [].

 a[j][k] = atof(argv[i]);

I assume this was a typo - perhaps change your font?

Misquotation answered 9/3, 2011 at 5:58 Comment(0)
S
1

Use

atof(argv[i])

instead of

atof[argv[i]]

Beware the difference between [] and ().

Smalt answered 9/3, 2011 at 5:57 Comment(0)
F
1

atof is a function, so you should use atof(argv[i]);

Fairish answered 9/3, 2011 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.