pointer to array as parameter to function

This is a discussion on pointer to array as parameter to function within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote a simple program that counts the letters in the words from a sentence. The program works, but ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    pointer to array as parameter to function

    Hi,

    I wrote a simple program that counts the letters in the words from a sentence.
    The program works, but what i was wondering is :
    did i use the correct approach to pass the pointers to the function bereken. Also with a pointer to an array, do you have to de-reference this pointer (in the function bereken) ? I just used the name without de-referencing, this seems to work.

    Thanks.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    void bereken(char *zinp, int *letters);
    
    void main()
    {
    	char zin[80];
    	int letters[10];
    	int i;
    
    	//clrscr();
    	cout << "Geef een nieuwe regel: ";
    	cin.getline(zin,80);
    
    	cout << "lengte zin : " << strlen(zin) << endl;
    
    	// initialiseren van de array
    
    	for(i=0; i<10; i++)
    		letters[i]=0;
    
    	bereken(zin, letters);
    
    	for (i=0; i<10; i++)
    	{
    		cout << "Aantal woorden met " << i+1 << " letters: " << letters[i] << endl;
    	}
    }
    
    void bereken(char *zinp, int *letters)
    {
     int j, letters2;
    
     letters2=0;
     j=0;
     do
     {
        if ((zinp[j] != ' ') && (zinp[j] != '.'))
           letters2+=1;
    	else
    	{
    	   letters[letters2-1]+=1;
    	   letters2=0;
    	}
    	j++;
     } while (zinp[j] != '.');
    
     // laatste waarde ook nog in tabel zetten
    
     letters[letters2-1]+=1;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    int main(). Main has never returned void in the history of C++. Anyone who tells you otherwise is wrong.

    >did i use the correct approach to pass the pointers to the function bereken
    Yes. You could have also used array notation for the function arguments to show that they're arrays:
    Code:
    void bereken(char zinp[], int letters[]);
    The effect is the same either way.

    >I just used the name without de-referencing, this seems to work.
    Ah, but you did dereference your pointers. You just didn't realize it. Array subscripting is simply dereferencing a pointer offset, so this:
    Code:
    letters[letters2-1]+=1;
    Is just notational convenience and the compiler really sees this:
    Code:
    *(letters + (letters2 - 1)) += 1;
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. A pointer to an object's function as a parameter
    By wiiire in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2007, 10:38 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Replies: 42
    Last Post: 12-19-2004, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21