|
-
April 8th, 2002, 12:36 PM
#1
returning structures
I have written 2 functions. The first works and the second does not. Please note, the calling function does not know the size of the array to be returned, so it cannot be pre-sized.
The problem is that I cannot determine how to make the second work. Thanks for any enlightenment.
// DynArray.cpp : Proto type for creating table on the fly
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
struct Tag
{
int index ;
char tag[5];
char value[125] ;
} ;
Tag * MakeArray1()
{
Tag * tbl = new Tag[2] ;
tbl[0].index = 0 ;
strcpy(tbl[0].tag, "Name\0");
strcpy (tbl[0].value, "Bob\0") ;
tbl[1].index = 1 ;
strcpy(tbl[1].tag, "Name\0") ;
strcpy(tbl[1].value, "Sue\0") ;
return tbl ;
}
int MakeArray2(Tag * tbl)
{
Tag * tmp = new Tag[2];
tmp[0].index = 0 ;
strcpy(tmp[0].tag, "Name\0");
strcpy (tmp[0].value, "Fred\0") ;
tmp[1].index = 1 ;
strcpy(tmp[1].tag, "Name\0") ;
strcpy(tmp[1].value, "Alice\0") ;
tbl = tmp ;
return 0 ;
}
int main(int argc, char* argv[])
{
Tag * Tbl1 ;
Tag * Tbl2 ;
int ret = -1 ;
//Dynamic Array returned via return
Tbl1 = MakeArray1() ;
printf("Name 1 = %s\n", Tbl1[0].value) ;
printf("Name 2 = %s\n", Tbl1[1].value) ;
//Dynamic Array return via argument
ret = MakeArray2((Tag *) Tbl2) ;
if (ret==0)
{
printf("Name 1 = %s\n", Tbl2[0].value) ;
printf("Name 2 = %s\n", Tbl2[1].value) ;
}
else
printf("Error creating table.\n") ;
//need function to delete tables
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|