I have an array of structures:

typedef struct
{
	char name[20];
	int ID;
	int status;
} seat;

Then, basically, I'm trying to print out the list of empty seats in a reservation system.

void listEmpty(seat plane[MAXCAP]) {
	int i;
	for(i = 0; i < MAXCAP; i++) {
		if(plane[i].status == 1) {
			printf("Seat ID %d\n", plane[i].ID);
		}
	}
}
plan[i].status == 1

indicates that the seat is empty. However, when it prints out, it prints out some weird long integers instead of Seat ID 1 to Seat ID 12. May I know where I have gone wrong?

The correct output should be to display Seat ID 1 to Seat ID 12 instead of these weird long integers. Thanks!

Dani AI

Generated

Likely cause: the seat ID field was never reliably set (or it was being clobbered by a string overflow), so printing it produced garbage. 's counter fix prints a stable sequence of empty-seat numbers but does not use the struct's own ID value. is correct that the real fix is to initialize the array (and to avoid overwriting fields when copying names).

A safe pattern: zero the array, assign human-friendly seat numbers, and give every entry a known status. Using named constants for status makes intent clearer.

enum { SEAT_EMPTY = 1, SEAT_OCCUPIED = 0 };

/* zero the whole array, then initialize fields */
memset(plane, 0, sizeof plane);
for (size_t i = 0; i < MAXCAP; ++i) {
    plane[i].ID = (int)(i + 1);   /* seat numbers 1..MAXCAP */
    plane[i].status = SEAT_EMPTY;
    plane[i].name[0] = '\0';
}

When copying user strings into name, do it safely to avoid overwriting ID/status. Prefer snprintf (or a bounded copy that guarantees a terminating NUL) over plain strcpy or unchecked strncpy usage:

/* safe assignment from an external source */
snprintf(plane[i].name, sizeof plane[i].name, "%s", input_name);

Debugging tips: print addresses and contents to detect corruption (printf("seat[%d] @%p name@%p ID=%d\n", i, (void*)&plane[i], (void*)plane[i].name, plane[i].ID);), run under valgrind or address sanitizer to catch overflows, and prefer explicit status constants or an enum so checks like if (plane[i].status == SEAT_EMPTY) are self-documenting.

Ok, managed to solve it. Should have used a counter.

void listEmpty(seat plane[MAXCAP]) {
	int i;
	int count = 0;
	for(i = 0; i < MAXCAP; i++) {
		if(plane[i].status == 1) {
			count++;
			printf("Seat ID %d\n", count);
		}
	}
}

Actually, I was thinking, is there another way to assign values to the

int id

in struct so that I can directly call from it?.. I.e. plane.id and it'll return the corresponding ID.

you can do this if you like to have id similar to the counter

void initializ(seat plane[], int MAXCAP )
static int CurrentID = 1;
for(i = 0; i < MAXCAP; i++) {
    plane[i].ID = CurrentID++;
}
void initializ(seat plane[], int MAXCAP )
    for(i = 0; i < MAXCAP; i++) {
        plane[i].ID = i;
    }
}

both working, but the first one will keep track of the last assigned value, so if you want to keep calling that function then use the fist one

by the way you have to initialize the ID before using the struct so you have a valid ID number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.