P
Paul Groth
Hi All,
Having lots of dramas trying to pass an array of structures to a function.
I manage to print to the elemnt of the first structure in the array and then
I get a core dump. The code is very simple as I am just experimenting. Can
anyone tell me what I am doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct oid {
char *Name;
int Value;
};
void print_values(struct oid *oids[])
{
printf("%s\n",oids[0]->Name);
printf("%s\n",oids[1]->Name);
printf("%s\n",oids[1]->Name);
}
int main (int argc, char **argv)
{
struct oid list_of_oids[2];
struct oid *pointer_to_oids;
list_of_oids[0].Name=malloc(100);
list_of_oids[1].Name=malloc(100);
list_of_oids[2].Name=malloc(100);
strcpy(list_of_oids[0].Name,"SNMPv2-MIB::sysObjectID.0");
strcpy(list_of_oids[1].Name,"SNMPv2-MIB::sysUpTime.0");
strcpy(list_of_oids[2].Name,"SNMPv2-MIB::sysName.0");
pointer_to_oids=list_of_oids;
print_values(&pointer_to_oids);
}
Thanks
Paul
Having lots of dramas trying to pass an array of structures to a function.
I manage to print to the elemnt of the first structure in the array and then
I get a core dump. The code is very simple as I am just experimenting. Can
anyone tell me what I am doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct oid {
char *Name;
int Value;
};
void print_values(struct oid *oids[])
{
printf("%s\n",oids[0]->Name);
printf("%s\n",oids[1]->Name);
printf("%s\n",oids[1]->Name);
}
int main (int argc, char **argv)
{
struct oid list_of_oids[2];
struct oid *pointer_to_oids;
list_of_oids[0].Name=malloc(100);
list_of_oids[1].Name=malloc(100);
list_of_oids[2].Name=malloc(100);
strcpy(list_of_oids[0].Name,"SNMPv2-MIB::sysObjectID.0");
strcpy(list_of_oids[1].Name,"SNMPv2-MIB::sysUpTime.0");
strcpy(list_of_oids[2].Name,"SNMPv2-MIB::sysName.0");
pointer_to_oids=list_of_oids;
print_values(&pointer_to_oids);
}
Thanks
Paul