#define kVOWEL_COUNT 5
/* Just fill in a pre-defined array.
* Be careful with this one! You need to
* make sure you always have enough
* space in the passed array otherwise
* BadThings™ will happen.
*/
void makeVowels_arg( char *vowels ) {
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
}
/*
* Dynamically allocate a new array and
* return a pointer to it.
* You will need to free() the returned value
* once you are done with it!
*/
char* makeVowels_alloc( void ) {
char *vowels = (char*)malloc( sizeof( char ) * kVOWEL_COUNT );
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
return vowels;
}
int main( void ) {
char varr[kVOWEL_COUNT] = {'\0'};
char *vptr = NULL;
/* Since the varr array exists in the current scope
* we can just pass a pointer to it and have this
* function fill it in. Remember, the array will still
* disappear once we leave the current scope!
*/
makeVowels_arg( (char*)varr );
/* Here we are getting a dynamically allocated array
* back from the makeVowels function. The difference
* between this function and the above one is that the
* array is allocated on the heap which means it will be
* around until either the program exits or you free()
* the pointer. Be careful with this one! If you don't manage your
* memory well you will end up with leaks.
*/
vptr = makeVowels_alloc();
free( vptr );
vptr = NULL; // Not necessary but I'm paranoid.
return 0;
}