473,385 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.
Nov 14 '05 #1
2 1458
On 9 Dec 2003 03:04:49 -0800
ri**********@hotmail.com (Neo) wrote:
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.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 14 '05 #2

On Tue, 9 Dec 2003, Flash Gordon wrote:

ri**********@hotmail.com (Neo) wrote:

#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

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob Meade | last post by:
Hi all, I have a login page which has username and password fields, a login button, and 2 validation controls (one for each field) - currently I have controls to display to the summary if the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.