C Programming - Converting exist function into function that uses pointers

This is a discussion on C Programming - Converting exist function into function that uses pointers within the C Programming forums, part of the General Programming Boards category; Hey. So I have never truly been able to understand pointers and need a little help. I wrote a small ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    C Programming - Converting exist function into function that uses pointers

    Hey. So I have never truly been able to understand pointers and need a little help. I wrote a small program that finds the number of words in a dictionary file but then my teacher told me that I need to rewrite it so that the function reads int WordCount(struct dict_node* head). However I had idea where to begin the conversion. Any guidance would be much appreciated!

    insert
    Code:
     int WordCount(void){
    
    	int WordCount;
    	char buffer[100];
    	FILE*fptr;
    	WordCount = 0;
    	
    	fptr = fopen("dictionary.txt", "r"); // opens the dictionary file
    	
    	//counts the number of words within the file
    	while(fscanf(fptr, "%s", &buffer) == 1){
    			WordCount++;
    	}		
    	fclose(fptr); //closes the file
    	
    return WordCount;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    What is the modified function supposed to do? Also, what is a struct dict_node?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Its suppose to do the same thing as the function I already wrote but he wont expect it because int WordCount(struct dict_node* head). Its I think (dont understand it very well) dict_node structure, teacher never really explained this to us.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Klcastillo
    Its suppose to do the same thing as the function I already wrote but he wont expect it because int WordCount(struct dict_node* head).
    If that is the case, then the solution is exceedingly simple
    Code:
    int WordCount(struct dict_node* head){
     
        int WordCount;
        char buffer[100];
        FILE*fptr;
        WordCount = 0;
         
        fptr = fopen("dictionary.txt", "r"); // opens the dictionary file
         
        //counts the number of words within the file
        while(fscanf(fptr, "%s", &buffer) == 1){
                WordCount++;
        }      
        fclose(fptr); //closes the file
         
        return WordCount;
    }
    But of course, you probably have the feeling that it is not so simple, because you must be expected to do something with the parameter. As such, the function is not supposed to do the same thing as what you already have, so you need to find out what it is.

    Quote Originally Posted by Klcastillo
    Its I think (dont understand it very well) dict_node structure, teacher never really explained this to us.
    Then you need to find out, because this is critical for you to get this right. I have my guesses, but I cannot explain to you what is struct dict_node without knowing what it is, which I don't. Consult your teacher if necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Sounds like your teacher has asked you for something a lot more complex. Especially as he expects you to deal with a "dict_node" called "head". Sounds like some sort of linked-list to me. There's a lot more "API" here that you've stated, and only your teacher knows what he actually expects of you.

    Sounds like you've missed out a lot of the point of the assignment.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and function calls inside a function
    By lexitron in forum C Programming
    Replies: 1
    Last Post: 12-03-2011, 05:43 PM
  2. Function for chek if letter exist
    By lio in forum C Programming
    Replies: 5
    Last Post: 01-18-2011, 01:43 PM
  3. Replies: 4
    Last Post: 05-21-2008, 06:48 AM
  4. Need help converting a C function to PHP
    By soadlink in forum C Programming
    Replies: 3
    Last Post: 10-04-2007, 04:51 PM
  5. Does a function like this currently exist?
    By IonBlade in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2003, 04:20 PM

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