Connecting Tech Pros Worldwide Forums | Help | Site Map

Strings as parameters/arguments to a function

JS
Guest
 
Posts: n/a
#1: Nov 14 '05
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

ramakrishnat@gmail.com
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Strings as parameters/arguments to a function


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:[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[/color]
array) as[color=blue]
> 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[/color]

JS
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Strings as parameters/arguments to a function


ramakrishnat@gmail.com wrote:
[color=blue]
> this is how it should be done ....
>
> void fill(char arg[])
> {
> strcpy(test->thread,arg);
> }[/color]


I have also tried to use char *arg and it also works fine...are there any
difference?
lndresnick@gmail.com
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Strings as parameters/arguments to a function


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[/color]
array) as[color=blue]
> 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[/color]

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

Al Bowers
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Strings as parameters/arguments to a function




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/

Lawrence Kirby
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Strings as parameters/arguments to a function


On Thu, 21 Apr 2005 16:30:48 +0200, JS wrote:
[color=blue]
> ramakrishnat@gmail.com wrote:
>[color=green]
>> this is how it should be done ....
>>
>> void fill(char arg[])
>> {
>> strcpy(test->thread,arg);
>> }[/color]
>
>
> I have also tried to use char *arg and it also works fine...are there any
> difference?[/color]

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

Lawrence Kirby
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Strings as parameters/arguments to a function


On Thu, 21 Apr 2005 07:09:32 -0700, ramakrishnat wrote:
[color=blue]
> 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[/color]

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
Artie Gold
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Strings as parameters/arguments to a function


JS wrote:[color=blue]
> ramakrishnat@gmail.com wrote:
>
>[color=green]
>>this is how it should be done ....
>>
>>void fill(char arg[])
>>{
>> strcpy(test->thread,arg);
>>}[/color]
>
>
>
> I have also tried to use char *arg and it also works fine...are there any
> difference?[/color]

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
lndresnick@gmail.com
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Strings as parameters/arguments to a function


Al Bowers wrote:[color=blue]
> JS wrote:[color=green]
> > I can't seem to figure out how to send a string as a paramenter to[/color][/color]
a[color=blue][color=green]
> > 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[/color][/color]
array) as[color=blue][color=green]
> > argument that will be writtin to the thread field in the list[/color][/color]
struct:[color=blue][color=green]
> >
> > void fill(char[] arg){
> >
> > test->thread = arg;
> >
> > }[/color]
>
> You should use function strcpy, or, function strncpy to
> be safe.[/color]

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

Keith Thompson
Guest
 
Posts: n/a
#10: Nov 14 '05

re: Strings as parameters/arguments to a function


ramakrishnat@gmail.com writes:[color=blue]
> 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[/color]

(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) kst-u@mib.org <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.
Keith Thompson
Guest
 
Posts: n/a
#11: Nov 14 '05

re: Strings as parameters/arguments to a function


Lawrence Kirby <lknews@netactive.co.uk> writes:
[...][color=blue]
> 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.[/color]

This was, in my opinion, a bad idea. There's enough confusion between
arrays and pointers; allowing "char arg[]" in a parameter list to mean
"char *arg" only adds to it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
Chris Torek
Guest
 
Posts: n/a
#12: Nov 14 '05

re: Strings as parameters/arguments to a function


In article <ln64yfzx2q.fsf@nuthaus.mib.org>
Keith Thompson <kst-u@mib.org> wrote:[color=blue]
>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.[/color]

Indeed.

I have to wonder whether comp.lang.c++ is plagued with people
posting advice like "don't use new, class, templates, or exceptions
in case you want to compile your C++ code with a C compiler"... :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jack Klein
Guest
 
Posts: n/a
#13: Nov 14 '05

re: Strings as parameters/arguments to a function


On Thu, 21 Apr 2005 20:38:03 GMT, Keith Thompson <kst-u@mib.org> wrote
in comp.lang.c:
[color=blue]
> Lawrence Kirby <lknews@netactive.co.uk> writes:
> [...][color=green]
> > 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.[/color]
>
> This was, in my opinion, a bad idea. There's enough confusion between
> arrays and pointers; allowing "char arg[]" in a parameter list to mean
> "char *arg" only adds to it.[/color]

