0

I have my own data type since C89 doesn't allow bool type. I can't use C99 sadly.

I have tested the below code and my program is not even recognizing that the names are different. The names are working just fine. I have those loaded in an instance of the struct.

struct ROOM roomList [MAX_ROOMS_COUNT]; 

I used memset and strcpy to copy in a list of rooms from a room array. Testing this gives me the results I want like so...

printf("%s", roomList[1].name) printf("%s", roomList[2].name) ... printf("%s", roomList[7].name) 

The rooms array has 7 values.

#define MAX_ROOMS_COUNT 7 typedef enum { true = 1, false = 0 } boolean; struct ROOM { char name[8]; char* type; int numConnections; char* connections [MAX_CONNECTIONS_COUNT]; }; boolean isSameRoom(struct ROOM x, struct ROOM y) { //printf("Is Same Room, 1:%s, 2:%s\n", x.name, y.name); if (x.name == y.name) { //printf("ROOM IS SAME!"); return true; } else { return false; } } struct ROOM getRandomRoom() { int random = rand() % MAX_ROOMS_COUNT; return roomList[random]; } void addRandomConnection() { struct ROOM A; struct ROOM B; A = getRandomRoom(); do { B = getRandomRoom(); }while(isSameRoom(A, B) == true); //printf("Add Random Connection, 1:%s, 2:%s\n", A.name, B.name); //for testing purposes } 

Using the print statements I can see that the isSameRoom function isn't working. It's generating random rooms just fine, but there are instances when I run the program that I'm getting the same room for A and B. Any idea why? Thanks! :)

3
  • 4
    You cannot compare strings with ==, you have to use strcmp Commented Oct 27, 2019 at 19:04
  • Thanks! I wondered if that was what it was. Commented Oct 27, 2019 at 19:06
  • Aside: you say you output the room names with roomList[1].name thru roomList[7].name for #define MAX_ROOMS_COUNT 7 rooms. In C arrays are indexed from 0 so that should be roomList[0].name thru roomList[6].name. Commented Oct 27, 2019 at 20:00

1 Answer 1

1

First of all, you're using pass-by-value for these structures, which is pretty inefficient. You should do pass by reference.

Second, as others mentioned, you cannot compare fixed-size strings with == in C; you have to use strncmp or some other structure.

Perhaps a better idea would be to do the whole comparison of the whole structure with memcmp?

return memcmp(&A, &B, sizeof(struct ROOM)) == 0; 
Sign up to request clarification or add additional context in comments.

2 Comments

If there is padding between struct members, is memcmp() uncertain? For an initialised struct I guess no because uninitialised parts are zeroed, but for a struct where members' values are individually assigned? For an unpadded struct, in the case where the only difference might be a string with unused space in the array, could they compare different when they are actually the same? Moreover, if any member is a pointer to a data value, the data might be the same, but the pointer different. I propose that the only safe way is to compare each member, according to its type.
@WeatherVane, Obviously, this would only work for a shallow comparions (re: pointer), but good point about the string. And I see that you already mentioned that. woops! Comment deleted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.