-
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.
-
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.
-
>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
-
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;
}
-
>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.