-2

Notice anything funny about the code below compared to the output? I have the files saved and compiled without error or warning.

What's funny is it tells me when i=1 that the element and the *(arr->elements + i) are equal (namely STRING3), yet it's not going inside the if statement and printing "Found the element!" for that i value.

Here is my code:

void arr_remove(Array *arr, char *element)
{
  printf("Searching for element %s\n", element);
  for (int i = 0; i < arr->count; i++)
  {
    printf("i is %d and element is %s. Arr->elements is %s \n", i, element, *(arr->elements + i));
    if (element == *(arr->elements + i))
    {
      printf("Found the element %s!!\n", element);
      memmove(*(arr->elements + i), arr->elements + i + 1, sizeof(char *) * (arr->count - (i + 1)));
      return;
    }
  }
  fprintf(stderr, "Fprintf stderr: Array element not found\n");
}

Here is my console output:

Searching for element STRING3
i is 0 and element is STRING3. Arr->elements is STRING2
i is 1 and element is STRING3. Arr->elements is STRING3
i is 2 and element is STRING3. Arr->elements is STRING1
i is 3 and element is STRING3. Arr->elements is STRING4
Fprintf stderr: Array element not found

In other words, if I add this line

printf("Boolean condition %d\n", (element == *(arr->elements + 1)));

The output is

Boolean condition 0

Why is this and what can I do to fix it? Is it line ending (carriage return type stuff? They're both char pointer types. Any help would be appreciated!

gcr
  • 443
  • 2
  • 5
  • 14
  • what is Array ? what are the inputs ? `element == *(arr->elements + i)` compares *pointers*, if you want to compare strings use *strcmp* for instance – bruno Jan 03 '21 at 21:05
  • That's probably it. Let me check – gcr Jan 03 '21 at 21:07
  • Yes that fixed it. Thank you!! I like these kinds of issues, because it gives me more insight into C and into computers themselves! – gcr Jan 03 '21 at 21:09
  • you welcome, note I put an answer – bruno Jan 03 '21 at 21:09
  • 1
    Please write `*(arr->elements + i)` as `arr->elements[i]` instead, the former hurts eyes. – Antti Haapala -- Слава Україні Jan 03 '21 at 21:10
  • Thanks for your feedback Antii. I may do that in the future. As I'm learning C and low level stuff, I like to do everything manually until I really understand what's going on and get good with the logic. It's why I wanted to learn C in the first place, but I hear you. I also figured out that my issues had to do with my lack of understanding of printf of all things, that it took pointers. When you're new and learning how to use printf, that fact is easily overlooked. – gcr Jan 04 '21 at 00:00

1 Answers1

1

having char *element that means element == *(arr->elements + i) compares pointers, you want to compare strings independently of their addresses, use strcmp for instance

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Thanks. I didn't see why my method compared pointers versus strings and was going to ask you about it, but I figured it out and I think I'll share. As my syntax output the strings to the console, I thought it should be the same in the if stmt. Actually, it's because of how printf with %s works. Printf takes pointers, so you're one level too high. I reasoned that if I added another dereferencer * to each side it would work without strcmp and in fact it did! I knew I shouldn't have to be dependent on a function, even if they're good to use for other reasons like readability. – gcr Jan 03 '21 at 23:53