c++ - Can't get my pointers to send a reference to the array -
#include<stdio.h> #include<stdlib.h> int* getevennumbers(int arr[], int n) { int i, k = 0 , a[50], p; (i = 0; < n; i++) { if (arr[i] % 2 == 0) { arr[k]=arr[i]; k++; } } return arr[k]; } int main () { int i, arr[5000000], n, a[500000], k, *p; printf("\nenter desired length of array:\n\n"); scanf("%d", &n); (i = 0; < n; i++) arr[i]= rand(); getevennumbers (arr, n); printf("\n\neven numbers in array follows:\n\n"); (i = 0; < n; i++) { a[i]= *(p+i); printf("\n[%d] = %d", (i+1), a[i]); } }
please know easy guys need figuring out how return pointer array without values of array getting deleted, can't use global variables , has function returns pointer pointing array
first of all, decrease size of arrays, don't need space. second of all, made
getevennumbers
function return int *
, , not int
. arr[k]
not int *
. don't why returning if nothing being assigned when call function. can change return type void
.
void getevennumbers(int arr[], int n)
you never allocate memory p
. can do
p = (int*) malloc(sizeof(int));
and since never allocated memory p
, following line of code
a[i]= *(p+i);
is assigning a[i]
random address. should try rewrite program. there lot of errors in program didn't correct. go on google finding numbers in array program
or similar , @ code of examples.
edit:
i found code examples use. hope helps!
Comments
Post a Comment