Thread: Local variable error

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Local variable error

    I have used:

    Code:
    clock_t start;
      start = clock();
    in one of my functions.

    I then use the same again in a different function, but in the second function I get an error:

    Code:
    Local variable 'start' is initialised but never used
    yet if I remove the line clock_t start in my second function, I then get an error saying that start is an undeclared identifier.

    Here is the part of the second function:

    Code:
    clock_t start;
      start = clock();
       for (tmp = list; tmp != NULL; tmp = tmp->next)
       {
          total += tmp->num;
          
       }
    
       for (tmp = list; tmp != NULL; tmp = tmp->next)
       {
          if (strcmp(tmp ->word, query) == 0) {   printf("\n'"; }
       }
        (double)(clock() - start) / CLOCKS_PER_SEC;
    Since the code for the clock with copied and pasted from my previous bit of code, I am very surprised it is not working.
    Why is this wrong, and what can I do to fix it?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I don't see anything wrong with that except that you don't store the value of your last division. Post more code.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    OK the previous bit was summarised to exclude irrelevant bits. Here's the whole function.

    Code:
    void printWord(WordPtr *list)
    {
       WordPtr *tmp;
       int size = 0;
       int total = 0;
    //routine to do search
    char query[40];
    printf("Enter a word for retrieval:");
    scanf("%s", query);
    clock_t start;
      start = clock();
       for (tmp = list; tmp != NULL; tmp = tmp->next)
       {
          total += tmp->num;
          size += 1;
       }
    
       for (tmp = list; tmp != NULL; tmp = tmp->next)
       {
          if (strcmp(tmp ->word, query) == 0) {   printf("\n'%s' occurs %4d times.\n", tmp->word, tmp->num); }
       }
        (double)(clock() - start) / CLOCKS_PER_SEC;
    
    }

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    My guess is that the compiler is recognizing that the following line does nothing, and is optimizing it out.
    Code:
    (double)(clock() - start) / CLOCKS_PER_SEC;
    Since that's the only place start is used, it says that it's effectively not being used. Try saving that value to another memory location.
    Code:
    double time_length = (double)(clock() - start) / CLOCKS_PER_SEC);
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Ah yes its working now. Thanks Pianorain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM