Thread: pass by reference question

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    pass by reference question

    Hey.
    Here is alittle bit of my code.

    Code:
    char words[MAX_SIZE]; /* array of words */
    int totalLetters;
    
    printf("%d\n", calcSize(words, &totalLetters)); /* print value of function calcSize*/
    int calcSize(char words[], int *totalLetters) /* function calcSize */
    {
    	int sum = 0;
    	int i =0;
    	while(words[i] != '\0') {
    		for (i =0; words[i] ; i++)
    			*totalLetters++;
    	}
    	return *totalLetters;
    
    }
    I keep getting some weird numbers such as 66732 when it prints and obviously i am only putting in a 4-5 letter word.
    What i am trying to do is everytime i run a word through this calcSize() function, to count the totalLetters of that word and return the totalLetters, but next time I call the function I want totalLetters to still hold the new value. So if i had 2 words: Hello world, at the end of the 2 calls , totalLetters should return 10.

    Im just starting to use pointers so i suspect the problem is related to them.
    Thanks
    PS. title should probably be Call by reference.
    Last edited by gp364481; 09-11-2008 at 10:56 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    The value of totalLetters is never being used outside of calcSize, so you really don't need to pass it in. You're already returning its value from the function anyway. What you'll want to do is declare totalLetters as a static local inside CalcSize. By declaring it as static, it will retain its value from one call to the next. Be sure to initialize it as part of the declaration.

  3. #3
    Registered User Peritus's Avatar
    Join Date
    Aug 2008
    Posts
    3
    (*totalLetters)++;

    precedence.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by gp364481 View Post
    Im just starting to use pointers so i suspect the problem is related to them.
    In fact it's pass by reference, so your title is correct. Also your problem is not related to pointers. You never initialise TotalLetters, so you are not guaranteed any particular value. Your function then just increments the total. So if you already have 6538 in there, you'll get 6538+5 for a five letter word. Add a = 0; to your declaration and it should work just fine.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you still want to use totalLetters as a global variable then you don't have to return its value from the function calcSize. It s a global variable, its value is changed inside a function, so why return it? You can just read the value of totalLetters.
    If you don't want it to be global then the "nicest" way is declaring it static as noted above

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Code:
    char words[MAX_SIZE];
    int totalLetters = 0;
    
    printf("%d\n", calcSize(words, &totalLetters));
    
    int calcSize(char words[], int *totalLetters)
    {
    	int sum = 0;
    	int i =0;
    	while(words[i] != '\0') {
    		for (i =0; words[i] ; i++) {
    			 (*totalLetters)++;
    		}
    	}
    	return (*totalLetters);
    
    }
    ok this is my updated code. it seems to be working
    Last edited by gp364481; 09-11-2008 at 06:55 PM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by QuantumPete View Post
    In fact it's pass by reference, so your title is correct. Also your problem is not related to pointers. You never initialise TotalLetters, so you are not guaranteed any particular value. Your function then just increments the total. So if you already have 6538 in there, you'll get 6538+5 for a five letter word. Add a = 0; to your declaration and it should work just fine.

    QuantumPete
    You were right. I had to initialized it to 0.
    also Pertius, (*totalLetters)++; , that help too ;0

    clairvoyant, i didnt really understand your approach. I got it to work but if you care to explain I wouldnt mind listening.


    thanks for ur replies everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM