pointers and arrays
hey i am trying to learn pointers and references.
how can i pass an array of characters by reference to a function?
this works with one character but i can't figure out how to make it work with an array
void funct(char *text)
cout << *text << endl;
*text = 'c';
cin >> text;
cout << text << endl;
Arrays are automatically passed by reference. If you want to pass a null-terminated character array, you can make the function parameter char* text or char text since the size is determined by the terminating null character. For other arrays, you have to pass the size as a separate parameter, or as part of the array parameter: int arr.
Of course, in C++ you should just use the C++ string class instead null-terminated character arrays, and vector (or some other container) instead of regular arrays.
You wouldn't need to pass an array by reference. You already pass the array by pointer. Just use cin.get(), fill a character array, and pass that to funct. What happens after that will be dependant on what you're trying to do. You'd have to use a loop to fill the array with 'c'. Or use memset().
that was simple lol