Difference between char * and char [] arguments?

This is a discussion on Difference between char * and char [] arguments? within the C Programming forums, part of the General Programming Boards category; say you have a(char *s){...} and b(char s[]){...} How are strings passed in treated differently when arguments in functions are ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Difference between char * and char [] arguments?

    say you have a(char *s){...} and b(char s[]){...}

    How are strings passed in treated differently when arguments in functions are declared in this way? Or are they 100% interchangeable?

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    Completely interchangeable. In this instance only (as the formal parameter of a function declaration), [] means pointer.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    In your example, they're interchangeable (apart from your functions having different names, of course).

    Note that this does not mean an array and a pointer are the same things - they are not, but a trap for a lot of beginners (and a few too many experts, and even a few textbooks) is believing they are the same.

    When passing a single-dimensional array to a function, the compiler quietly converts the name of the array passed into a pointer (to the first element). However, that type of conversion is not universal - for example, the compiler is required to complain bitterly if two-dimensional array is passed directly to a function expecting a pointer-to-pointer (or vice versa).
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    in this case, they are the same, u don't know whether the arg is a array or a string

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jesius View Post
    in this case, they are the same, u don't know whether the arg is a array or a string
    A string IS an array... There are no true strings in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21