gcc 4.4.4 c89
I have a program that I am testing. I create a struct object called devname and allocate memory so that I can populate the elements. I show them and then free the allocated memory.
However, I get the following error:
invalid operands to binary != (have ‘struct Devices_names’ and ‘void *’)
This is in a for for loop to display structure elements. However, I feel like testing a NULL pointer.
One more question, is there a problem with free?
Thanks so much for any advice,
#include <stdio.h>
#include <stdlib.h>
static struct Devices_names {
#define MAX_NAME_LEN 80
int id;
char name[MAX_NAME_LEN];
} *devname;
static void g_create_device_names(size_t devices);
static void g_get_device_names();
static void destroy_devices();
int main(void)
{
#define DEVICES 5
g_create_device_names(DEVICES);
g_get_device_names();
destroy_devices();
return 0;
}
static void g_create_device_names(size_t devices)
{
size_t i = 0;
devname = calloc(devices, sizeof *devname);
if(devname == NULL) {
exit(0);
}
for(i = 0; i < devices; i++) {
devname[i].id = i;
sprintf(devname[i].name, "device: %d", i);
}
}
static void g_get_device_names()
{
size_t i = 0;
for(i = 0; devname[i] != NULL; i++) { <-- ERROR HERE
printf("Device id --- [ %d ]\n", devname[i].id);
printf("Device name - [ %s ]\n", devname[i].name);
}
}
static void destroy_devices()
{
while(devname != NULL) {
free(devname++);
}
}
source
share