Thread: Problem involving a pointer/passing a pointer through a function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Problem involving a pointer/passing a pointer through a function

    edit: took out the code since I posted updated code a few posts down
    Last edited by Sholcomb1; 11-05-2006 at 02:40 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > FileOpen(&fileinp, dataFile, &length);
    You need an extra * in several places then.

    Code:
    void FileOpen(FILE **fileptr, char *file_name, int *lengthptr)
    {
       printf("Please enter the filename you wish to open : ");
       scanf("%s", file_name);
    
       *fileptr = fopen(file_name, "r");
       
       if (*fileptr == NULL)
       {
          printf("An error has occurred while attempting to open the file\n");
          exit(-1);
       }
    
       while(fgetc(*fileptr) != EOF)
       {
          length++;
       }
      
      
    }
    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
    Sep 2006
    Posts
    10
    ooooh, so that extra * in the prototype is what was needed, Ok. Thanks alot!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Ok, so the extra * allowed the file input to work out well, but I'm still having issues with the length pointer counter actually incrementing.

    Code:
    void FileOpen(FILE *fileptr, char *file_name, int *lengthptr)
    {
       printf("Please enter the filename you wish to open : ");
       scanf("%s", file_name);
    
       fileptr = fopen(file_name, "r");
       
       if (fileptr == NULL)
       {
          printf("An error has occurred while attempting to open the file\n");
          exit(-1);
       }
    
       while(fgetc(inp1) != EOF)
       {
          length++;
       }
      
      
    }
    I pass the variable by reference "length" to the function, and the value of length is always zero. However, when I copy/paste that exact same code into main and swap out the pointers for the regular variables it works perfectly fine. Am I not allowed to do an increment to lengthptr? Am I missing something that would keep lengthptr from incrementing?

    Code:
    while ( fgetc(fileinput) != EOF
    {
        length++;
    }
    (this is what I would put into main, which would work out fine)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Same thing - use a *

    Though watch out for
    ++*p
    is different from
    *p++
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. problem about static Lib and function pointer
    By wu7up in forum C Programming
    Replies: 3
    Last Post: 02-24-2003, 09:34 AM