Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strings as parameters/arguments to a function

Reply
Thread Tools

Strings as parameters/arguments to a function

 
 
JS
Guest
Posts: n/a
 
      04-21-2005
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;

}

But it seems that its not the way to do it, hope someone can help.

JS
 
Reply With Quote
 
 
 
 
ramakrishnat@gmail.com
Guest
Posts: n/a
 
      04-21-2005
this is how it should be done ....

void fill(char arg[])
{
strcpy(test->thread,arg);
}

u may have to do - #include <string.h> if you are using a c++ compiler
..


JS wrote:
> 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;
>
> }
>
> But it seems that its not the way to do it, hope someone can help.
>
> JS


 
Reply With Quote
 
 
 
 
JS
Guest
Posts: n/a
 
      04-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> this is how it should be done ....
>
> void fill(char arg[])
> {
> strcpy(test->thread,arg);
> }



I have also tried to use char *arg and it also works fine...are there any
difference?
 
Reply With Quote
 
lndresnick@gmail.com
Guest
Posts: n/a
 
      04-21-2005
JS wrote:
> 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;
>
> }
>
> But it seems that its not the way to do it, hope someone can help.
>
> JS


See http://www.eskimo.com/~scs/C-faq/q8.3.html

You probably want something like this (not compiled or tested code)

void fill(const char *arg)
{
/* if arg is NULL, deal somehow */
size_t len = strlen(arg);
if (len < sizeof(test->thread)) {
memmove(test->thread, arg, len+1);
}
else {
/* doesn't fit, deal */
}
}

I'm assuming you have allocated the list, as in
test = malloc(sizeof *test)?

-David

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      04-21-2005


JS wrote:
> 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;
>
> }


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: (E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      04-21-2005
JS wrote:
> (E-Mail Removed) wrote:
>
>
>>this is how it should be done ....
>>
>>void fill(char arg[])
>>{
>> strcpy(test->thread,arg);
>>}

>
>
>
> I have also tried to use char *arg and it also works fine...are there any
> difference?


There is no difference, but only in the context of a parameter
declaration, i.e.:

void f(char *stuff);

and

void f(char stuff[]);

are semantically indistinguishable.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
 
Reply With Quote
 
lndresnick@gmail.com
Guest
Posts: n/a
 
      04-21-2005
Al Bowers wrote:
> JS wrote:
> > 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;
> >
> > }

>
> You should use function strcpy, or, function strncpy to
> be safe.


YMMV, but I think strncpy is a basically useless function.
e.g. see http://www.eskimo.com/~scs/C-faq/q13.2.html

Which doesn't even mention the irritiating side effect of setting the
remainder of the string to the null character, a possible performance
problem if the target is much bigger than the source.

Using a combination of strlen and memcpy/memmove seems like more the
way to go when you aren't sure source fits into target (or strncat).

-David

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      04-21-2005
On Thu, 21 Apr 2005 16:30:48 +0200, JS wrote:

> (E-Mail Removed) wrote:
>
>> this is how it should be done ....
>>
>> void fill(char arg[])
>> {
>> strcpy(test->thread,arg);
>> }

>
>
> I have also tried to use char *arg and it also works fine...are there any
> difference?


void fill(char arg[]) is interpreted by the compiler sa if you had written
void fill(char *arg). Remember that you can't pass arrays to functions in
C. If you try to pass an array in the caller the normal conversion to a
pointer to the array's first element in performed, so a pointer gets
passed. The designers of C decided it was a good idea to mirror this
within the funciton parameter list.

Lawrence

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      04-21-2005
On Thu, 21 Apr 2005 07:09:32 -0700, ramakrishnat wrote:

> this is how it should be done ....
>
> void fill(char arg[])
> {
> strcpy(test->thread,arg);
> }
>
> u may have to do - #include <string.h> if you are using a c++ compiler


If you are compiling C code you should be using a C compiler. But note
that you should #include <string.h> if you use strcpy() in C too. SOme
compilers may not complain if you don't but that doesn't mean the code is
correct.

Lawrence
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-21-2005
(E-Mail Removed) writes:
> this is how it should be done ....
>
> void fill(char arg[])
> {
> strcpy(test->thread,arg);
> }
>
> u may have to do - #include <string.h> if you are using a c++ compiler


(Please don't top-post. Your reply belongs after, or interspersed
with, any quoted text, which should be trimmed to what's necessary to
provide enough context.)

(Please don't use abbreviations like 'u'; take the time to spell out
the word.)

You should have a "#include <string.h>" regardless of what compiler
you're using. A C90 compiler may not diagnose the error, but it's
still an error.

If you're using a C++ compiler, you should be programming in C++ and
posting to comp.lang.c++. C is compiled with C compilers.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments