Understanding Pointer Syntax and Structure Access in C Programming
In C programming, the placement of the asterisk (*) is critical for determining pointer semantics. When the asterisk appears inside parentheses with a variable, it declares a pointer.
Pointer Declarations
char *argp[]- Array of pointers to charactersvoid (*func_ptr)(int)- Pointer to a function taking an integer parameterint *func_sum(int n)- Function returning a pointer to an integer
String Comparison with Pointers
The strcmp function accepts two pointer arguments for string comparison:
if (strcmp(ptr1, ptr2) == 0)
if (strcmp(argp[1], "fpga") == 0)
Structure Pointer Access Operations
Different operations on structure pointers yield different results:
*pConfig->workThread- Dereferences the pointerpConfigand accesses theworkThreadmember value&pConfig->workThread- Obtains the memory address of theworkThreadmember within the structure pointed to bypConfig
The arrow operator (->) has higher precedence than the address-of operator (&).
Structure Member Access Examples
typedef struct {
uint32_t flipx;
uint32_t flipy;
} FlipConfig;
FlipConfig flipSettings[MAX_SENSOR_COUNT];
SystemConfig* configPtr = getSystemConfig();
ThreadInfo* threadParam = &configPtr->workerThread;
ThreadParams* params = configPtr->workerThread.privateData;
OSA_mutexLock(threadParam->workLock);
params->isWorking = false;
OSA_mutexUnlock(threadParam->workLock);
Complex Structure Definitions
typedef struct {
DRV_AmbHandle driverHandle;
VBP_Config inputConfig;
DEBUG_CONFIG debugSettings;
uint8_t sensorCount;
SEN_CONFIG sensorConfig[MAX_SENSOR_COUNT];
uint16_t bayerPattern[MAX_SENSOR_COUNT];
MODEL_CONFIG* modelConfig[MAX_SENSOR_COUNT];
VIN_CONFIG vinConfig;
// Various thread information structures
THREAD_INFO workerThread;
THREAD_INFO watchdogThread;
// Additional configuration arrays
struct vindev_video_info videoInfo[MAX_SENSOR_COUNT];
struct iav_chan_cfg channelConfig[MAX_CHANNELS];
} SystemConfig;
SystemConfig* getSystemConfig() {
return globalConfig;
}
Structure Pointer Arrays
typedef struct {
IspCaliParam* ispCalibration;
gainCalcFunc gainFunction;
JpegEncodeInitData jpegInit;
senInit sensorInitUser;
setSenBlackFunc blackFunc;
setAdcOffsetFunc adcOffsetFunc;
UniqueConfig* uniqueConfig;
SensorSpecCONFIG* sensorSpec;
} ModelConfig;
ModelConfig* modelConfigArray[MAX_SENSOR_COUNT];
This demonstrates an array of pointers to ModelConfig structures.