Thread: Need to pass struct array to function. What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176

    Need to pass struct array to function. What am I doing wrong?

    Code:
     void sortDictionary(struct entry dictionary, int size)
    {
    	int a,b;
    	struct entry temp;
    
    	for(a=0;a<size-1;a++)
    	{
    		for(b=a+1;b<size;b++)
    		{ // execute loop if compareString answer = 1
    			if(compareString(dictionary[a].word,dictionary[b].word)==1)
    			{
    				temp=dictionary[b];
    				dictionary[b]=dictionary[a];
    				dictionary[a]=temp;
    			}
    		}
    	}
    	return;
    )
    The compile errors I get from this are:

    Code:
    ------ Build started: Project: structure array sort practice, Configuration: Debug Win32 ------
    Compiling...
    structure array sort practice.cpp
    c:\documents and settings\cruisin'\my documents\visual studio 2008\projects\structure array sort practice\structure array sort practice\structure array sort practice.cpp(19) : error C2676: binary '[' : 'entry' does not define this operator or a conversion to a type acceptable to the predefined operator
    c:\documents and settings\cruisin'\my documents\visual studio 2008\projects\structure array sort practice\structure array sort practice\structure array sort practice.cpp(19) : error C2228: left of '.word' must have class/struct/union
    c:\documents and settings\cruisin'\my documents\visual studio 2008\projects\structure array sort practice\structure array sort practice\structure array sort practice.cpp(19) : error C2676: binary '[' : 'entry' does not define this operator or a conversion to a type acceptable to the predefined operator
    c:\documents and settings\cruisin'\my documents\visual studio 2008\projects\structure array sort practice\structure array sort practice\structure array sort practice.cpp(19) : error C2228: left of '.word' must have class/struct/union
    c:\documents and settings\cruisin'\my documents\visual studio 2008\projects\structure array sort practice\structure array sort practice\structure array sort practice.cpp(19) : error C3861: 'compareString': identifier not found

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void sortDictionary(struct entry dictionary, int size)
    That is not an array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Oops got it.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    ok I got another one that is making me scratch my head. Runtime error says 'i' is being used without being initialized. And the debuig arrow points to line in bold.

    Code:
    #include <stdio.h>
    
    int countDecimalPlace(char decimal[20])
    {
    	int i,count=0;
    
    	while(decimal[i]!='\0')
    			{
    				
    				if(decimal[i]=='.')
    				{
    						do
    						{
    							i++;
    							count++;
    						}while(decimal[i] !='\0');
    
    						return count;
    				}
    				i++;
    
    			}
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Whoops ignore last post I'm stupid. haha. sorry. Nothing to see here.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you look at the row above the bold one. You see, int i there, you don't give i an initial value, it's thus not initialized which means it can have any value.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Quote Originally Posted by Subsonics View Post
    If you look at the row above the bold one. You see, int i there, you don't give i an initial value, it's thus not initialized which means it can have any value.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  2. How to pass a pointer of a struct to a function
    By drty2 in forum C Programming
    Replies: 12
    Last Post: 01-19-2009, 12:34 PM
  3. Pass struct to a function.
    By Hybird in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2005, 12:12 AM
  4. How to pass a typedef struct to function?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 09-19-2004, 04:06 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM