Passing array of structures to a function

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
 
E

Eric Gorr

Paul Groth said:
struct oid list_of_oids[2];

You have said here that you only want two oid structs.
list_of_oids[0].Name=malloc(100);
list_of_oids[1].Name=malloc(100);
list_of_oids[2].Name=malloc(100);

Here, you are trying to use three oid structs.
Try

struct oid list_of_oids[3];

There may be other problems, but this was the most obvious one.
 
S

signuts

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);

what you're trying to do is this
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);
Should check to see if these malloc's fail (return NULL)
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);
we don't need to pass the addressof the pointer, this is a result of
declaring print_values(struct oid *oids[]) (you probably changed this to
get around the compiler error)


/* we want this */
exit(EXIT_SUCCESS);


Hoep this helps
 
E

Emmanuel Delahaye

Paul Groth said:
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);

You probably wanted :

printf("%s\n",oids[2]->Name);
}

int main (int argc, char **argv)
{
struct oid list_of_oids[2];

Warning. If you want to acces to the [2] element, you need an array of [3].
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");

<OT>
Warning, AFAIK, the MIB strings go up to 256 bytes (255 char + 0)
pointer_to_oids=list_of_oids;
print_values(&pointer_to_oids);

Don't forget to free the memory blocks you don't need anymore.
 
M

Mr. 4X

Paul Groth said:
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[])

IIRC a POINTER, POSSIBLY TO AN ARRAY would be done with *oids or oids[]
(remember: ARRAYS ARE ALWAYS PASSED VIA THEIR POINTERS) but this looks more
like an POINTER TO <POINTER TO oids STRUCTURE>, POSSIBLY AN ARRAY OF THEM.

Remember the alternative forms of the main(...) arguments:

char *argv[]
char **argv

because main's argv is a POINTER TO AN ARRAY OF CHAR POINTERS!
{
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
 
A

Al Bowers

Paul said:
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[])

This function definition should be:
void print_values(const struct oid *oids)
{
printf("%s\n",oids[0].Name);
printf("%s\n",oids[1].Name);
printf("%s\n",oids[1].Name);
}

And the call in main should be:
print_values(list_of_oids);
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);
}

To simply(remove dynamic allocatiosn):

#include <stdio.h>
#include <string.h>

struct oid {
char Name[100];
int Value;
};

void print_values(const struct oid *oids)
{
printf("%s\n",oids[0].Name);
printf("%s\n",oids[1].Name);
printf("%s\n",oids[1].Name);
}

int main (void)
{
struct oid list_of_oids[3];

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");
print_values(list_of_oids);
return 0;
}

Al Bowers
Tampa, Fl USA
mailto: (e-mail address removed) (remove the x)
http://www.geocities.com/abowers822/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top