I have a function which counts the number of vowels and consonants in a string:
void CountVowelsConsonants(char* str, int *vowels, int *consonants){
size_t size=strlen(str);
//size-1 because of the '\n' at the end of the string when pressing enter
for (int i = 0; i < size ; ++i) {
char c = str[i];
if(c=='A' || c== 'E' || c=='I' || c== 'O' || c=='U' ||
c=='a' || c== 'e' || c=='i' || c== 'o' || c=='u'){
*vowels++;
}
}
*consonants= size-1 - *vowels;
}
But when I call this function, like this for example:
int vowels;
int consonants;
CountVowelsConsonants("abc", &vowels, &consonants);
it returns:
Vowels: -858993460
Consonants: 858993463
instead of:
Vowels: 1
Consonants: 2
I would like to know why this is the case.
I have a factorial function which has a similar implementation of the pointer and it works correctly:
void factorial(int n, int*fac){
*fac=1;
for (int i = 2; i <= n; ++i) {
*fac*=i;
}
}
Acessing the fact pointer in the factorial function will alter the value correctly. The only difference is that in the CountVowelsConsonants function I'm incrementing by 1 instead of multiplying.
However, if I access the vowels pointer in the CountVowelsConsonants function to increment it will produce errors in the counting.
My IDE (CLion) will grey out the * in *vowels, indicating that the pointer operator in
*vowels++; is useless.
Any ideas as to why this is the case?