Thread: Passing CHAR array to function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    Quote Originally Posted by Mario F. View Post
    You pass the array name which will translate to a pointer to the first element. For the function declaration/definition:

    Code:
    void somefunction(char* foo) { /* ... */ }
    void somefunction(char foo[]) { /* ... */ }
    I prefer the second form when the semantics of the argument justify you wanting to make it clear the function will perform pointer arithmetics. Otherwise I use the first form (pointer notation). In any case the fist form could probably be considered the correct one since arrays are never passed as arguments. Only pointers.

    So, that's the parameters definitions. As for argument passing:

    Code:
    char bar[3] = { 'a', 'b', 'c' };
    
    somefunction(bar);
    As simple as that. Finally, inside the function it's a pointer you are dealing with.
    Sweet, thank you. It was driving me mad.

    So, to get this strait, you cannot pass a character array to a function unless it is actually a pointer to the array (makes sense) Other than that, you can really only pass elements of the array one at a time (through a loop or whatever.) ?



    -Zach

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Zzaacchh View Post
    So, to get this strait, you cannot pass a character array to a function unless it is actually a pointer to the array (makes sense) Other than that, you can really only pass elements of the array one at a time (through a loop or whatever.) ?
    Erm no, sorry.
    You can either pass a pointer to the first element (default when you just pass the array with no address-of operator).
    Or, you can pass a pointer to an array, which is different from the above.
    When incrementing or using array syntax ([]), the first increments one element ( sizeof(*pointer) ), while the second jumps to the end of the array.
    You get the second if you use address-of on the array when passing:

    char myarray[] = "My love!";
    myfunc(myarray); // Method 1
    myfunc(&myarray); // Method 2

    Anyway, if you want to avoid headaches - stick to the first!
    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. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM