I'm doing an exercise about pointers in this C book i'm reading. This code should traverse through a linked list and there's a function that removes an entry from the list.

It works, and I don't know why. The function removeEntry is undeclared int the main() function.

Here's the code:

// remove an entry from an existing linked list

#include <stdio.h>

// linked list structure
struct entry
{
  int value;
  struct entry *next;
};

void removeEntry(struct entry *afterThis)
{
  // is this really the way it should be done?
  struct entry *list_pointer;
  list_pointer = afterThis; 
  list_pointer = list_pointer->next;
  afterThis->next = list_pointer->next;
  // this works though 
}

int main(void)
{
  // declarations

  struct entry n1, n2, n3, e1, start_list;
  struct entry *list_pointer = &start_list;
  struct entry *remove = &n1; //remove entry after n1 (n2)
  
  start_list.value = 0;
  start_list.next = &n1;
  
  n1.value = 10;
  n1.next = &n2;
  
  n2.value = 20;
  n2.next = &n3;
  
  n3.value = 30;
  n3.next = (struct entry *) 0; // maybe just a '0'?
  
  e1.value = 100;
  e1.next = (struct entry *) 0;
  
  removeEntry(remove);
  
  // lets print the results and see what happened
  while ( list_pointer != (struct entry *) 0 )
  {
    printf("value: %i\n", list_pointer->value);
    list_pointer = list_pointer->next;
  }
  
  return 0;
}

Any other comments on the code are also welcomed :)

It works, and I don't know why. The function removeEntry is undeclared int the main() function

The printf function is also undeclared within the main function too, but you don't seem to be worried about that :)

It is not necessary to declare a function inside the main function in order to use it. It is only necessary to declare or define a function in such a way that the compiler sees the declaration or definition before it has to use it.

Edited 5 Years Ago by Moschops: n/a

I did not know that :)

I always declared my functions int the beginning of the main(). I thought it was mandatory because the examples in the book are done this way.

Here I would have declared it

void removeEntry(struct entry *afterThis);

I always got a warning if I had a wrong declaration. Good to know I can omit that now :)

BTW. I tested moving the function after the main() and now it gives a warning. Still works though.

Thanks for your help.

I thought it was mandatory because the examples in the book are done this way.

You should probably get a newer book. In the example from your first post, removeEntry() is declared prior to main(). A function definition also acts as a declaration.

You should probably get a newer book. In the example from your first post, removeEntry() is declared prior to main(). A function definition also acts as a declaration.

Maybe I should, this one is from 2005. It's consistent throughout the book, functions are located before the main(). That's the approach I've adapted as well.

I can think of one good reason for this approach though. You can always see witch functions are used in the program just by looking at the beginning of main(). Could be useful when programs get larger.

this one is from 2005

What book is it?

Could be useful when programs get larger.

When programs get larger, you should be breaking the code down into multiple files and including headers for declarations. The only thing a function declaration inside main() does is reduce visibility of the declaration to that scope. For small programs, there's little reason to do that, which is why prototypes (when used) are usually placed at global scope prior to any function definitions.

The book is "Programming in C - A Complete Introduction to C Programming Language" by Stephen G Kochan. I like the book though, it explains all the concepts in detail and makes some of the harder things easier to understand (like pointers).

Working with multiple files is still a couple chapters away :)

Amazingly enough, that's one of the books that I don't despise. I don't imagine your edition would be drastically worse than the edition I last looked at. ;)

Books are prone to doing this ( putting function definitions before main() ), because it saves them space in every program they print.

I always like main() first, then all functions go in alphabetical order, with function declarations in global space, unless the function is used entirely as a helper for one function only. Then the helper function declaration goes inside it's parent function.

That arrangement seems to cause the killer asteroids to steer clear of Earth, so I'm happy with it. ;)

This question has already been answered. Start a new discussion instead.