functions??

This is a discussion on functions?? within the C Programming forums, part of the General Programming Boards category; Hi? is anyone good at functions in C programming?? i need to implement a function say mystring() that will return ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    functions??

    Hi? is anyone good at functions in C programming??

    i need to implement a function say mystring() that will return a pointer to the next string in storage (starting with the first); returning a NULL pointer at the end of the list.

    How would i implement this? can anyone help me here please..

    cheers
    Atif.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    You could try using linked lists instead of a function
    Something like this:


    char str[20];
    struct string
    {
    char colour[20];
    struct string *next;
    }start, *node;

    start.next = NULL;
    node = &start;

    for (i = 0; i < number; i++) // how many strings
    {
    scanf("%s" , str);
    node->next = (struct string *) malloc(sizeof (struct string));
    node = node->next;
    node->colour = str;
    node->next = NULL;
    }

    Don't know how well it will work for you but you can try it.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How would i implement this?
    Since it sounds like a helper function in a linked list, try this:
    Code:
    struct node * mystring ( struct node *current )
    {
      return current->next;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    functions in C?

    Hi ? does anyone know how to convert this linked list code into a function..say i need to implement a function dumpstring(),each call to dumpstring() will return a pointer to the next string in storage (starting with the first); returning a NULL pointer at the end of the list.

    cheers

    Atif.

    here is the linked list..

    char str[20];
    struct string
    {
    char colour[20];
    struct string *next;
    }start, *node;

    start.next = NULL;
    node = &start;

    for (i = 0; i < number; i++) // how many strings
    {
    scanf("%s" , str);
    node->next = (struct string *) malloc(sizeof (struct string));
    node = node->next;
    node->colour = str;
    node->next = NULL;
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Hi? is anyone good at functions in C programming??

    No, C programmers usually don't use functions. :-)

    >say i need to implement a function dumpstring(),each call to
    >dumpstring() will return a pointer to the next string in storage
    >(starting with the first)

    I recommend Prelude's option. More specific to Drako's code it would look like:

    Code:
    struct string *return_ptr_to_next_str (struct string *list_ptr)
    {
        return list_ptr->next;
    }
    Note that automatically NULL is returned at the end of the list. As you can see from Drako's code, each time a new node is created, the next-pointer is NULL. So for the last node, the next-pointer is NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21