STOPIMACODER
STOPIMACODER

Reputation: 822

Reference inside node?

I'm trying to build a BST. My struct;

typedef struct node{
    int charSize;
    char english[30];
    char span[40];
    struct node *left;
    struct node *right;
} BST;

i'm trying to compare two character arrays. In Xcode, it does not give me any warning using the following lines to compare the arrays, but i'm sure its wrong and was wondering why is it not giving me an error.

if (head->english <= head2->english)
    printf("headOne is smaller or equal");
else
    printf("headOne is Bigger");

Upvotes: 1

Views: 55

Answers (2)

arktos
arktos

Reputation: 83

if (head->english <= head2->english) will compare the value of the pointers, meaning the addresses they point to. Not the contents of the memory addresses.

strcmp() will actually compare the individual values of the characters in the string, so it's more useful for sorting.

If you're trying to determine which string is lengthier you should use if (strlen(head->english) <= strlen(head2->english))

Upvotes: 1

tdao
tdao

Reputation: 17713

if (head->english <= head2->english)

why is it not giving me an error.

Because it's legal to compare pointers head->english and head2->english. Remember array name (character array or any other type of array) is pointer to the first element. So here you are actually comparing those two pointers.

Of course that is not what you want, you need strcmp to compare C strings.

Upvotes: 1

Related Questions