Forumchem - Forum with AI(ALICE BOT & HAL9000) and TTS

More dificult for us, more easy for you
It is currently Fri Apr 19, 2024 2:50 am

All times are UTC





Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 
Author Message
 Post subject: C programming arrays of structures?
PostPosted: Sun Dec 23, 2012 9:15 pm 
Offline
User avatar

Joined: Wed Oct 14, 2009 4:13 pm
Posts: 11
Hello. I just started looking into arrays of structures. I wrote out my code the long way for assigning values to three structs. I have a feeling the code can be reduced down to just two function, assign and print for all three structs, am I right? How does pointer arithmetic work with structs? I recently learned that if we have a pointer that points to an array of integers, it will point to the first element and ptr+1 will point to the second element and so on(even though I detain thinking that it should move by one byte not one element, Ill just have to get use to it). Now how does it work with structs? We point to a struct and then how do we move to the next struct? I have a feeling there is going to be sizeof(). I havent started double pointers yet so if their use is necessary please explain a bit.

Here is my code:

#include
#include
#include

typedef struct structureName

char firstName[20];
int age;
structureName;

void obtainStructureZero(structureName *ptr);
void printStructureZero(structureName *ptr);
void obtainStructureOne(structureName *ptr);
void printStructureOne(structureName *ptr);
void obtainStructureTwo(structureName *ptr);
void printStructureTwo(structureName *ptr);

void obtainStructureZero(structureName *ptr)

printf("Enter the designate:\\n");
scanf("%s",ptr->firstName);

printf("Enter the age:\\n");
scanf("%d",&(ptr->age));


void printStructureZero(structureName *ptr)

printf("The first designate is %s\\n", ptr->firstName);
printf("Age is %d\\n", ptr->age);



void obtainStructureOne(structureName *ptr)

printf("Enter the designate:\\n");
scanf("%s",ptr->firstName);

printf("Enter the age:\\n");
scanf("%d",&(ptr->age));


void printStructureOne(structureName *ptr)

printf("The first designate is %s\\n", ptr->firstName);
printf("Age is %d\\n", ptr->age);



void obtainStructureTwo(structureName *ptr)

printf("Enter the designate:\\n");
scanf("%s",ptr->firstName);

printf("Enter the age:\\n");
scanf("%d",&(ptr->age));


void printStructureTwo(structureName *ptr)

printf("The first designate is %s\\n", ptr->firstName);
printf("Age is %d\\n", ptr->age);



int main()


structureName structures[3];

obtainStructureZero(&structures[0]);
printStructureZero(&structures[0]);

obtainStructureOne(&structures[1]);
printStructureOne(&structures[1]);

obtainStructureTwo(&structures[2]);
printStructureTwo(&structures[2]);

return 0;


Please designate the function that obtains names and ages obtainStructures() and for printing printStructures().
I meant *(ptr+1) to move to next element.


Top
 Profile      
 
 Post subject: C programming arrays of structures?
PostPosted: Sat Aug 23, 2014 8:17 am 
Offline
User avatar

Joined: Thu Apr 02, 2009 9:59 am
Posts: 1446
it i similar for the struct.
struct bibi *p;
p = 0x1000;
p++;
is the same as

struct bibi *p;
p = 0x1000 + sizeof(struct bibi);


Top
 Profile      
 
 Post subject: C programming arrays of structures?
PostPosted: Tue Sep 02, 2014 11:06 pm 
Offline
User avatar

Joined: Thu Apr 02, 2009 6:54 am
Posts: 1379
Your functions dont change at all. You can make them into simply two functions (obtainStructure and printStructure). Here i a demo -- http://ideone.com/JFaWki#view_edit_box


Top
 Profile      
 
 Post subject: C programming arrays of structures?
PostPosted: Wed Sep 10, 2014 2:58 pm 
Offline
User avatar

Joined: Fri Apr 03, 2009 2:28 am
Posts: 1413
You are getting way too much attention, Bill!!

Anyway, since you are also learning malloc I decided to provide two approaches for you to consider:

#include
#include
#include

typedef struct structureName
char firstName[20];
int age;
structureName;

int obtainStructure( structureName * ptr )
if ( ptr == NULL ) return 0;
printf( "Enter the first name:\\n" );
if ( fgets( ptr->firstName, sizeof( ptr->firstName ), stdin ) != NULL )
char buf[10];
size_t s= strlen( ptr->firstName );
if ( s > 0 && ptr->firstName[s-1] == \\n )
ptr->firstName[s-1]= \\0;
printf( "Enter the age:\\n" );
if ( fgets( buf, sizeof( buf ), stdin ) != NULL )
ptr->age= (int) strtol( buf, NULL, 10 );
return 1;


