473,657 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fill structure's fields in a loop ?

Hello,

Can anyone tell me how do i fill a structure's fields in a for loop ?

#define MAX_FIELD_LEN 10

typedef struct
{
char field1[MAX_FIELD_LEN]; /*will store first string read from
keyboard*/
char field2[MAX_FIELD_LEN]; /*will store 2nd string read from
keyboard*/
char field3[MAX_FIELD_LEN]; /*will store 3rd string read from
keyboard*/
} RECORD;

i want to read 3 strings from stdin and assign those strings in a for
loop:

RECORD rec, *pRec;
pRec = &rec;

char tmp[3][MAX_FIELD_LEN];

for(i=0;i<3;i++ );
{
gets(tmp[i], MAX_FIELD_LEN +1);

/*Here i want to fill in the fields of 'rec' struct one
field per iteration...How do i do that ?*/
}

Jun 21 '07 #1
24 4431
"ol****@gmail.c om" <ol****@gmail.c omwrote:
Can anyone tell me how do i fill a structure's fields in a for loop ?
You can't.
#define MAX_FIELD_LEN 10

typedef struct
{
char field1[MAX_FIELD_LEN]; /*will store first string read from keyboard*/
char field2[MAX_FIELD_LEN]; /*will store 2nd string read from keyboard*/
char field3[MAX_FIELD_LEN]; /*will store 3rd string read from keyboard*/
} RECORD;

i want to read 3 strings from stdin and assign those strings in a for
loop:

RECORD rec, *pRec;
pRec = &rec;

char tmp[3][MAX_FIELD_LEN];