Indeed it is, but it's 30 years too late to do anything about it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Emmanuel Delahaye
Guest
 
Posts: n/a
#14: Nov 14 '05

re: Strings as parameters/arguments to a function


JS wrote on 21/04/05 :[color=blue]
> struct list {
> char thread[25];
> struct list *previous;
> struct list *next;
> };
>
> void fill(char[] arg){
>
> test->thread = arg;[/color]

I feel rather bizarre that you pretend to manipulate complex concepts
like double linked lists without knowing the vary basics of the
language.

What the hell is your C-book ? Or do you think that beeing an expert in
some other language makes you an expert in C just by magic ?

C is not a kiddy language. It needs to be learnt from scratch, step by
step. It takes time. One of the step is about strings and strings
functions like strcpy(), strncat() etc.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."

Emmanuel Delahaye
Guest
 
Posts: n/a
#15: Nov 14 '05

re: Strings as parameters/arguments to a function


ramakrishnat@gmail.com wrote on 21/04/05 :[color=blue]
> 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[/color]

Nobody here knows what a 'c++ compiler' is, but yes, if you want to
properly use a function, its prototype must be in scope. Including the
dedicated header certainly is the best way of achieving this goal.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Emmanuel Delahaye
Guest
 
Posts: n/a
#16: Nov 14 '05

re: Strings as parameters/arguments to a function


JS wrote on 21/04/05 :[color=blue][color=green]
>> void fill(char arg[])
>> {
>> strcpy(test->thread,arg);
>> }[/color]
>
> I have also tried to use char *arg and it also works fine...are there any
> difference?[/color]

Thre is not difference. *In a parameter context*, 'type *id' and 'type
id[]' are identical.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Emmanuel Delahaye
Guest
 
Posts: n/a
#17: Nov 14 '05

re: Strings as parameters/arguments to a function


Chris Torek wrote on 21/04/05 :[color=blue]
> In article <ln64yfzx2q.fsf@nuthaus.mib.org>
> Keith Thompson <kst-u@mib.org> wrote:[color=green]
>> 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.[/color]
>
> Indeed.
>
> I have to wonder whether comp.lang.c++ is plagued with people
> posting advice like "don't use new, class, templates, or exceptions
> in case you want to compile your C++ code with a C compiler"... :-)[/color]

LOL !

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Emmanuel Delahaye
Guest
 
Posts: n/a
#18: Nov 14 '05

re: Strings as parameters/arguments to a function


lndresnick@gmail.com wrote on 21/04/05 :[color=blue]
> 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);[/color]


Meow ! Why memmove() ? Is there any overlapping here I was not aware of
?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Emmanuel Delahaye
Guest
 
Posts: n/a
#19: Nov 14 '05

re: Strings as parameters/arguments to a function


Al Bowers wrote on 21/04/05 :[color=blue]
> You should use function strcpy, or, function strncpy to
> be safe.[/color]

Hard to be safe with strncpy()... Easier with strncat().

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Emmanuel Delahaye
Guest
 
Posts: n/a
#20: Nov 14 '05

re: Strings as parameters/arguments to a function


lndresnick@gmail.com wrote on 21/04/05 :[color=blue]
> 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).[/color]

Agreed.

Note that memmove() is expansive. Only needed when there is
overlapping.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Emmanuel Delahaye
Guest
 
Posts: n/a
#21: Nov 14 '05

re: Strings as parameters/arguments to a function


Joe Wright wrote on 01/05/05 :[color=blue]
> Emmanuel Delahaye wrote:[color=green]
>> Note that memmove() is expansive. Only needed when there is overlapping.[/color]
>
> Dolly Parton's bodice is expansive. A drink at Crazy Horse is expensive.[/color]

Lol! Well noticed.
[color=blue]
> I love Paris. Tell me again when the French leave town. July? :=)[/color]

The good period is mid-July to mid-August.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Closed Thread