Strings as parameters/arguments to a function

J

JS

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
 
R

ramakrishnat

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
..
 
J

JS

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?
 
L

lndresnick

JS said:
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
 
A

Al Bowers

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

Artie Gold

JS said:
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
 
L

lndresnick

Al said:
JS said:
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
 
L

Lawrence Kirby

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
 
L

Lawrence Kirby

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
 
K

Keith Thompson

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.
 
K

Keith Thompson

Lawrence Kirby said:
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.

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.
 
C

Chris Torek

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.

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"... :)
 
J

Jack Klein

Lawrence Kirby said:
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.

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.

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

Emmanuel Delahaye

JS wrote on 21/04/05 :
struct list {
char thread[25];
struct list *previous;
struct list *next;
};

void fill(char[] arg){

test->thread = arg;

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."
 
E

Emmanuel Delahaye

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

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?"
 
E

Emmanuel Delahaye

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top