JS wrote:[color=blue]
> I can't seem to figure out how to send a string as a paramenter to a
> function.
>
> I have this structure:
>
> struct list {
> char thread[25];
> struct list *previous;
> struct list *next;
> };
>
> struct list *test;
>
>
> When I call this function I would like to give it a string (char array) as
> argument that will be writtin to the thread field in the list struct:
>
> void fill(char[] arg){
>
> test->thread = arg;
>
> }[/color]
You should use function strcpy, or, function strncpy to
be safe. Change the function fill, to one that dynamically
allocates a node and "fills" the string and places the
new node in the list. See function AddToList below.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define THRD_SZ 25
struct list
{
char thread[THRD_SZ+1];
struct list *previous;
struct list *next;
};
struct list *AddToList(struct list **head,const char *thread);
void PrintList(struct list *head);
void FreeList(struct list **head);
int main(void)
{
struct list *test = NULL;
AddToList(&test,"Capitol Hill");
AddToList(&test,"Senator Smith");
AddToList(&test,"Senator With A Long Name That "
"Seems To Never End");
puts("The Threads....");
PrintList(test);
FreeList(&test);
return 0;
}
struct list *AddToList(struct list **head,const char *thread)
{
struct list *tmp;
if((tmp = malloc(sizeof *tmp)) != NULL)
{
strncpy(tmp->thread,thread,THRD_SZ);
tmp->thread[THRD_SZ] = '\0';
tmp->previous = NULL;
tmp->next = *head;
*head = tmp;
}
return tmp;
}
void PrintList(struct list *head)
{
unsigned i;
for(i = 0 ; head; head = head->next,i++)
printf("%4u) %s\n",i+1,head->thread);
return;
}
void FreeList(struct list **head)
{
struct list *tmp;
for( ; *head; *head = tmp)
{
tmp = (*head)->next;
free(*head);
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto:
xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/