I'm trying to iterate through a variables of a struct, with a function. When I give the starting address of the pointer, I get a warning: "initialization from incompatible pointer type [-Wincompatible-pointer-types]". When I add this pointer to the function, I got an other warning: "passing argument 1 of ‘ManipulateMessage’ from incompatible pointer type [-Wincompatible-pointer-types]". I've searched these errors, but the results didn't help me. The code actually is working, but I want to avoid undefined behavior. What is the cleanest way of this simple code?
#include <stdio.h>
#include <stdlib.h>
/*************************************************
include
**************************************************/
//A message structure
typedef struct SIGNAL_STRUCTURE
{
int name;
int manipstarttime;
} signal_structure;
//Structure what collects all the signals
typedef struct SIGNAL_COLLECTOR
{
signal_structure EngSpeed;
signal_structure TransReqGear;
signal_structure CurrentGear;
} signal_collector;
//Function to do with the above structure
void ManipulateMessage(signal_structure * signal)
{
signal->name = 10;
signal->manipstarttime = 11;
}
/*************************************************
main
**************************************************/
void fcn(signal_collector * param_signal, int len)
{
int *pointer = param_signal;
while(len--)
{
printf("pointer: %p\n", pointer);
ManipulateMessage(pointer);
pointer += (sizeof(signal_structure) / sizeof(int));
}
}
int main(void)
{
signal_collector dummy;
fcn(&dummy, 2);
return 0;
}
signal_structure *as anint *? - Andrew Henleint *pointerwithsignal_structure *pointerandpointer += (sizeof(signal_structure) / sizeof(int));withpointer++;- Kevinfcn()expects the argument to be an array ofsignal_collectorstructures. You're passing a singlesignal_collector, but telling it that there are 2 of them in thelenargument. - Barmarwhile (len--)will iterate 3 times, not 2 times.while (--len)will iterate 2 times. - Barmarsize_t iinstead... :) - Some programmer dude