473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_re sult *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 1473
On 9 Dec 2003 03:04:49 -0800
ri**********@ho tmail.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_re sult *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**********@ho tmail.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_re sult *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
2340
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 items missing etc.all working fine... However, when the user logs in I fire off to a stored procedure, at this point, unless the login is successful I can return up to about 8 different error numbers in a string which I then split out into an...
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.