if you look at the function i produced below, (intensity) i want to increment that element of intensity_histogram using the pointer so *(pointer+1)++
means i
intend to increment element 1 of array_histogram
, however the program doesnt allow me to do so, errors are formed, hence my code does not allow me to compile, errors ar at each pointer increment, please help
#include <stdio.h>
#include <stdlib.h>
int mean_func (int array[16]);
void intensity(int array[16], int *pointer);
int main()
{
int pix_in[4][4] = {{0,1,2,3},{4,5,6,7},{0,1,2,3},{4,5,6,7}};
int pix_out[16];
int i;
int j;
int n = 0;
int mean;
int intensity_histogram[7];
for(i =0;i <4; i++ )
{//open for
for(j=0;j<4;j++)
{//open second for
pix_out[n] = pix_in[i][j];
n++;
}//close second for
}//close for
for(i = 0; i<16; i++)
mean = mean_func(pix_out);
intensity(pix_out, intensity_histogram);
for(i=0;i<8;i++)
printf("%d\n", intensity_histogram[i]);
return 0;
}
int mean_func (int array[16])
{
int sum = 0;
int i;
int j;
int average;
for(i = 0; i<16; i++)
{
sum = sum + array[i];
}
average = sum/16;
return average;
}
void intensity (int array[16], int *pointer)
{//open intensity
int i;
for(i = 0 ; i<16;i ++)
{//open for
if(array[i]== 0)
{//open first if
*pointer++;
}//close first if
else if( array[i]== 1)
{
*(pointer+1)++;
}
else if( array[i]== 2)
{
*(pointer + 2)++;
}
else if( array[i]== 3)
{
*(pointer + 3)++;
}
else if( array[i]== 4)
{
*(pointer + 4)++;
}
else if( array[i]== 5)
{
*(pointer + 5)++;
}
else if( array[i]== 6)
{
*(pointer + 6)++;
}
else if( array[i]== 7)
{
*(pointer + 7)++;
}
}//close for
}//close intensity */
try to use (pointer[1])++
instead of *(pointer+1)++
*(pointer+1)++
is equivalent to *((pointer+1)++)
and not equivalent to (*(pointer+1))++
.
So the incrementation here is applied on (pointer+1)
and (pointer+1)
is not an lvalue
the result of (pointer+1)
is an rvalue (not an lvalue) so you can not assign to it a value
(*(pointer+i))++;
or
pointer[i]++;
this is the problem with the associativity
Although others provided correct solutions to the question, namely instead of *(pointer+1)++;
:
(*(pointer+1))++;
or
pointer[1]++;
I want to point out the error:
The increment operator ++
takes precedence over the dereferencing operator *
. Therefore the order in which *(pointer+1)++;
is evaluated is first pointer+1
, the result of which is then applied the increment operator. This is not possible since pointer+1
is a temporary value that is nowhere stored, it is no lvalue
. Incrementing it makes no sense and is a syntax error.
Applying brackets, such that the dereferencing happens before the increment, results in correct code, as the dereferenced pointer is an actual variable in memory.
The code also has another issue which you might or might not be aware of. Because of the missing indentations its not obvious, but you are executing the line mean = mean_func(pix_out);
16 times. This seems unintended.
Also all your if ... else if ...
constructs can be compactified to:
if(array[i] >= 0 && array[i] <= 7)
pointer[array[i]]++;
intensity(pix_out, intensity_histogram);
--> invalid argument passing to the function
Because in this function void intensity (int array[16], int *pointer);
--> In this function your declaring as int pointer
So you have pass an argument as intensity(pix_out, &intensity_histogram);
This might work
Enjoy your coding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.