Thread: having some trouble with making a simple function,help please

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    157

    having some trouble with making a simple function,help please

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void generate(int *number,int *number_of_words,char words[30][6]);
    
    
    int main()
    {     int number,number_of_words;
         char words[30][6];
        
        generate(&number,&number_of_words,&words[30][6]);
        
        printf("\n\n word is %c%c%c%c%c",words[number][1],words[number][2],words[number][3],words[number][4],words[number][5]);
    
    
    return 0;
    }
    void generate(int *number,int *number_of_words,char words[30][6])
    {
     
         
        int i,j;
        FILE*text;
        text=fopen("words.txt","r");
        srand( (unsigned) time (NULL) );
        *number=rand()%30;
        
         
         
         if (text == NULL)
         {
             exit(1);
         }
        
        fscanf(text,"%i",*number_of_words);
        
        for (i=0;i<*number_of_words;i++)
        
        {    for(j = 0; j <6 ; j++)
            {
              
            fscanf(text,"%c",words[i][j]);
            
            
            }
            
        
            
        }    
        
            fclose(text);
            
    }
    Last edited by africanwizz; 11-18-2012 at 03:55 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char words[30][6];
    If this is your array.

    Then the function should be declared as (and the same in the implementation)
    void generate(int *number,int *number_of_words,char words[30][6]);

    And called as
    generate(&number,&number_of_words,words);


    > fscanf(text,"%c",*words[i][j]);
    Do exactly what you would do, if the array was in scope.
    ie
    fscanf(text,"%c",&words[i][j]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i tried that but it still gives me an error "passing argument 3 of 'generate' makes pointer from integer without cast"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Inside the generate() function, with words a pointer to int, words[i] is a char. words[i][j] is meaningless, which is why your compiler is complaining bitterly. Prefixing words[i][j] with an asterix is just insane (but fortunately the compiler will keep complaining).

    The bigger problem is in how you're calling the function &words[30][6] is the address of an int that is past the end of the words array. Since you're then (not withstanding the errors I have pointed out inside the generate() function) treating that address as the start of a 2D array, your code will tromp random areas of memory.



    More generally, when a compiler complains, the onus is on you to reason out WHY it is complaining, rather than just randomly hacking code in the hope things will work. Your biggest mistake is that you are electing to use the hacking approach. If my response seems unhelpful, it is because your hacking approach makes it pointless to be more helpful than I am.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    okay,i have re-wrote the code,but i still dont understand why i am getting "passing argument 3 of 'generate' makes pointer from integer without cast" if someone would be kind enough to explain this to me

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void generate(int *number,int *number_of_words,char words[30][6]);
    
    
    int main()
    {     int number,number_of_words;
         char words[30][6];
        
        generate(&number,&number_of_words,words);
        
        printf("\n\n word is %c%c%c%c%c",words[number][1],words[number][2],words[number][3],words[number][4],words[number][5]);
    
    
    return 0;
    }
    void generate(int *number,int *number_of_words,char words[30][6])
    {
     
         
        int i,j;
        FILE*text;
        text=fopen("words.txt","r");
        srand( (unsigned) time (NULL) );
        *number=rand()%30;
        
         
         
         if (text == NULL)
         {
             exit(1);
         }
        
        fscanf(text,"%i",number_of_words);
        
        for (i=0;i<*number_of_words;i++)
        
        {    for(j = 0; j <6 ; j++)
            {
              
            fscanf(text,"%c",&words[i][j]);
            
            
            }
            
        
            
        }    
        
            fclose(text);
            
    }
    See?
    Compiles without error here.

    Whether it runs or not is another matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    THANK's salem,it makes sense now ..and i feel pretty dumb.thanks

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    could someone please explain to me why the program wont work,the compiler builds it without error but it doesnt work

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > could someone please explain to me why the program wont work,the compiler builds it without error but it doesnt work
    And what do you mean "doesn't work"?

    Does it crash?
    If it doesn't crash, what output do you see, and how is that different from what you expect?

    Remember, we don't have your words.txt file to even begin to guess how you're reading it wrong.

    But given that you're using %c to read the file, you need to understand that file format is critical. If you're out by even 1 character, then the program just won't work as expected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    the program crahes,im sure it reads from the text fine.on its own it worked fine .its only when i make to refrence the funtion(used in a larger program) that it gives me a problem,i dont know if i am using the wromg parameters

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Without the code and an example of the text file we can't help you.

    Bye, Andreas

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    the text file is simply a list of 30 ,5 letter words with number 30 at the top.

    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void generate(int *number,int *number_of_words,char words[30][6]);
    
    
    int main()
    { 	int number,number_of_words;
     	char words[30][6];
    	
    	generate(&number,&number_of_words,words);
    	
    	
    	printf("\n\n word is %c%c%c%c%c",words[number][1],words[number][2],words[number][3],words[number][4],words[number][5]);
    
    
    
    
    return 0;
    }
    void generate(int *number,int *number_of_words,char words[30][6])
    {
     
     	
    	int i,j;
    	FILE*text;
    	text=fopen("words.txt","r");
    	srand( (unsigned) time (NULL) );
        *number=rand()%30;
    	
    	 
    	 
    	 if (text == NULL)
    	 {
    	 	exit(1);
    	 }
    	
    	fscanf(text,"%i",*number_of_words);
    	
    	for (i=0;i<*number_of_words;i++)
    	
    	{	for(j = 0; j <6 ; j++)
    		{
    		  
    	    fscanf(text,"%c",words[i][j]);
    		
    		
    		}
    		
        
    		
    	}	
    	
    		fclose(text);
    		
    }

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    for (i=0;i<*number_of_words;i++)
    "number_of_words" is not initialised.
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    the fscanf function gives a value of 30 to "number_of_words"

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by africanwizz View Post
    the fscanf function gives a value of 30 to "number_of_words"
    Whoops - So it does!

    Check to see if it is 30.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Trouble making a font name list
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 11-10-2006, 04:20 PM
  3. Trouble making an Array equal a Function
    By Ozor in forum C++ Programming
    Replies: 16
    Last Post: 07-11-2003, 10:33 AM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM

Tags for this Thread