hi everyone,
i have written a code in c++. first i created a structure "elements". then declared a array "m" of datatype "elements" in the main(). When i passed this variable to a function by reference, i got this error
tdma.cpp:7:12: error: no match for ‘operator[]’
i got the same error for many lines within the function
below is the code which i am working on.
#include<iostream>
struct elements
{ float a,b,d,p,f,q;
};
int tdma(struct elements m)
{ int n=0;
while(m[n].a!='\0') // here is the error- m[n].a
{ n++;}
m[0].p=-(m[0].b)/(m[0].d);
m[0].q=m[0].f/m[0].d;
for(int i=1;i<n;i++)
{ m[i].p=-m[i].b/(m[i].d+m[i].a*m[i-1].p);
m[i].q=(m[i].f-m[i].a*m[i-1].q)/(m[i].d+m[i].a*m[i-1].p);
}
m[n-1].p=0;
return 0;
};
int main()
{ using namespace std;
float tdm[4][4]; elements m[4];
int n=4;
forgot to tell you, the program is not complete. there may be other irrelevant mistakes in the program, please dont care about that and just help me with the error.
thanks Moschops for the help, i dont see the error now after the modification you suggested. just one more thing, if i want to pass it by reference then can i try
You can pass the array by reference, but there's very little point in doing that. When you pass an array, you actually pass a pointer to that array, so any changes you make persist when the function returns.
When you pass a pointer to an array you would also need to pass the dimensions of the array.
I disagree. I regularly pass just a pointer to an array. It's common practice. strcpy springs to mind as a commonly used function in which just a pointer to the array is passed.
I regularly pass just a pointer to an array. It's common practice.
I never said otherwise. I also regularly pass pointer to arrays (along with the dimensions of the array). Char arrays are a bit different because they use a null character to terminate the string but on the whole you need a method of knowing how big the array is.