Why this simple code cannot 被successfully compiled?

 

void f(int *j) 
  *j = 100; 
}

I save the above code in a  test.mqh  file. After pressing F5in MetaEditor. The compiler reports two errors: 1) *- comma expected. 2) j- variable not defined.

 Can someone tell me what is the problem? and how should i correct this issue? Thanks a lot for your time and consideration .  


 
tzm:
#property library

void f(int *j) 
  *j = 100; 
}

I save the above code in a  test.mqh  file. After pressing F5in MetaEditor. The compiler reports two errors: 1) *- comma expected. 2) j- variable not defined.

 Can someone tell me what is the problem? and how should i correct this issue? Thanks a lot for your time and consideration .  


I assume  *   is a pointer ?  mql4 doesn't use pointers.
 
RaptorUK:
I assume  *   is a pointer ?  mql4 doesn't use pointers.


RaptorUK: Thank you for your prompt reply. Yes, I intend to use int * j as a  pointer. If mql4 doesnot use pointers, can i use address operator as a reference to return valuevalues from a function? thanks again for your prompt reply.

 

If you are going to let variable be changed inside the procedure then use & before the argument instead of *: 

void f (int& j) { 
  j = 100; 
}
 
tzm:


RaptorUK: Thank you for your prompt reply. Yes, I intend to use int * j as a  pointer. If mql4 doesnot use pointers, can i use address operator as a reference to return valuevalues from a function? thanks again for your prompt reply.

 

You can pass a variable by reference as wmlab  has pointed out or just return the value . . .

int f (int p) 
   { 
   j = 100 * p;
   return(j); 
   }
 

Wmlab's solution can change multiple external variables during a single call. So I chose it because of its flexibility.RatptorUKand Wmlab: Many thanks for your  timely and effective help.

 
tzm:

Wmlab's solution can change multiple external variables during a single call. So I chose it because of its flexibility. Many thanks for timely and effective help.

Sure,  this is also how you would pass an array :-)
 
Interestingly, I did try to pass an array element to call the function f() so as to change the array element directly....(prototyping by Wmlab above: 
void f (int& j) 
  j = 100; 
}
The first statement I used to call f is:
int j[10]; 
f(& j[3];
The second statement I used to call f is:
int j[10]; 
f(& (j[3]);
The third statement I used to call f is:
int j[10]; 
f(j[3]);
all of the above statements are compiled with an error ("incompatible types").So I devised the fourth statments to indirectly change j[3], as follows:
int j[10],k; 
f(k);

j[3]=k;

Any suggestions to make the above above code more concise are highly appreciated....  

 
tzm:
Interestingly, I did try to pass an array element to call the function f() so as to change the array element directly....(prototyping by Wmlab above:  

Any suggestions to make the above above code more concise are highly appreciated....  

Please use the SRC button to post code.

This isn't valid for an array . . .

j = 100; 

 If your function were . . . 

void f (int& j) 
   { 
   j[9] = 100; 
   }

a valid call would be . .

int k[10];
 
f(k);

 then the value of k[9] would be 100

Why do yo want concise code ? 

 
RaptorUK:

Please use the SRC button to post code.

This isn't valid for an array . . .

 If your function were . . . 

a valid call would be . .

 then the value of k[9] would be 100

Why do yo want concise code ? 

 


if we dont use a constant array index but an variable one, as shown in the following function:

int func(int& j, int k)
{
j[k]=100;
}

the code can not be compiled correctly....is it possible to read a document so as to avoid these frequently met issues? thank you, RaptorUK.

 

 
tzm:
.is it possible to read a document so as to avoid these frequently met issues? thank you, RaptorUK.

 

You should find what you need from the documentation and Book

Your issue may be due to the fact that mql4 doesn't support dynamically sized arrays,  when the arrayis declared either give it the size needed . . .

int k[1000];  // array declred as having 1000 elements

 or resize it as needed

int k[];

ArrayResize(k, 1000);   // array resized to have 1000 elements
Reason: