Skip to main content
Associate III
June 11, 2026
Solved

Incompatible pointer assignment

  • June 11, 2026
  • 4 replies
  • 53 views

Hi Guys,

I am writing a code to measure a pulsating DC voltage. All that I have decided to take 50 samples and take the highest one.  Multiplying the peak value by a factor will give me the approximate RMS value. Please see my code. When built, an error “ Incompatible pointer assignment” shows. 

#include "stm8s.h"

unsigned int max_v;
unsigned k;
unsigned int val[100];

void Clock_Config(void) {
CLK->ICKR |= CLK_ICKR_HSIEN; // Enable HSI
while (!(CLK->ICKR & CLK_ICKR_HSIRDY)); // Wait for stabilization
CLK->CKDIVR = 0x00; // HSI 16MHz / 1 = 16MHz
CLK->SWR = 0xE1; // Use HSI as master clock
}

void adc_init(void) {
ADC1 -> CR1 = 0x40;
ADC1 -> CR2 = 0x08;
ADC1 -> TDRH = 0xFF;
ADC1 -> TDRL = 0xFF;
ADC1 -> CR1 |= ADC1_CR1_ADON;
}

uint16_t read_mains_value(void) {
unsigned int value;
ADC1 -> CSR = 0x05;
ADC1_StartConversion();
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
value = ADC1_GetConversionValue();
ADC1_ClearFlag(ADC1_FLAG_EOC); //
return value;
}

void delay_us (int us) //Function Definition
{
int i =0 ;
int j=0;
for (i=0; i<=us; i++)
{
for (j=0; j<16; j++) // Nop = Fosc/4
_asm("nop"); //Perform no operation //assembly code <span style="white-space:pre"> </span>
}
}


void main(void)
{

Clock_Config();

GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);
adc_init();

for ( k = 0; k<50; k++ ) {
val[k] = read_mains_value;
delay_us(200);
}
max_v = 0;
for ( k = 0; k < 50; k++) {
if (val[k] > max_v) {
max_v = val[k];
}
val[k] = 0;
}

 

Best answer by Andrew Neil

So where, exactly, does that error occur ?

 

This can’t be right:

val[k] =  read_mains_value;

Did you mean:

val[k] =  read_mains_value();

 

Also:

uint16_t read_mains_value(void) {
unsigned int value;

You should make up your mind whether you’re using uint16_t or unsinged int

4 replies

Andrew Neil
Andrew NeilBest answer
Super User
June 11, 2026

So where, exactly, does that error occur ?

 

This can’t be right:

val[k] =  read_mains_value;

Did you mean:

val[k] =  read_mains_value();

 

Also:

uint16_t read_mains_value(void) {
unsigned int value;

You should make up your mind whether you’re using uint16_t or unsinged int

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
ankholaAuthor
Associate III
June 11, 2026

Thanks Andrew Neil. I made a gross mistake. In place of  “read_mains_value()” I have written “read_mains_value”. Now the code has been built without error.

Andrew Neil
Super User
June 11, 2026

You’re welcome.

 

In C, when you give the name of a function without the parentheses, that gives the address of the function 0 ie, a pointer.

As the error message suggests, a pointer is not compatible with your var[k] for assignment.

 

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
David Littell
Senior II
June 12, 2026

What, pray tell, is the shape of this “pulsating DC voltage”?  

ankholaAuthor
Associate III
June 12, 2026

The shape is just positive half cycles of ac main. Actually, I want to measure the ac main voltage by  rectifying  the stepped down voltage. Precision is not so important.