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