Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning af struct

Status
Not open for further replies.

kemsa

Programmer
Feb 8, 2005
3
DK
Hello;

I´m struggling with a program, which I want to return a struct.

My program looks like this:

void make_bookingliste()
{
/* Disse konstanter definerer vore øvre og nedre grænse. Bruges til rand() */
const int LOW = 1;
const int HIGH = sizeof (personer) / sizeof (struct typer);

int k, j, tal;

struct typer liste[400];

/* Erklærer variabel til at indeholde sekunder på uret */
time_t sekunder;
/* Få værdien fra system-klokken og placer det i variablen sekunder */
time(&sekunder);
/* Konvertér sekunder til en unsigned integer */
srand((unsigned int) sekunder);

printf("Navn\t\t\t\t\tVIP (1 = VIP)\n");
printf("-----------------------------------------------------------------\n");

for(k = 1 ; k <= 140 ; k++)
{
tal = rand() % (HIGH - LOW) + LOW;

printf("%d ", k);

if(k <= 12)
personer[tal].vip = 1;
else
personer[tal].vip = 0;

liste[tal] = personer[tal];

print_typer (&personer[tal]);
}
}

Now, I would like this funktion to return the struct "list", which should be a copy of "personer". How is that possible? Thanks...
 
Liste is an array of structs declared statically
and locally. You cannot return an array.
What you can do is allocate the array and return
a pointer to it.
Code:
struct typer *liste = malloc(400 * sizeof(struct typer));
/* rest of your code */
return liste;
 
Dou you mean something like that. (And thank you for your quick answer):

void make_bookingliste()
{
struct typer *liste = malloc(400 * sizeof(struct typer));
/* Disse konstanter definerer vore øvre og nedre grænse. Bruges til rand() */
const int LOW = 1;
const int HIGH = sizeof (personer) / sizeof (struct typer);
int k, j, tal;

/* Erklærer variabel til at indeholde sekunder på uret */
time_t sekunder;
/* Få værdien fra system-klokken og placer det i variablen sekunder */
time(&sekunder);
/* Konvertér sekunder til en unsigned integer */
srand((unsigned int) sekunder);

printf("Navn\t\t\t\t\tVIP (1 = VIP)\n");
printf("-----------------------------------------------------------------\n");

for(k = 1 ; k <= 140 ; k++)
{
tal = rand() % (HIGH - LOW) + LOW;

printf("%d ", k);

if(k <= 12)
personer[tal].vip = 1;
else
personer[tal].vip = 0;

liste[tal] = personer[tal];

print_typer (&personer[tal]);
}
return liste;

}
 
Code:
lude <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#define RANDOMSTRING(l,str) {int i = 0; while (i < l) {str[i] = (char)(1 + rand() % 127); ++i;} str[i] = '\0';}
#define BSZ 256
#define ALEN 10

typedef struct mytype
{
  int vip;
  char record[BSZ];
} M;



M *retM (int);
void printIt (M *, int);
void head (void);
void foot (void);

int
main (void)
{
  M *foo = NULL;

  foo = retM (ALEN);
  printIt (foo, ALEN);
  return 0;
}

M *
retM (int sz)
{
  int y = 0;
  char buf[BSZ];
  M *foo1 = malloc (sz * sizeof (M));

  if (foo1 == NULL)
    {
      return NULL;
    }
  while (y < sz)
    {
      RANDOMSTRING (BSZ, buf);
      strncpy (foo1[y].record, buf, BSZ);
      foo1[y].vip = (y % 4 == 0 ? 1 : 0);
      ++y;
    }
  return foo1;
}


void
printIt (M * thing, int len)
{
  int y = 0;

  while (y < len)
    {
      /*head(); */
      printf ("\n\nElement at %d , with vip value = %d and record = %s\n\n",
	      y, thing[y].vip, thing[y].record);
      /*foot(); */
      ++y;
    }
  return;
}
An example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top