return 0;


structureName * createStructure( void )
structureName * result;
result= (structureName *) malloc( sizeof( structureName ) );
if ( result != NULL )
if ( obtainStructure( result ) ) return result;
free( result );

return NULL;


void printStructure( const structureName *ptr )
if ( ptr == NULL ) return;
printf( "The first name is %s\\n", ptr->firstName );
printf( "Age is %d\\n", ptr->age );
return;


int main( void )
int i;
structureName structures1[3], * structures2[3], * next;
printf( "Using method 1:\\n" );
obtainStructure( & structures1[0] );
obtainStructure( & structures1[1] );
obtainStructure( & structures1[2] );
for ( next= & structures1[i= 0]; i < sizeof(structures1) / sizeof(structures1[0]); ++i )
printStructure( next++ );
printf( "\\nUsing method 2:\\n" );
structures2[0]= createStructure();
structures2[1]= createStructure();
structures2[2]= createStructure();
for ( i= 0; i < sizeof(structures2) / sizeof(structures2[0]); ++i )
printStructure( structures2[i] );
free( structures2[i] );

return 0;


Top
 Profile      
 
 Post subject: C programming arrays of structures?
PostPosted: Fri Sep 12, 2014 6:21 am 
Offline
User avatar

Joined: Thu Apr 02, 2009 11:09 pm
Posts: 1379
Yes, it points to the next array item regardless of type. It is the way pointer arithmetic works, unless the example shown which is showing the position of memory. First ptr++ is equal to (ptr+1), so it points to next item.


Thanks!


Top
 Profile      
 
 Post subject: C programming arrays of structures?
PostPosted: Fri Sep 12, 2014 9:05 am 
Offline
User avatar

Joined: Thu Apr 02, 2009 2:45 pm
Posts: 1434
Pointer arithmetic works with structure, and you should definitely be using iteration rather than creating separate functions a youve done. You should soon find that scanf i not very good for getting and validating user input. Also, you should be starting to add some input validation so that bogu user input cannot make your programs misbehave. Ive seen in your recent question that youre taking advice, learning how things work, and progressing nicely in your C ability. Keep it up. Here are some suggestion to make your program better:

#include
#include
#include

#define MAX_STR_LEN 32
#define NUM_PEOPLE 2

typedef struct
char firstName[MAX_STR_LEN];
int age;
person_t;

void getPersonInfo(person_t *);
void displayPersonInfo(const person_t *);
void displayAllPeople(const person_t *, size_t);

void getPersonInfo(person_t *ptr)
char in[MAX_STR_LEN], *p;

do
printf("Enter the name: ");
fget(ptr->firstName, MAX_STR_LEN, stdin);
while (strlen(ptr->firstName) <= 1);
if ((p = strchr(ptr->firstName,\\n)) != NULL) *p = \\0;
do
printf("Enter the age: ");
fgets(in, MAX_STR_LEN, stdin);
while (sscanf(in, "%d",&(ptr->age)) != 1);
puts("");


void displayPersonInfo(const person_t *ptr)
printf("The first name is %s\\n", ptr->firstName);
printf("Age is %d\\n\\n", ptr->age);


void displayAllPeople(const person_t *p, size_t n)
size_t i = 0;

while (i++ < n)
displayPersonInfo(p++);



int main(int argc, char *argv[])
person_t people[NUM_PEOPLE], *p = people;
size_t i;

for (i = 0; i < NUM_PEOPLE; i++)
getPersonInfo(p++);

displayAllPeople(people, NUM_PEOPLE);
return 0;


#if 0

Sample run:

Enter the name: Mary
Enter the age: 16

Enter the name: Joe
Enter the age: 20

The first name i Mary
Age i 16

The first name is Joe
Age is 20

#endif



Top
 Profile      
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: etouboudedu and 6 guests


 
Search for:
 
Jump to:  

cron
Click me:
Powered by phpBB © 2000, 2002, 2005, 2007, 2008, 2009 phpBB Group
Chronicles phpBB3 theme by Jakob Persson. Stone textures by Patty Herford.
With special thanks to RuneVillage

This site have 4 type of tecnology in order to convert text to speech. By default you use the vozme tecnology. In order to know the other you need to sign for.


- Privacy Policy -