Enable your compiler's warnigs!!! You should get something like this:
prog.c: In function 'main':
prog.c:8:16: warning: comparison between pointer and integer
peaks == nums[i];
^~
prog.c:8:16: warning: statement with no effect [-Wunused-value]
peaks == nums[i];
~~~~~~^~~~~~~~~~
prog.c:10:13: warning: returning 'int *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
return peaks;
^~~~~
prog.c:10:13: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c:13:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
printf("Peak numbers are %d",peaks);
~^ ~~~~~
%ls
So, change this:
peaks == nums[i];
to this:
peaks[j] = nums[i];
where j
is another counter you could use.
Then you return peaks
, why? It's not a custom function, so you shouldn't return.
Moreover, you try to print an array like:
printf("Peak numbers are %d",peaks);
which is not possible, you need to loop over the elements of the array, like this:
for(i = 0; i < 4; ++i)
printf("%d\n", peaks[i]);
After correcting your warnings, you are ready to actually work on yuor logic. Your for loop will not iterate over all numbers of the array. So change this:
for(i = 0; i < nums[i]; i++)
to this:
for(i = 0; i < 14; i++)
Moreover your if statement will cause Undefined Behavior:
if(nums[i] > nums[i-1] && nums[i] > nums[i+1])
when i
is 0 and when i
is 13.