Finding even numbers by using pointers in c++ -
i doing assignment pointers. in 1 of question, asks me find numbers in array , print of them. have use signature given assignment , can not use use & operator or [] notation in function.
signature: void print_evens(int *nums,int length) ;
i know have use,
if(i%2==0) cout << << endl;
to find numbers don't know how pointers.
how can pass array main function print_evens
since there no parameters array?
thank help.
how can pass array main function print_evens since there no parameters array?
you can never pass array function, regardless of signature. arrays decay pointers first element in such situation.
in case, need dereference pointer value @ current location.
for (int = 0; < length; ++i) { int current = *(nums + i); if ((current % 2) == 0) cout << current << endl; }
and can pass in array (which decays pointer of course)
#define len 10 int main(void) { int arr[len]; /* initialize elements of arr */ print_evens(arr, len); }
Comments
Post a Comment