473,387 Members | 1,766 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,387 software developers and data experts.

STRUCTURES accessing using i

structures

typedef struct{
char[80] name;
int age;
float balance;
}account;

account xyx;
accout *ptr;
ptr=&xyz;

is there any way to refer to a member(say age) other than
using say

ptr -> age
or
xyz.age
or
(*ptr).age

?

can i use int i; for ( i=0 ; i< ??;++i)
and then access the members using that ?

thanks
archilles

Nov 14 '05 #1
6 2046
ar****************@hotmail.com wrote:
structures

typedef struct{
char[80] name;
int age;
float balance;
}account;

account xyx;
accout *ptr;
ptr=&xyz;

is there any way to refer to a member(say age) other than
using say

ptr -> age
or
xyz.age
or
(*ptr).age

?

can i use int i; for ( i=0 ; i< ??;++i)
and then access the members using that ?

thanks
archilles


You're asking if you can iterate through the members of a structure.
Ans: not generally.
You could devise a custom way for a particular structure but it would be
very messy.
Why do you want to do that?
You would be better off writing a function to analyze the
contents of 1 structure and call that instead for each structure.
Eric

--
Byte=8 bits, a kilobyte is 1024 bytes
There is no such thing as a kibibit,mebibit etc

Nov 14 '05 #2
Thanks Eric,
I have this a big structure and need to assign initial values to the
member elements and was wondering if there was an easier way to access
it other than by its name

archilles

Nov 14 '05 #3
ar****************@hotmail.com wrote:
structures

typedef struct{
char[80] name;
int age;
float balance;
}account;

account xyx;
accout *ptr;
ptr=&xyz;

is there any way to refer to a member(say age) other than
using say

ptr -> age
or
xyz.age
or
(*ptr).age

?

can i use int i; for ( i=0 ; i< ??;++i)
and then access the members using that ?

thanks
archilles


No, you can't. And you obviously know that. The members of a structure
are not like elements of an array. What do you want to do?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4
In article <11*********************@g14g2000cwa.googlegroups. com>
ar****************@hotmail.com <ar****************@hotmail.com> wrote:

[some small edits, including fixing the "name" field]
typedef struct {
char name[80];
int age;
float balance;
} account;

account xyx, *ptr = &xyz;

is there any way to refer to a member(say age) other than
using ptr->age, xyz.age, or (*ptr).age?
You can compute a pointer to the field:

int *p = &ptr->age;

and use that; but you have to name the structure member.
can i use int i; for ( i=0 ; i< ??;++i)
and then access the members using that ?


No. In particular, what would happen if the index "i" were such
that you were accessing xyz.name or xyz.balance instead? How would
the compiler know that the name should have type "array 80 of char",
or that the balance should have type "float"?

Given either a structure, or a pointer to a structure, naming the
member gives the compiler *two* key clues: the offset of the member
(to compute its address given the address of the entire structure),
and its type. It is possible to use address arithmetic and the
offsetof() macro to compute the first value, and pointer type-casting
to produce the second, so there are tricks that can fake it, but
this is usually the wrong approach.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
ar****************@hotmail.com wrote:
Thanks Eric,
I have this a big structure and need to assign initial values to the
member elements and was wondering if there was an easier way to access
it other than by its name


To declare a struct variable and initialize it at the
same time, you could write

typedef struct {
char name[80]; /* corrected from original */
int age;
float balance;
} account;

account xyz = { "Benny, Jack", 39, 0.75 };

To declare and initialize an array of such structs,

account many[] = {
{ "Comaneci, Nadia", 44, 10.0 },
{ "Porgy", 71, 0.0 },
/* and so forth */
};

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #6


ar****************@hotmail.com wrote:
structures

typedef struct{
char[80] name;
int age;
float balance;
}account;

account xyx;
accout *ptr;
ptr=&xyz;

is there any way to refer to a member(say age) other than
using say

ptr -> age
or
xyz.age
or
(*ptr).age

?

can i use int i; for ( i=0 ; i< ??;++i)
and then access the members using that ?

I find that the best way to manage large structures
is to write functions that manipulate it. Examples would
be functions that assign, functions that print, etc. Once
these functions are written and debugged all you need to
worry with is the provision of the correct arguments when
you call the function.

Example:

#include <stdio.h>
#include <string.h>

typedef struct ACCOUNT
{
char name[64];
char major[32];
char counselor[64];
unsigned age;
float gpa;
float balance;
}ACCOUNT;
/* Prototypes */
ACCOUNT *AssignACCOUNT(ACCOUNT *p,
const char *name,
const char *major,
const char *counselor,
unsigned age,
float gpa,
float balance);
void PrintACCOUNT(ACCOUNT *p);

int main(void)
{
ACCOUNT my;

AssignACCOUNT(&my,"George Washington","Zoology",
"Harry Smith",46u,3.46f, 1201.56f);
PrintACCOUNT(&my);
AssignACCOUNT(&my,"Bill Clinton","Political Science",
"Richard Nixon",47u,3.25f,426.76f);
PrintACCOUNT(&my);
return 0;
}

ACCOUNT *AssignACCOUNT(ACCOUNT *p, const char *name,
const char *major, const char *counselor,
unsigned age,float gpa, float balance)
{
strncpy(p->name,name,sizeof p->name);
p->name[sizeof p->name - 1] = '\0';
strncpy(p->major,major,sizeof p->major);
p->name[sizeof p->major - 1] = '\0';
strncpy(p->counselor,counselor,sizeof p->counselor);
p->name[sizeof p->counselor - 1] = '\0';
p->age = age;
p->gpa = gpa;
p->balance = balance;
return p;
}

void PrintACCOUNT(ACCOUNT *p)
{
printf("Name: %s\n"
"Age: %u\n"
"Balance: %.2f\n"
"Major: %s\n"
"Counselor: %s\n"
"GPA: %.2f\n\n", p->name,p->age,p->balance,
p->major,p->counselor,p->gpa);
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #7

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

Similar topics

13
by: Ian Tuomi | last post by:
Could anyone please tell me what is wrong with this code? Compiler error is: "parse error before '.' token" in lines 13-16. I tried searching the internet but all the reference guides etc. say It's...
14
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: ...
8
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: ...
0
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
2
by: jeff_j_dunlap | last post by:
I recently began using STL containers, mainly to store pointers to structures using a vector: std::vector <dataStruc*vdata; Next, I fill the container within a loop: vdata.push_back(new...
4
by: yugidnu | last post by:
Hi all, I have the following problem. I have to access different types of structures getting passed via a void pointer. I have the code snippet here. ...
13
by: Cell | last post by:
I have structures like this in my program - typedef vector vectorstruct { double x, y,z; } vector; typedef struct verticesstruct { vector v;
32
by: Paulo J. Matos | last post by:
Hi all, I am a Python beginner, reading through 2.6 tutorial. I am wondering where are structures? On the other hand, I think I might have the answer. Since Python focus on having one way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.