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
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
Of course you can. Show us some code, maybe we can help.
You pass the array name which will translate to a pointer to the first element. For the function declaration/definition:
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.Code:void somefunction(char* foo) { /* ... */ } void somefunction(char foo[]) { /* ... */ }
So, that's the parameters definitions. As for argument passing:
As simple as that. Finally, inside the function it's a pointer you are dealing with.Code:char bar[3] = { 'a', 'b', 'c' }; somefunction(bar);
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.
FTFY:
Code:char bar[4] = { 'a', 'b', 'c', '\0' }; somefunction(bar);
He mentioned char array. The null terminator is irrelevant. And depending on the context, an actual error.
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.
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.
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.
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
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!
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.