for(i=0;i<3;i++ );
{
gets(tmp[i], MAX_FIELD_LEN +1);
Don't bloody use gets()! Don't they teach the children basic road safety
anymore?

Besides, that's not how you call gets() in the first place.
/*Here i want to fill in the fields of 'rec' struct one
field per iteration...How do i do that ?*/
You can't. If you want to use an loop and an index, use an array instead
of a struct. If you need to use a struct, you'll have to write the
member assignments out in full.

Richard
Jun 21 '07 #2
On Jun 21, 1:39 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"oli...@gmail.c om" <oli...@gmail.c omwrote:
Can anyone tell me how do ifilla structure'sfiel dsin a for loop ?

You can't.
#define MAX_FIELD_LEN 10
typedefstruct
{
char field1[MAX_FIELD_LEN]; /*will store first string read from keyboard*/
char field2[MAX_FIELD_LEN]; /*will store 2nd string read from keyboard*/
char field3[MAX_FIELD_LEN]; /*will store 3rd string read from keyboard*/
} RECORD;
i want to read 3 strings from stdin and assign those strings in a for
loop:
RECORD rec, *pRec;
pRec = &rec;
char tmp[3][MAX_FIELD_LEN];
for(i=0;i<3;i++ );
{
gets(tmp[i], MAX_FIELD_LEN +1);

Don't bloody use gets()! Don't they teach the children basic road safety
anymore?

Besides, that's not how you call gets() in the first place.
/*Here i want tofillin thefieldsof 'rec'structone
field per iteration...How do i do that ?*/

You can't. If you want to use an loop and an index, use an array instead
of astruct. If you need to use astruct, you'll have to write the
member assignments out in full.

Richard
Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function. Second, what's the
problem with gets() in getting a string from stdin? And if you are
such and expert in C why don't you make a recommandation about that
matter, explaining the difference ?

Jun 21 '07 #3
On Jun 21, 1:49 pm, "oli...@gmail.c om" <oli...@gmail.c omwrote:
On Jun 21, 1:39 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"oli...@gmail.c om" <oli...@gmail.c omwrote:
Can anyone tell me how do ifilla structure'sfiel dsin a for loop ?
You can't.
#define MAX_FIELD_LEN 10
typedefstruct
{
char field1[MAX_FIELD_LEN]; /*will store first string read from keyboard*/
char field2[MAX_FIELD_LEN]; /*will store 2nd string read from keyboard*/
char field3[MAX_FIELD_LEN]; /*will store 3rd string read from keyboard*/
} RECORD;
i want to read 3 strings from stdin and assign those strings in a for
loop:
RECORD rec, *pRec;
pRec = &rec;
char tmp[3][MAX_FIELD_LEN];
for(i=0;i<3;i++ );
{
gets(tmp[i], MAX_FIELD_LEN +1);
Don't bloody use gets()! Don't they teach the children basic road safety
anymore?
Besides, that's not how you call gets() in the first place.
/*Here i want tofillin thefieldsof 'rec'structone
field per iteration...How do i do that ?*/
You can't. If you want to use an loop and an index, use an array instead
of astruct. If you need to use astruct, you'll have to write the
member assignments out in full.
Richard

Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function. Second, what's the
problem with gets() in getting a string from stdin? And if you are
such and expert in C why don't you make a recommandation about that
matter, explaining the difference ?
Anyway, thanks for your answer...

Jun 21 '07 #4
Le 21-06-2007, ol****@gmail.co m <ol****@gmail.c oma écrit*:
On Jun 21, 1:39 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>"oli...@gmail. com" <oli...@gmail.c omwrote:
Can anyone tell me how do ifilla structure'sfiel dsin a for loop ?

Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function.
First: the code you post uses gets, not _gets
Second: why did you not use fgets
Second, what's the
problem with gets() in getting a string from stdin?
Buffer overflow.

Marc Boyer
Jun 21 '07 #5
ol****@gmail.co m said:

<snip>
Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function.
So what? Your code didn't call _gets(). It called gets(). So your
objection is spurious.
Second, what's the
problem with gets() in getting a string from stdin?
It's a security breach. gets() is impossible to use safely, because you
have no way to protect your buffer from accidental or malicious
overruns. This is how the 1988 Worm brought down a big chunk of the
Internet in a very short time. Don't Do That.
And if you are such and expert in C
No matter what else you may think about him, Richard Bos is certainly an
expert in C, and you would be wise to listen to him instead of trying
to argue with him.
why don't you make a recommandation about that
matter, explaining the difference ?
Because this newsgroup's archives are already chock-full of explanations
about why calling gets() is dumb and what you should call instead. So
if you want to know (and you *should* want to know), go check the
archives.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 21 '07 #6
On Jun 21, 2:46 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
oli...@gmail.co m said:

<snip>
Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function.

So what? Your code didn't call _gets(). It called gets(). So your
objection is spurious.
My code, CALLED _gets(). The code i entered HERE didnt called
_gets(). Come on, man...it was a typing mistake.

Second, what's the
problem with gets() in getting a string from stdin?

It's a security breach. gets() is impossible to use safely, because you
have no way to protect your buffer from accidental or malicious
overruns. This is how the 1988 Worm brought down a big chunk of the
Internet in a very short time. Don't Do That.
Ok, i understand this. The thing is that, in my code i used, as i
said a nonportable version of gets(), with 2 parameters. The second
parameter is the maximum input of characters from stdin (the maximum
no. of chars that you are allowed to enter). So this version is safer
than standard gets(). Or is something else that you were referring
at ?

And if you are such and expert in C

No matter what else you may think about him, Richard Bos is certainly an
expert in C, and you would be wise to listen to him instead of trying
to argue with him.
I don't think anything about anyone.I don't know Richard Bos and if he
is, or not an expert. I'm just a regular guy who tries to get an
answer from someone who is willing to help other people on a free web
site especially designed for this purpose, so as a consequence, i
don't like this attitude (qoute: "Don't bloody use gets()! Don't they
teach the children basic road safety
anymore?"). Since i'm not his student, and he is not my teacher, a
little respect is necessary from his side.

why don't you make a recommandation about that
matter, explaining the difference ?

Because this newsgroup's archives are already chock-full of explanations
about why calling gets() is dumb and what you should call instead. So
if you want to know (and you *should* want to know), go check the
archives.
Believe me, the first thing i do when i need something is to
search for it! I didnt't find it (if you find a similar question to my
question (not about gets()) on this group, please let me know) so i
posted 'again'...Is that such a terrible thing ?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Jun 21 '07 #7
ol****@gmail.co m said:
On Jun 21, 2:46 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>oli...@gmail.c om said:

<snip>
Ok, first of all.. i forgot to mention: that _gets is actually a
non portable version of standard C gets() function.

So what? Your code didn't call _gets(). It called gets(). So your
objection is spurious.

My code, CALLED _gets(). The code i entered HERE didnt called
_gets(). Come on, man...it was a typing mistake.
We're not psychic. We assume the code you post is the code about which
you're inviting comment. If you don't want us to waste our time and
yours debugging typos, use copy and paste rather than re-typing your
code.
Second, what's the
problem with gets() in getting a string from stdin?

It's a security breach. gets() is impossible to use safely, because
you have no way to protect your buffer from accidental or malicious
overruns. This is how the 1988 Worm brought down a big chunk of the
Internet in a very short time. Don't Do That.

Ok, i understand this. The thing is that, in my code i used, as i
said a nonportable version of gets(), with 2 parameters. The second
parameter is the maximum input of characters from stdin (the maximum
no. of chars that you are allowed to enter). So this version is safer
than standard gets(). Or is something else that you were referring
at ?
If you have a question about non-portable implementation library
functions, you need to ask them in a newsgroup that deals with that
implementation. Here, we discuss standard C, not extensions.
And if you are such and expert in C

No matter what else you may think about him, Richard Bos is certainly
an expert in C, and you would be wise to listen to him instead of
trying to argue with him.

I don't think anything about anyone.I don't know Richard Bos and if he
is, or not an expert.
He is.
I'm just a regular guy who tries to get an
answer from someone who is willing to help other people on a free web
site especially designed for this purpose,
Then I suggest you find a Web site especially designed for that purpose.
Usenet is *not* a Web site, and that remains true even if you happen to
have found a Web-based gateway to Usenet. Usenet uses the NNTP protocol
and port 119, whereas the Web uses the HTTP protocol and port 80.

so as a consequence, i
don't like this attitude (qoute: "Don't bloody use gets()! Don't they
teach the children basic road safety
anymore?"). Since i'm not his student, and he is not my teacher, a
little respect is necessary from his side.
Respect must be earned. Richard Bos has earned respect on this newsgroup
over a period of many years, because of his expertise in C and his
willingness to provide high-quality help to comp.lang.c subscribers (at
no charge, I might add). He is under no *obligation* to give you the
fluffy-bunny treatment. If you want fluffy-bunny, you'll need to spend
more time researching your problem, to demonstrate that you're not
using this wonderful comp.lang.c resource as a substitute for thinking
about the problem yourself.
why don't you make a recommandation about that
matter, explaining the difference ?

Because this newsgroup's archives are already chock-full of
explanations about why calling gets() is dumb and what you should
call instead. So if you want to know (and you *should* want to know),
go check the archives.

Believe me, the first thing i do when i need something is to
search for it! I didnt't find it (if you find a similar question to my
question (not about gets()) on this group, please let me know) so i
posted 'again'...Is that such a terrible thing ?
The comment on explanations about gets() was in response to your asking
why Richard Bos didn't explain why gets() is bad and what to use
instead. With regard to your *original* question, you did the right
thing in checking out whether the question had been asked before.

It has, actually, but I can hardly blame you for not finding it, since
it's so difficult to think up the right search terms, and anyway Google
Groups (which, for all its faults, has the least incomplete clc
archive) has gone downhill in recent years, making it much harder to
search for useful information than it used to be.

Having said all that, I'll now answer your original question.

Outside your main loop, set up an array of pointers, like this:

char *fldn[3];
fldn[0] = rec.field1;
fldn[1] = rec.field2;
fldn[2] = rec.field3;

You can now use fldn[n], in a loop, to access the char arrays of the rec
object.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 21 '07 #8
On 21 Jun, 11:18, "oli...@gmail.c om" <oli...@gmail.c omwrote:
Hello,

Can anyone tell me how do i fill a structure's fields in a for loop ?

#define MAX_FIELD_LEN 10

typedef struct
{
char field1[MAX_FIELD_LEN]; /*will store first string read from
keyboard*/
char field2[MAX_FIELD_LEN]; /*will store 2nd string read from
keyboard*/
char field3[MAX_FIELD_LEN]; /*will store 3rd string read from
keyboard*/

} RECORD;

i want to read 3 strings from stdin and assign those strings in a for
loop:

RECORD rec, *pRec;
pRec = &rec;

char tmp[3][MAX_FIELD_LEN];

for(i=0;i<3;i++ );
{
gets(tmp[i], MAX_FIELD_LEN +1);

/*Here i want to fill in the fields of 'rec' struct one
field per iteration...How do i do that ?*/
}
You can't. You would need extra initialised variables,
something like this :-

char *dest[] = { rec.field1,
rec.field2,
rec.field3 };

and a statement in the for-loop something like :-

strcpy(dest[i], tmp[i]);

But it's rather difficult to see from your posting
what properties the RECORD structure might have
that the tmp[][] array hasn't. Why did you not
simply declare :-

typedef struct
{
char field[3][MAX_FIELD_LEN];

} RECORD;

then tmp[][] is completely redundant, and your
gets() can fill in a different rec.field[i] string
each time around the for-loop.
--

Jun 21 '07 #9
ol****@gmail.co m wrote:
On Jun 21, 2:46 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>oli...@gmail.c om said:

<snip>
>>Ok, first of all.. i forgot to mention: that _gets is actually a non
portable version of standard C gets() function.
So what? Your code didn't call _gets(). It called gets(). So your
objection is spurious.

My code, CALLED _gets(). The code i entered HERE didnt called
_gets(). Come on, man...it was a typing mistake.
Then what have we learned about posting the exact code you are asking
for opinions on?
Jun 21 '07 #10

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

Similar topics

21
6643
by: simon | last post by:
From my previous post... If I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2; int iSomeNum1; int iSomeNum2;
26
7064
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
0
942
by: Dave McKie | last post by:
Hi all, I am looking for a sample to fill a form from a SQLDataReader or DataSet that loops through the data from the DataSet and populates the form. Something like the following... Do While dbReader.Read() Me.fields(0) = dbReader.fields(0) Loop
30
3295
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None, 'abc', '12345') # demonstrate map's None fill-in feature The motivation is to provide a means for looping over all data elements
4
2007
by: sameerishah | last post by:
Hi, I have a need to read the fields of a structure in a for loop. for eg. the structure looks like: str.a1, str.a2, str.a3 where a1, a2, a3 are of type string How do I do it? Thanks in advance, Sam
4
2210
by: eking | last post by:
//Thanks for any help,thank you!лл¡£ public override void FloodFill(Bitmap bmp, Point pt) { int ctr=timeGetTime(); //Debug.WriteLine("*******Flood Fill******"); //get the color's int value, and convert it from RGBA to BGRA format (as GDI+ uses BGRA)
3
4298
by: bill | last post by:
All, I have the following: ...... unsafe public struct Example { public double we;
1
2186
by: Kev | last post by:
Hello I have a form (RosterForm) based on a table - RosterRange RosterRange has 4 fields: RosterRangeID Autonumber RosterStartDate Date RosterEndDate Date (probably unnecessary) Ward Text I have 2 unbound combo boxes looking up values - cmbDepartment and
4
4909
by: souravmallik | last post by:
I have a structure declared in a header file, and its initialized in the header file too. struct rectype{ char out_storenum; unsigned int out_usrcode; unsigned int out_strngtyp; char out_bit0_ind; char out_bit1_ind; char out_bit2_ind; char out_bit3_ind;
0
8392
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
8305
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
8825
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...
1
8503
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.