7 Replies - 8726 Views - Last Post: 25 February 2011 - 10:54 AM Rate Topic: -----

#1 monicojr84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 11-February 11

Initializing an array of Structs

Posted 25 February 2011 - 09:28 AM

Hi
I am trying to figure out if I can initialize an array of structs in a for loop in C.

I need to initialize the seat number an name to empty.
I need to initialize 100 seats (rows 1-20 and each row has seat A-F)

/****************************************/
typedef struct
{ char seat[3];
  char name[25];
}Reservation;

Reservation x[101];  //I need 100 seats
/****************************************/

When I try to do it in a for loop I keep getting a segmentation fault.

Can this be done in a for loop or do you have to initialize all 100 structs?
Or does anyone have a better way of doing this?

And I will have to access each struct(seat) later to change the name field.

This post has been edited by JackOfAllTrades: 25 February 2011 - 09:29 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Initializing an array of Structs

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,542
  • Joined: 23-August 08

Re: Initializing an array of Structs

Posted 25 February 2011 - 09:29 AM

You need to do it in a loop. Show the code -- IN CODE TAGS! -- and we'll tell you why you're getting the seg fault.

:code:
Was This Post Helpful? 0
  • +
  • -

#3 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Initializing an array of Structs

Posted 25 February 2011 - 09:35 AM

Your indexing is probably wrong. Remember arrays are zero indexed so you should have something similar to this:

Reservation x[100]; // this will will give you 100 elements 0-99 = 100 elements
for (int i=0; i<100; ++i){
    //do your initialization here
    x[i].seat = //whatever
}


This post has been edited by shintetsu_80: 25 February 2011 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 monicojr84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 11-February 11

Re: Initializing an array of Structs

Posted 25 February 2011 - 09:41 AM

Here is the for loop code

typedef struct
{ char name[25];
  char seat[3];
}Reservation;

Reservation x[101];

int i,j;  //For loop counter
char k;  //For loop counter
  
  //Initialize seat numbers
  for(i=0; i<sizeof(x); i++)
    for(j=1; j<=20; j++)
      for(k='a'; k<='f'; k++)
	{ strcpy(x[i].seat, (char*)1);
	  strcat(x[1].seat, (char*)k);
	}
}//End of initialize




This is how I first started, even after I tried to just put numbers in the each struct
I still got a segmentation fault. I figure that I have to use a string to store the seat
number because its alphanumeric.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: Initializing an array of Structs

Posted 25 February 2011 - 09:49 AM

The functions strcpy() and strcat() only work with NULL terminated character arrays ( C-strings). You are not using these null terminated arrays.

Jim

This post has been edited by jimblumberg: 25 February 2011 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,542
  • Joined: 23-August 08

Re: Initializing an array of Structs

Posted 25 February 2011 - 09:57 AM

for(i=0; i<sizeof(x); i++)


sizeof is going to give you the size of all the data in the array, not the size of the array. Use sizeof(x)/sizeof(x[0]) to get the size of the array.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: Initializing an array of Structs

Posted 25 February 2011 - 10:01 AM

But since you are using a constant to determine your array size why not just #define arraysize 101 before main() and then for(i=0; i< arraysize; i++).


Jim
Was This Post Helpful? 1
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5877
  • View blog
  • Posts: 12,757
  • Joined: 16-October 07

Re: Initializing an array of Structs

Posted 25 February 2011 - 10:54 AM

#define RESERVATIONS_MAX 100
typedef Reservation Reservations[RESERVATIONS_MAX];

void initReservations(Reservations);



I'm having a hard time seeing what you want the seats to be. rows 1-20 with seats a-f?

You need one extra space for that. Seat 20f is actually four spaces "20f\0".

Hmm...
typedef struct { char name[25], seat[4]; } Reservation;

// #define RESERVATIONS_MAX 100
// if you really want 20 and a-f, that 120
#define RESERVATIONS_MAX 120

typedef Reservation Reservations[RESERVATIONS_MAX];

void initReservations(Reservations r) {
	int i, j, k;
	i = 0;
	for(j=1; j<=20; j++) {
		for(k='a'; k<='f'; k++) {
			sprintf(r[i].seat, "%02d%c", j, k);
			r[i].name[0] = 0; // empty string
			i++;
		}
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1