Like Tree1Likes
  • 1 Post By Mario F.

Passing CHAR array to function

This is a discussion on Passing CHAR array to function within the C++ Programming forums, part of the General Programming Boards category; Is it possible to pass an array of CHARs to a function? How do you do it? I keep getting ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Passing CHAR array to function

    Is it possible to pass an array of CHARs to a function? How do you do it? I keep getting garbage.. I know I'm missing something, but I'm not even sure if what I'm trying to do is possible.

    Thanks,

    Zach

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Of course you can. Show us some code, maybe we can help.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Quote Originally Posted by Zzaacchh View Post
    Is it possible to pass an array of CHARs to a function? How do you do it? I keep getting garbage.. I know I'm missing something, but I'm not even sure if what I'm trying to do is possible.
    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.
    codebugger likes this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    FTFY:
    Code:
    char bar[4] = { 'a', 'b', 'c', '\0' };
    somefunction(bar);

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    He mentioned char array. The null terminator is irrelevant. And depending on the context, an actual error.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Mario F. View Post
    He mentioned char array. The null terminator is irrelevant. And depending on the context, an actual error.
    To make that more clear, strings in C are null terminated, but not all char arrays are considered strings. Meaning, you may want an array of char but not a string.
    The difference is that there are special functions for strings, specially in string.h, that require the char array to be null terminated to work properly. And for example in printf/scanf %s works for null terminated char arrays.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    OK, but if you were going to pass an array of characters (not for use as a string), wouldn't you also need to pass the size of that array?

    In any case, I contend that given the nature of the question, this person is most likely using the array of chars as a string, and therefore the non-null-terminated solution would almost certainly lead to further confusion on the part of the OP.

  8. #8
    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

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User Will Hemsworth's Avatar
    Join Date
    Sep 2008
    Location
    England - Lymm
    Posts
    6
    I guess if you just wanted to pass a "char array", not a pointer to one, you could do something like this,
    however both arrays need to have the same number of characters otherwise they are considered different types.
    But this is most probably not the best solution, so I would say, stick with pointers
    Code:
    #include <iostream>
    using namespace std;
    
    template<size_t length>
    struct char_array {
       char chars[length];
       operator char*() {
          return chars;
       }
    };
    
    void somefunction(char_array<100> b) {
    }
    
    int main() {
       char_array<100> chars;
       strcpy(chars, "Some String");
       somefunction(chars);
       return 0;
    }
    Last edited by Will Hemsworth; 09-21-2008 at 11:15 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    But this is not desirable either since it makes a copy of the entire array...
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 04:26 PM

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