STRUCTURES accessing using i

A

archilleswaterland

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
 
E

Eric

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
 
A

archilleswaterland

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
 
C

Chris Torek

[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.
 
J

Joe Wright

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?
 
E

Eric Sosman

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 */
};
 
A

Al Bowers

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top