SUMMARY: How to return an array from a sub-routine.

N

Neo

Thanks to all those who responded. Your tips really worked. So I will
give a simple program to explain, how I did it.

#include<stdio.h>
void update();
struct fields
{
char f1[20];
char f2[20];
char f3[20];
};
typedef struct
{
struct fields row[5];
} query_result;
main()
{
query_result e;
update(&e);
printf("\nYou entered %s\n",e.row[1].f1);
printf("\nYou entered %s\n",e.row[2].f2);
printf("\nYou entered %s\n",e.row[3].f3);
}
void update(query_result *p)
{
char n[20];
printf("\nInput first number: ");
scanf("%s",&n);
strcpy(p->row[1].f1,n);
printf("\nInput second number: ");
scanf("%s",&n);
strcpy(p->row[2].f2,n);
}

I defined a structure in the main program and then passed its
reference to the function (not sub-routine ;-)). Then from within the
function I populated the output in structure and it was passed on to
main function successfully.

Thanks once again.

Take care,
Rizwan.
 
F

Flash Gordon

On 9 Dec 2003 03:04:49 -0800
Thanks to all those who responded. Your tips really worked. So I will
give a simple program to explain, how I did it.

#include<stdio.h>
void update();
struct fields
{
char f1[20];
char f2[20];
char f3[20];
};
typedef struct
{
struct fields row[5];
} query_result;
main()
{
query_result e;
update(&e);
printf("\nYou entered %s\n",e.row[1].f1);
printf("\nYou entered %s\n",e.row[2].f2);
printf("\nYou entered %s\n",e.row[3].f3);
}
void update(query_result *p)
{
char n[20];
printf("\nInput first number: ");

This may not be printed before the scanf is run. You need
fflush(stdout);
scanf("%s",&n);

What happens if the user types in more that 19 characters?
Answer, undefined behaviour which could be something relatively harmless
like crashing your entire system to erasing your hard disk or giving a
hacker root access.
strcpy(p->row[1].f1,n);
printf("\nInput second number: ");
scanf("%s",&n);
strcpy(p->row[2].f2,n);

You seem to have forgotten row[3].f3. which you print out above.
}

I defined a structure in the main program and then passed its
reference to the function (not sub-routine ;-)). Then from within the
function I populated the output in structure and it was passed on to
main function successfully.

Thanks once again.

I think you need to re-read about arrays and structures as well as
scanf.

The above looks like you could not decide whether you wanted a
structure with three char arrays or an array of char arrays, so you've
got an array of structures containing char arrays giving you one more
level of data structure than you need.
 
A

Arthur J. O'Dwyer

#include<stdio.h>
void update();
struct fields
{
char f1[20];
char f2[20];
char f3[20];
};
typedef struct
{
struct fields row[5];
} query_result;

As Mark points out, this is an unnecessary layer of indirection,
unless you're planning to add more members to the structure someday.
A simpler method would be to write

typedef struct fields query_result[5];

and then simply search-and-delete on ".row" in the rest of your
code.
main()
{
query_result e;
update(&e);
printf("\nYou entered %s\n",e.row[1].f1);
printf("\nYou entered %s\n",e.row[2].f2);
printf("\nYou entered %s\n",e.row[3].f3);
}
void update(query_result *p)
{
char n[20];
printf("\nInput first number: ");

This may not be printed before the scanf is run. You need
fflush(stdout);
scanf("%s",&n);

What happens if the user types in more that 19 characters?
Answer, undefined behaviour which could be something relatively harmless
like crashing your entire system to erasing your hard disk or giving a
hacker root access.

Or it could be something dangerous. ;-)
Anyway, the OP *already* has undefined behavior in the above code;
he's passing the "%s" specifier to scanf(), which expects to see a
pointer to char; but he's giving it '&n', which is a pointer to char[20].
This alone is enough to crash the program, although it doesn't do so
on most machines today. Fixed both bugs:

scanf("%19s", n);

Read up on "format specifiers" to find out what the "19" does (even
though I bet you can guess if you don't know already).

strcpy(p->row[1].f1,n);
printf("\nInput second number: ");
scanf("%s",&n);
strcpy(p->row[2].f2,n);

You seem to have forgotten row[3].f3. which you print out above.
}

I defined a structure in the main program and then passed its
reference to the function (not sub-routine ;-)). Then from within the
function I populated the output in structure and it was passed on to
main function successfully.

Thanks once again.

I think you need to re-read about arrays and structures as well as
scanf.

The above looks like you could not decide whether you wanted a
structure with three char arrays or an array of char arrays, so you've
got an array of structures containing char arrays giving you one more
level of data structure than you need.

Not that there's anything wrong with that... :)

-Arthur
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top