How to get the same result using pointers notation

Hi ppl i'm new to this C++ programming and learning more.
recently i constructed a programs to count the number of characters.
i manage and successfully make the program to run and get the result i want.
i read and was told that it also able to work in pointers notation.
it was unsuccessful for me to change it to pointers and run.(think becos i m too noob)
so expert out there. how am i suppose to change it to pointers to get back the same result, what must i take note of??what must be added??


while (!inFile.eof())
{
inFile.getline(fileName, 90);
int size = 0;
for (int i=0; i<strlen(fileName); i++)
{
cout << fileName[i];
if (fileName[i] != ' ')
cntall++;
if (isspace(fileName[i]))
cntb ++;
if (isupper(fileName[i]))
cntu ++;
if (islower(fileName[i]))
cntl ++;
if (isdigit(fileName[i]))
cntd ++;
if (ispunct(fileName[i]))
cntp ++;


}
cout << "\n";
}
cout <<"\n";
cout << "Total number of characters: " << cntall << endl;
cout << "Number of uppercase characters: " << cntu << endl;
cout << "Number of lowercase characters: " << cntl << endl;
cout << "Number of decimal digits: " << cntu << endl;
cout << "Number of blanks: " << cntb << endl;
cout << "Number of end-of-sentence punctuation marks: " << cntp << endl;
cout << "Others: " << cnto << endl;
cout <<"\n";
Last edited on
Hi !!! :)

defining any array is somehow the same as defining a pointer to the same type that the array elements are, the difference is that the array somehow keeps ("knows") its size (number of elements). So the following two snippets of codes are equivalent:

1
2
3
4
5
6
7
8
9
char c_arr[40];

// 1. initialization of elements using array notation
for (int k=0; k<40; ++k)
    c_arr[k] = k;

// 2. initialization of elements using pointers' notation
for (int k=0; k<40; ++k)
    *(c_arr + k) = k;


The same will work for dynamically allocated memory pointer:

1
2
3
char* c_arr = new char(40);
.........................
delete [] c_arr; 


the name of the array is a pointer per sei, which points to the beginning of that array. Any increment with k to that pointer again is a pointer which points to the memory k*sizeof(array_element_type) offset from beginning of array. So using the operator * you can get the value of pointing memory, i.e. , (c_arr + i) is a pointer of int* type, and the *(c_arr + i) is the value of array's i-th element.
Topic archived. No new replies allowed.