Thread: string buffer

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    string buffer

    Hello everyone,


    Suppose we defined a string buffer (array), like this,

    char array[] = "hello world";
    char buf[256]

    Sometimes, I noticed that we either use,

    1. array (buf)
    or use,
    2. &array (&buf)
    or use
    3. &array[0] (&buf[0])

    as the beginning address of the array,

    example like,

    strcpy (buf, array);
    strcpy (&buf, array);
    ...

    I am wondering the differences between the 3 approaches, and which approach is the most correct?


    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm... both array and buf are the same type of variable, and since they're arrays, they're automatically cast to char* when you type the names, so the first strcpy is correct, and the second is not correct. But from what I understand, it's legal in C (which is just outright silly).
    buf == char* and &buf == char**.
    strcpy takes a char* and const char*.

    Code:
    1. array (buf)
    or use,
    2. &array (&buf)
    or use
    3. &array[0] (&buf[0])
    This is just another stupid example.
    array is not a function, it's an array - so array(buf) is not valid!
    But even array[buf] is really stupid since it would most likely give an access violation (not to mention you're converting char* to int, which is legal in C, but really stupid).

    Oh and,
    &array[0] (&buf[0])
    Is the same as
    array(buf)
    Last edited by Elysia; 12-10-2007 at 06:09 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM