Char []

Discussion in 'C Programming' started by Rick, Oct 14, 2003.

  1. Rick

    Rick Guest

    Hi,

    Before copying something into a character array, do we need to
    initialize it with something? For example:

    char a[10];

    strcpy(a, "hey");

    Can we do:

    char a[10] = {0};

    strcpy(a, "hey");


    Thanks

    Rick
     
    Rick, Oct 14, 2003
    #1
    1. Advertising

  2. "Rick" <rrquick@nospam-com> wrote in message
    news:3f8bade2$...
    > Hi,
    >
    > Before copying something into a character array, do we need to
    > initialize it with something? For example:
    >
    > char a[10];
    >
    > strcpy(a, "hey");
    >
    > Can we do:
    >
    > char a[10] = {0};
    >
    > strcpy(a, "hey");
    >

    You can write:

    char a[] = "hey";


    >
    > Thanks
    >
    > Rick
    >
     
    Vijay Kumar R Zanvar, Oct 14, 2003
    #2
    1. Advertising

  3. Rick

    Rick Guest

    > You can write:
    >
    > char a[] = "hey";


    I need to copy a string into the array at a later stage.

    Rick
     
    Rick, Oct 14, 2003
    #3
  4. Rick

    Alex Guest

    "Rick" <rrquick@nospam-com> wrote in message
    news:3f8bade2$...
    > Before copying something into a character array, do we need to
    > initialize it with something?


    No. It serves no purpose, but you can if you want to.

    Try writing a function my_strcpy() that behaves like strcpy(). Does this
    function care what the contents of the array are beforehand?

    Alex
     
    Alex, Oct 14, 2003
    #4
  5. Rick

    Richard Bos Guest

    Rick <rrquick@nospam-com> wrote:

    > Before copying something into a character array, do we need to
    > initialize it with something? For example:


    No.

    > char a[10];
    >
    > strcpy(a, "hey");
    >
    > Can we do:
    >
    > char a[10] = {0};
    >
    > strcpy(a, "hey");


    You can, but you don't need to. It serves no purpose whatsoever, except
    possibly to keep braindead compilers from issuing warnings.

    Richard
     
    Richard Bos, Oct 14, 2003
    #5
  6. In article <3f8bb9f4$>, Rick wrote:
    >> You can write:
    >>
    >> char a[] = "hey";

    >
    > I need to copy a string into the array at a later stage.


    No, you don't need to initialize the string. You only need to
    make sure that the string i long enough to hold the data that
    you're copying to it.

    If you're copying the string character by character yourself,
    make sure to place a nul character (string terminator, '\0')
    after the last "visible" character in the string, to terminate
    the string. Most library functions handles the string
    termination themselves (read about each function you use).

    The terminator counts as one charcater, so there must be room
    for it as well.


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 14, 2003
    #6
  7. Rick <rrquick@nospam-com> wrote:

    >Before copying something into a character array, do we need to
    >initialize it with something? For example:
    >
    >char a[10];
    >strcpy(a, "hey");
    >
    >Can we do:
    >
    >char a[10] = {0};
    >strcpy(a, "hey");


    You can, but you don't have to.
    Just for completeness:

    char a[10] = "hey";

    is fine, too.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 14, 2003
    #7
  8. Rick

    Rick Guest

    Does the following hold any meaning on its own:

    char a[10] = {0};

    I know this works for structs.. but I'm not sure about character arrays.
    Thanks

    Rick
     
    Rick, Oct 14, 2003
    #8
  9. Rick <rrquick@nospam-com> wrote:

    >Does the following hold any meaning on its own:
    >
    >char a[10] = {0};


    Initialize all elements of array a to 0.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 14, 2003
    #9
  10. On Tue, 14 Oct 2003 19:05:42 +1000, in comp.lang.c , Rick
    <rrquick@nospam-com> wrote:

    >> You can write:
    >>
    >> char a[] = "hey";

    >
    >I need to copy a string into the array at a later stage.


    in that case, make it big enogh when you declare it
    char a[BIG_ENOUGH] = "pointless initialiser";

    The initialiser serves no purpose except to cause unexpected errors if
    you use memcpy or strcat carelessly. This may of course be a useful
    purpose. I sometimes initialise strings with '\0' to allow better
    error checking, or so that you don't need to consider the first copy
    into it a special case. .

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #10
  11. On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
    Grausewitz <> wrote:

    >Rick <rrquick@nospam-com> wrote:
    >
    >>Does the following hold any meaning on its own:
    >>
    >>char a[10] = {0};

    >
    >Initialize all elements of array a to 0.


    strictly, I believe it initialises the first element to the value
    inside the braces, and then the rest to zero or a null pointer as
    appropriate for the data type. Which adds up to the same thing for
    most data types.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #11
  12. Rick

    Al Bowers Guest

    Rick wrote:
    > Hi,
    >
    > Before copying something into a character array, do we need to
    > initialize it with something? For example:
    >
    > char a[10];
    >
    > strcpy(a, "hey");
    >
    > Can we do:
    >
    > char a[10] = {0};
    >
    > strcpy(a, "hey");
    >
    >


    Whether you initialize or not initialize will depend on your
    intended use of the char array.
    If you are function strcpy, the destination char array does
    not need to be initialized. If you are going to use the char
    array as the destination for the strcat family of functions,
    you wll need to initialize it or somehow make it a string
    before you use it.

    Ex:
    An empty string:
    char a[16] = "";
    or
    char a[16] = {'\0'};

    strcat(a,"Hello World");
    char array a would contain the string "Hello World".

    You can initialize with a string and append.
    char a[16] = "Hello ";
    strcat(a,"World");
    char array a would contain the string "Hello World".

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Oct 14, 2003
    #12
  13. Mark McIntyre <> wrote:

    >On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
    >Grausewitz <> wrote:
    >
    >>Rick <rrquick@nospam-com> wrote:
    >>
    >>>Does the following hold any meaning on its own:
    >>>
    >>>char a[10] = {0};

    >>
    >>Initialize all elements of array a to 0.

    >
    >strictly, I believe it initialises the first element to the value
    >inside the braces, and then the rest to zero [...].


    That's more accurate, right. However, given the example above, the
    result is that all elements are initialized to zero, anyway.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 14, 2003
    #13
  14. Rick

    John Bode Guest

    Rick <rrquick@nospam-com> wrote in message news:<3f8bade2$>...
    > Hi,
    >
    > Before copying something into a character array, do we need to
    > initialize it with something? For example:
    >


    Generally, no. If you're using strcpy() or strcat() to write to a
    string, it doesn't matter what was there before. Depending on the
    circumstances, I might initialize the contents of an array if there's
    a chance it will be read by something before it's been written to.
     
    John Bode, Oct 14, 2003
    #14
  15. you might want to initialize it when using functions like strncpy or
    make sure that after you copy n bytes to a string that "string"[n] is 0.

    John Bode wrote:
    > Rick <rrquick@nospam-com> wrote in message news:<3f8bade2$>...
    >
    >>Hi,
    >>
    >>Before copying something into a character array, do we need to
    >>initialize it with something? For example:
    >>

    >
    >
    > Generally, no. If you're using strcpy() or strcat() to write to a
    > string, it doesn't matter what was there before. Depending on the
    > circumstances, I might initialize the contents of an array if there's
    > a chance it will be read by something before it's been written to.
     
    Ron Croonenberg, Oct 14, 2003
    #15
  16. John Bode wrote:
    > Rick <rrquick@nospam-com> wrote in message news:<3f8bade2$>...
    >> Before copying something into a character array, do we need to
    >> initialize it with something? For example:

    >
    > Generally, no. If you're using strcpy() or strcat() to write to a
    > string, it doesn't matter what was there before.


    strcat() is not a good example; it relies on the destination buffer
    containing a string.

    Jeremy.
     
    Jeremy Yallop, Oct 14, 2003
    #16
  17. On Tue, 14 Oct 2003 17:48:55 +0200, in comp.lang.c , Irrwahn
    Grausewitz <> wrote:

    >Mark McIntyre <> wrote:
    >
    >>On Tue, 14 Oct 2003 14:20:20 +0200, in comp.lang.c , Irrwahn
    >>Grausewitz <> wrote:
    >>
    >>>Rick <rrquick@nospam-com> wrote:
    >>>
    >>>>Does the following hold any meaning on its own:
    >>>>
    >>>>char a[10] = {0};
    >>>
    >>>Initialize all elements of array a to 0.

    >>
    >>strictly, I believe it initialises the first element to the value
    >>inside the braces, and then the rest to zero [...].

    >
    >That's more accurate, right. However, given the example above, the
    >result is that all elements are initialized to zero, anyway.


    Absolutely., I wanted to avoid a potential misunderstanding.



    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #17
  18. Rick

    John Bode Guest

    Jeremy Yallop <> wrote in message news:<>...
    > John Bode wrote:
    > > Rick <rrquick@nospam-com> wrote in message news:<3f8bade2$>...
    > >> Before copying something into a character array, do we need to
    > >> initialize it with something? For example:

    > >
    > > Generally, no. If you're using strcpy() or strcat() to write to a
    > > string, it doesn't matter what was there before.

    >
    > strcat() is not a good example; it relies on the destination buffer
    > containing a string.
    >
    > Jeremy.


    You're absolutely right. I was a bit fried when I wrote that.
     
    John Bode, Oct 15, 2003
    #18
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    588
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
Loading...

Share This Page