Strings as parameters/arguments to a function

Discussion in 'C Programming' started by JS, Apr 21, 2005.

  1. JS

    JS Guest

    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
     
    JS, Apr 21, 2005
    #1
    1. Advertising

  2. JS

    Guest

    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
     
    , Apr 21, 2005
    #2
    1. Advertising

  3. JS

    JS Guest

    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?
     
    JS, Apr 21, 2005
    #3
  4. JS

    Guest

    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
     
    , Apr 21, 2005
    #4
  5. JS

    Al Bowers Guest

    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: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Apr 21, 2005
    #5
  6. JS

    Artie Gold Guest

    JS wrote:
    > 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
     
    Artie Gold, Apr 21, 2005
    #6
  7. JS

    Guest

    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
     
    , Apr 21, 2005
    #7
  8. On Thu, 21 Apr 2005 16:30:48 +0200, JS wrote:

    > 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
     
    Lawrence Kirby, Apr 21, 2005
    #8
  9. 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
     
    Lawrence Kirby, Apr 21, 2005
    #9
  10. 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) <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, Apr 21, 2005
    #10
  11. Lawrence Kirby <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <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, Apr 21, 2005
    #11
  12. JS

    Chris Torek Guest

    In article <>
    Keith Thompson <> wrote:
    >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"... :)
    --
    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.
     
    Chris Torek, Apr 21, 2005
    #12
  13. JS

    Jack Klein Guest

    On Thu, 21 Apr 2005 20:38:03 GMT, Keith Thompson <> wrote
    in comp.lang.c:

    > Lawrence Kirby <> writes:
    > [...]
    > > 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.

    --
    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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Apr 22, 2005
    #13
  14. 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."
     
    Emmanuel Delahaye, May 1, 2005
    #14
  15. wrote on 21/04/05 :
    > 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?"
     
    Emmanuel Delahaye, May 1, 2005
    #15
  16. JS wrote on 21/04/05 :
    >> 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?


    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, May 1, 2005
    #16
  17. Chris Torek wrote on 21/04/05 :
    > In article <>
    > Keith Thompson <> wrote:
    >> 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"... :)


    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, May 1, 2005
    #17
  18. wrote on 21/04/05 :
    > 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);



    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, May 1, 2005
    #18
  19. Emmanuel Delahaye, May 1, 2005
    #19
  20. Emmanuel Delahaye, May 1, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    703
    sushant
    Jan 20, 2005
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
  3. tutmann
    Replies:
    4
    Views:
    460
  4. jmborr
    Replies:
    1
    Views:
    458
    Stargaming
    Nov 3, 2007
  5. Replies:
    3
    Views:
    864
Loading...

Share This Page