Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting MYTYPE* to MYTYPE (impossible ?)

Status
Not open for further replies.

RealityVelJackson

Programmer
Aug 6, 2005
10
0
0
US
What can one do if they need to pass an ADT to a function and all they have is a pointer to the ADT ? For example:

Code:
int main(){

   MYTYPE* a = createInstance();
   
   //Call func: doSomething( ??? ) 
   /*  
     ...with a as the argument.  
     In other words, dereference the pointer to somehow
     expose the bare object *a in order to pass it    
     by-value.  I'm not sure if i've ever seen      
     someone write doSomething(*a)...
   */
 
   free(a);
   return 0;
}

MYTYPE* createInstance(){
   MYTYPE* p = malloc(sizeof(MYTYPE));
   if(p == NULL) exit(1);
   return p;
}

void doSomething(MYTYPE x){
   ...
}
 
There is no reason why you can't just call doSomething (*a). Its valid. Of course if you control the doSomething call, you may want to change it to match up so things are less confusing.
 
it happens all the time

Code:
void Remove_End_Space (char *text)
{
    int x;
    int length = strlen(text);
    
    for (x = length - 1; text [x] == ' '; x--)
    {
        if (text [x] == ' ')
            text [x] = NULL;
    }  
}

or

Code:
int FileCopy (char *source, char *target)
{
    /*
    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    */

    FILE *stream1;
    FILE *stream2;

    int ch;

    // open source file for reading.
    stream1 = fopen(source, "r+b");

    // create target file for writing.
    stream2 = fopen(target, "w+b");

    do
    {
        // read a char from the source file
        ch = fgetc(stream1);

        // if ch is not the end of file marker write
        // the character to the target file.
        if (ch != EOF)
            fputc(ch, stream2);

    }while (ch != EOF);

    fclose(stream1);
    fclose(stream2);

    // get the file handles for use by the file length function.
    int handle1 = open(source, O_RDONLY | O_BINARY);
    int handle2 = open(target, O_RDONLY | O_BINARY);

    // get the file length of both the files.
    long x = filelength(handle1);
    long y = filelength(handle2);

    close(handle1);
    close(handle2);

    // the return value is 0 if the file length is the same.
    if (x == y)
        return 0;
    // the return value is 1 if the file length is not the same.
    else
        return 1;
}

TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top