passing pointers outside of functions

This is a discussion on passing pointers outside of functions within the C Programming forums, part of the General Programming Boards category; I am trying to create a function that reads everything from a file and puts it into memory. void fileRead(char ...

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

    Question passing pointers outside of functions

    I am trying to create a function that reads everything from a file and puts it into memory.

    void fileRead(char *filePtr,const char *filename)
    Successfully copies the data from the file into the char *buffer variable,
    the char *filePtr then points to what char *buffer is pointing to.

    For some reason I just cannot seem to access the file data after the function ends.

    char *copydatatohere;
    fileRead(copydatatohere,"myfile.txt");
    printf("%s",copydatatohere); <------------copydatatohere points to nothing.????! why?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void displayUsage(){
    	printf("-----------------------------\n");
    	printf("Usuage: bwPercentage input.bmp\n");
    	printf("-----------------------------\n");
    }
    
    void fileRead(char *filePtr,const char *filename){
    	FILE * pFile;
    	long lSize;
    	size_t result;
    	char *buffer;
    
    	pFile = fopen ( filename, "rb" );
      	if (pFile==NULL) {
    		fputs ("File error",stderr); 
    		exit (1);
    	}
    
      	fseek (pFile , 0 , SEEK_END);
      	lSize = ftell (pFile);
      	rewind (pFile);
    
      	buffer = (char*) malloc (sizeof(char)*lSize);
      	if (buffer == NULL) {
    		fputs ("Memory error",stderr); 
    		exit (2);
    	}
    
      	result = fread (buffer,1,lSize,pFile);
      	if (result != lSize) {
    		fputs ("Reading error",stderr); 
    		exit (3);
    	}
      	fclose (pFile);
    	filePtr = buffer;
    	//printf("%s",filePtr); WORKS?!
    	//why doesn't the char *data variable point to the buffer after this function finishes?
    	}
    
    
    
    int main(int argc, char **argv)
    {
    	char *filename;
            if(argc == 2){
    		filename = argv[1];
    	}
    	else{
    		displayUsage();
    		exit(EXIT_FAILURE);	
    	}
    	char *data;
    	fileRead(data,filename); //does not transfer into char *data
    	printf("%s",data); //prints nothing but an address
            exit(EXIT_SUCCESS);
    }

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Not working because you are only passing a random adress to your function instead of passing an adress of a char *.

    Code:
    //...
    
    void fileRead(char **filePtr,const char *filename){
    	FILE * pFile;
    	long lSize;
    	size_t result;
    	char *buffer;
    
    	pFile = fopen ( filename, "rb" );
      	if (pFile==NULL) {
    		fputs ("File error",stderr); 
    		exit (1);
    	}
    
      	fseek (pFile , 0 , SEEK_END);
      	lSize = ftell (pFile);
      	rewind (pFile);
    
      	buffer = (char*) malloc (sizeof(char)*lSize);
      	if (buffer == NULL) {
    		fputs ("Memory error",stderr); 
    		exit (2);
    	}
    
      	result = fread (buffer,1,lSize,pFile);
      	if (result != lSize) {
    		fputs ("Reading error",stderr); 
    		exit (3);
    	}
      	fclose (pFile);
    	*filePtr = buffer;
    	//printf("%s",filePtr); WORKS?!
    	//why doesn't the char *data variable point to the buffer after this function finishes?
    }
    
    
    
    int main(int argc, char **argv)
    {
    	char *filename;
            if(argc == 2){
    		filename = argv[1];
    	}
    	else{
    		displayUsage();
    		exit(EXIT_FAILURE);	
    	}
    	char *data;
    	fileRead(&data,filename); //does not transfer into char *data
    	printf("%s",data); //prints nothing but an address
            exit(EXIT_SUCCESS);
    }
    And careful with this, you are reading your file as binary but you are printing it as a string, which is not really semantically good (also, fread() doesn't append a '\0' character...). Anyway, be aware of this.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You are passing by value! The original function's variable is copied to the called function, thus the original is unmodified. What you need to do is take a pointer to the variable that should hold the address to the buffer. Therefore it needs to be a pointer to a pointer, char**.
    I also suggest you take a look at http://cpwiki.sourceforge.net/User:E...cket_placement
    Because
    Code:
    void foo() {
    	}
    Isn't correct bracket placement.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    It works =D.
    Well, I still don't understand this whole pointer ordeal, but
    thank you very much, I appreciate the help foxman.

    I will definitely work on those brackets, Elysia.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    OK, so let me explain a little further on the pointer ordeal.
    You have a pointer to a buffer. That variable is char*, OK?
    The char* holds an address to the buffer. It does not contain the actual buffer. This is why it's called a pointer. It contains an address that points to something.
    So you want a function to modify a variable of type char. You must pass the address of that char variable, right? So that means a pointer to a char variable, hence char*, basically the type (char) plus a star (*) to make it a pointer.
    Now, what if you wanted to pass a char pointer (char*) to a function which can modify it, how would we do now? If we want to modify the buffer, we pass the address to the buffer, right? So that means the function accepts a pointer to a buffer, char*.
    But if you want to modify the actual variable, then you must pass the address of that variable, right? So the type is char* and we must make a pointer to that variable, so it becomes char**, since type = char* and pointer = *.
    Basically it's a pointer to char*.
    Does that make sense?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I would like to add that if you have some problems understanding why you need a char** as the first argument of your function, well, you could rewrite your function for something more "natural"

    Code:
    //...
    
    char* fileRead(const char *filename){
    	FILE * pFile;
    	long lSize;
    	size_t result;
    	char *buffer;
    
    	//...your original code minus the line "filePtr = buffer;"
    
    	return buffer;
    }
    
    int main(int argc, char **argv)
    {
    	//...
    	char *data;
    	data = fileRead(filename);
    }
    You are interested in where buffer points to. When you originally wrote "filePtr = buffer;" in your fileRead function, well, that's exactly like declaring a local char* variable in the fileRead function and doing "thisCharPtrVar = buffer;". That's not making any change outside the function.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    I would just see it as that you are taking a pointer to a variable defined in the calling function.
    Since the type of that variable is char*, that makes a pointer look like char**. Pointer = type + *. Type = char*, so char* + * = char**.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 02:29 PM
  2. passing pointers in nested functions
    By tabstop in forum C Programming
    Replies: 6
    Last Post: 12-04-2008, 03:49 AM
  3. Passing lists to functions
    By Griever in forum C Programming
    Replies: 23
    Last Post: 11-14-2008, 05:26 PM
  4. Passing Pointers to Functions question..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 10-18-2008, 04:51 PM
  5. Replies: 6
    Last Post: 05-06-2003, 04:08 PM

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