Hi,
I have had a little programming experience with C but not enough to
accomplish what I am trying to do. Nor have I been able to find any
references on the internet regarding what I am trying to do.
Basically, I have a structure, which I am unable to change, inside
this structure are fields of an integer type that have the names: F0,
F1, F2, F3, F4, F5.
All I want to be able to to is iterate through them is something along
the lines of this pseudocode;
for(N, N == 5, N++) {
STRUCTURE.F{N} = SOME_VAL;
}
I think I may need to utilize an array or hash but I'm really not
sure.
Basically I want to append N to the variable name. Is this possible,
is there some other way of doing it?
Thanks,
Alex 7 5527
On 4 Jun 2008 at 11:34, di************@ gmail.com wrote:
Basically, I have a structure, which I am unable to change, inside
this structure are fields of an integer type that have the names: F0,
F1, F2, F3, F4, F5.
All I want to be able to to is iterate through them is something along
the lines of this pseudocode;
for(N, N == 5, N++) {
STRUCTURE.F{N} = SOME_VAL;
}
If F0,...,F5 are successive fields in the struct, then it's very likely
that you can do something like
int *ip = &structure.F 0;
for(i=0; i<=5; i++, ip++)
/* do stuff */
Otherwise, you could use offsetof to build a table of the offsets of the
different fields, and then do something very much like indirect
addressing.
In article <ab************ *************** *******@e53g200 0hsa.googlegrou ps.com>,
<di************ @gmail.comwrote :
>Basically, I have a structure, which I am unable to change, inside this structure are fields of an integer type that have the names: F0, F1, F2, F3, F4, F5.
All I want to be able to to is iterate through them is something along the lines of this pseudocode;
for(N, N == 5, N++) {
STRUCTURE.F{N} = SOME_VAL; }
You can build an array containing the offsets of the fields,
and then do a little pointer arithmetic:
#include <stdio.h>
#include <stddef.h>
struct foo {
int f0, f1, f2, f3, f4, f5;
};
size_t foo_f_offset[6] = {
offsetof(struct foo, f0),
offsetof(struct foo, f1),
offsetof(struct foo, f2),
offsetof(struct foo, f3),
offsetof(struct foo, f4),
offsetof(struct foo, f5)
};
int main(void)
{
int i;
struct foo myfoo;
for(i=0; i<=5; i++)
*(int *)((char *)&myfoo + foo_f_offset[i]) = i*i;
printf("myfoo.f 4 = %d\n", myfoo.f4);
return 0;
}
-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
On Jun 4, 7:56 am, Antoninus Twink <nos...@nospam. invalidwrote:
On 4 Jun 2008 at 11:34, digital.par...@ gmail.com wrote:
Basically, I have a structure, which I am unable to change, inside
this structure are fields of an integer type that have the names: F0,
F1, F2, F3, F4, F5.
All I want to be able to to is iterate through them is something along
the lines of this pseudocode;
for(N, N == 5, N++) {
STRUCTURE.F{N} = SOME_VAL;
}
If F0,...,F5 are successive fields in the struct, then it's very likely
that you can do something like
int *ip = &structure.F 0;
for(i=0; i<=5; i++, ip++)
/* do stuff */
This is not very good code IMHO. Not written with an eye to being
maintainable, hopefully wouldn't pass a code review. Forgetting the
padding issue (seems unlikely to me that padding will between ints, I
assume you mean that by your "likely" comment, though an assert could
be added to test this), lying to the compiler means that it won't
notice changes in the struct. This code will continue to compile just
fine as long as something named F0 remains if you, for example
eliminate F5
insert something between F2 and F3
reorder the fields, putting F0 last
etc
Resulting in some irritating memory overwrites or uninitialized memory
that could be a bit of a pain to debug. Yeah, maybe nobody will ever
reorder or otherwise munge this struct, but too many such assumptions
that nobody will ever do a variety of unlikely things means one will
happen somewhere anyway...
>
Otherwise, you could use offsetof to build a table of the offsets of the
different fields, and then do something very much like indirect
addressing.
Seems a more maintainable choice to me assuming it needs doing at
all. I assume that the OP actually has a good reason for this, as
opposed to the simpler and very clear:
STRUCTURE.F0 = SOME_VAL;
STRUCTURE.F1 = SOME_VAL;
....
STRUCTURE.F5 = SOME_VAL;
-David
On Jun 4, 12:56*pm, Antoninus Twink <nos...@nospam. invalidwrote:
On *4 Jun 2008 at 11:34, digital.par...@ gmail.com wrote:
Basically, I have a structure, which I am unable to change, inside
this structure are fields of an integer type that have the names: F0,
F1, F2, F3, F4, F5.
All I want to be able to to is iterate through them is something along
the lines of this pseudocode;
for(N, N == 5, N++) {
* * STRUCTURE.F{N} = SOME_VAL;
}
If F0,...,F5 are successive fields in the struct, then it's very likely
that you can do something like
int *ip = &structure.F 0;
for(i=0; i<=5; i++, ip++)
* /* do stuff */
Otherwise, you could use offsetof to build a table of the offsets of the
different fields, and then do something very much like indirect
addressing.
Hi,
Thanks for your help.
Alex.
Hi,
Right, basically I think I've been really stupid in not reading the
documentation (what there is of it) properly and probably wasted your
time (although your code has gone into my code library because I can
see myself using it somewhere).
I think I need a better explanation of the problem as I am now
struggling to understand it myself.
I am actually programming a PIC chip using C, P16F627 to be precise
and I have plenty of information and have successfully programmed it
in assembly but I want to make my programming a little easier and I am
trying to program it in C, the documentation for C however is
virtually non existent and I had to work out some things by 'reverse
engineering' the assembly into C.
I think, what I originally stated as a struct is actually a bit field.
I will explain how it works however and allow you to make up your
mind.
PORTB <- what I originally thought was a structure, allows me to use
the output pins of the chip of which there are 6. In assembly the
assignments to turn the outputs on and off in sequence are as follows;
PORTB B'00000001'
PORTB B'00000010'
PORTB B'00000100'
PORTB B'00001000'
PORTB B'00010000'
PORTB B'00100000'
I can do this in C by performing the following;
PORTB = 1
PORTB = 2
PORTB = 4
PORTB = 8
PORTB = 16
PORTB = 32
If I assign products of this then I can get multiple outputs to turn
on;
PORTB = 63 // Turns all outputs on.
I mechanism is also provided to access and set each individual output;
PORTB.F0 = 1 // Turn output 1 on.
PORTB.F0 = 1 // Turn output 1 off.
PORTB.F1 = 1 // Turn output 2 on.
PORTB.F1 = 1 // Turn output 2 off.
This means that I can use;
PORTB.F0 = ~PORTB.F0
To toggle the output on or off.
Essentially what I am trying to do as part of my first very basic
program is turn the outputs on in sequence, then off in sequence. I
can do this;
void main() {
while(1) {
PORTB.F0 = ~PORTB.F0;
Delay_ms(100);
PORTB.F1 = ~PORTB.F1;
Delay_ms(100);
PORTB.F2 = ~PORTB.F2;
Delay_ms(100);
PORTB.F3 = ~PORTB.F3;
Delay_ms(100);
PORTB.F4 = ~PORTB.F4;
Delay_ms(100);
PORTB.F5 = ~PORTB.F5;
Delay_ms(100);
}
}
I suppose it's a re-factoring issue more than anything but I can see a
lot of code repetition above that I would prefer to shorten as per my
original post.
Obviously, the number of output pins doesn't change and I would prefer
to use the PORTB.Fx method rather than work out all the maths to have
to use the PORTB = x method of setting to outputs.
Hope I haven't annoyed people to much with my initial stupidity. If
people think it would be more appropriate I will move this topic to a
PIC mailing list.
Thanks & apologies,
Alex
<di************ @gmail.comwrote in message
news:80******** *************** ***********@y38 g2000hsy.google groups.com...
I am actually programming a PIC chip using C, P16F627 to be precise
I think, what I originally stated as a struct is actually a bit field.
I will explain how it works however and allow you to make up your
mind.
PORTB = 63 // Turns all outputs on.
Essentially what I am trying to do as part of my first very basic
program is turn the outputs on in sequence, then off in sequence. I
can do this;
void main() {
while(1) {
PORTB.F0 = ~PORTB.F0;
Delay_ms(100);
PORTB.F1 = ~PORTB.F1;
Delay_ms(100);
PORTB.F2 = ~PORTB.F2;
Delay_ms(100);
PORTB.F3 = ~PORTB.F3;
Delay_ms(100);
PORTB.F4 = ~PORTB.F4;
Delay_ms(100);
PORTB.F5 = ~PORTB.F5;
Delay_ms(100);
}
}
I suppose it's a re-factoring issue more than anything but I can see a
lot of code repetition above that I would prefer to shorten as per my
original post.
Obviously, the number of output pins doesn't change and I would prefer
to use the PORTB.Fx method rather than work out all the maths to have
to use the PORTB = x method of setting to outputs.
There's little maths involved. To turn on the first bit write 1. To turn on
the next bit, write 3:
bits=1;
for (i=0; i<6; ++i) {
PORTB = bits;
bits = (bits<<1) |1;
delay...
}
bits=63;
for (i=0; i<6; ++i) {
PORTB = bits;
bits = (bits<<1); /* propagate 0 into lower bits */
delay...
}
I haven't tested this but I think it's on the right lines. If you really
want to use F0 to F5, you can write a couple of functions getbit(n) and
setbit(n,x), each of which uses a switch statement to access F0 to F5, and
use those instead, if you don't like bit-fiddling.
--
Bartc
On Jun 4, 3:23*pm, "Bartc" <b...@freeuk.co mwrote:
<digital.par... @gmail.comwrote in message
news:80******** *************** ***********@y38 g2000hsy.google groups.com...
I am actually programming a PIC chip using C, P16F627 to be precise
I think, what I originally stated as a struct is actually a bit field.
I will explain how it works however and allow you to make up your
mind.
PORTB = 63 // Turns all outputs on.
Essentially what I am trying to do as part of my first very basic
program is turn the outputs on in sequence, then off in sequence. I
can do this;
void main() {
*while(1) {
* *PORTB.F0 = ~PORTB.F0;
* *Delay_ms(100);
* *PORTB.F1 = ~PORTB.F1;
* *Delay_ms(100);
* *PORTB.F2 = ~PORTB.F2;
* *Delay_ms(100);
* *PORTB.F3 = ~PORTB.F3;
* *Delay_ms(100);
* *PORTB.F4 = ~PORTB.F4;
* *Delay_ms(100);
* *PORTB.F5 = ~PORTB.F5;
* *Delay_ms(100);
*}
}
I suppose it's a re-factoring issue more than anything but I can see a
lot of code repetition above that I would prefer to shorten as per my
original post.
Obviously, the number of output pins doesn't change and I would prefer
to use the PORTB.Fx method rather than work out all the maths to have
to use the PORTB = x method of setting to outputs.
There's little maths involved. To turn on the first bit write 1. To turn on
the next bit, write 3:
bits=1;
for (i=0; i<6; ++i) {
*PORTB = bits;
*bits = (bits<<1) |1;
*delay...
*}
bits=63;
for (i=0; i<6; ++i) {
*PORTB = bits;
*bits = (bits<<1); /* propagate 0 into lower bits */
*delay...
*}
I haven't tested this but I think it's on the right lines. If you really
want to use F0 to F5, you can write a couple of functions getbit(n) and
setbit(n,x), each of which uses a switch statement to access F0 to F5, and
use those instead, if you don't like bit-fiddling.
--
Bartc
The code works perfectly, just reading through my C book to better
understand what it does then I'll see if I can write some code that
produces another sequence.
Thanks,
Alex. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paxton |
last post by:
At the risk of being told "If it ain't broke, don't fix it", my code works,
but is ugly.
Part of the admin site I'm working on at the moment includes a facility for
users to enter Formulations (recipes for making cosmetics etc) in 3 stages:
Stage 1: basic info such as title, method etc and number of Phases (steps in
recipe).
Stage 2: dynamically generated form containing the exact number of phases as
textboxes, depending on the number...
|
by: Christopher Benson-Manica |
last post by:
If an array is sparse, say something like
var foo=;
foo=4;
foo='baz';
foo='moo';
is there a way to iterate through the entire array?
--
|
by: ben |
last post by:
hello,
an algorithm book i'm reading talks about the connectivity
problem/algorithm.
it gives a number of examples where the connectivity problem applies to
real life situations (like, the objects may represent computers in a
large network and the pairs represent connections between them)
another real situation example that's given is:
|
by: pbali |
last post by:
Hi,
I am using PHP 5.1 and MySQL. I have a result set obtained by executing
PDO:: query. I want to create an XML file by using this result set. The
XML file will contain column names as XML node name and column values
as node values.
$orders = $db->query($sql);
if (!empty($orders)) {
|
by: News |
last post by:
Hi everyone,
My goal is to pull command switches/options from a file and then assign
the values to select variables which would eventually be included in a
class object.
The data file looks something like this but the switches could be in any
order and not all may be used.
-m quemanager -s server -p port -k key -o object -c 20 -t test@email.com
| |
by: John |
last post by:
Does the length of my C variable names have any affect, performance-wise, on
my final executable program? I mean, once compiled, etc., is there any
difference between these two:
number = 3;
n = 3;
I know its setting aside storage for the variable itself; does it also use
up more storage if the variable name is longer? I realize it would probably
take quite a lot of long variable names to make any impact if so, but I was
just curious...
|
by: Ispep |
last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without saying I'm not asking you to solve the question - that won't help come exam time :().
Anyway I have a CSV delimited file in the following format;
STRING,INT
STRING,INT,INT,INT,INT,INT
STRING,INT,INT,INT,INT,INT
STRING,INT,INT,INT,INT,INT
...
|
by: Tom |
last post by:
I have a textbox on my web form were users can enter in 1 name or many names. The form then validates the name against the database.
If the name is valid its then saved into a varaible to be used at a later time. The issue I'm having is as follows:
the user enters in 2 names
John, Greg
clicks my valid button they're both valid so both names are stored in the variable so my variable looks like
John,Greg and both are in the textbox.
...
|
by: RMWChaos |
last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?"
Alright, here's the story: with a great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes...
|
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,...
|
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...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |