10 Replies - 1330 Views - Last Post: 28 June 2011 - 06:20 AM Rate Topic: -----

#1 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

how to copy a function to another function in c language

Posted 27 June 2011 - 12:07 AM

how to copy a function from file1.c file to another file file2.c in c language?


file1.c

{
 FILE *p;
 int s;
 // code for reading value of s---------
 p=fopen("file2.c",a+); // file2.c created with append mode
 switch(s)
   {
    case 1:
           printf("function f1 called");

           //below is showing an error... i knw its not the proper way
           fwrite(p,"func1()");  // "here i want to copy function func1 to another file...on selecting case1" 

    //other cases etc
   }

fclose(p);
getch();
}


func1()
 {

//body

 }





I want to copy the whole function func1() to another file file2.c by using file handling.........or an other way......



plz helpppppppppppppppppppppppppppppppppppppppppp

Is This A Good Question/Topic? 0
  • +

Replies To: how to copy a function to another function in c language

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 05:14 AM

Moved to C/C++ Help. Please reserve the C++ Programmers forum for topical, non-help related discussion.
Was This Post Helpful? 0
  • +
  • -

#3 Electron_Volt  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 30-November 09

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 05:28 AM

If you want to copy a fuction from one .c file to another I suggest copy/paste.
Was This Post Helpful? 0
  • +
  • -

#4 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 08:40 AM

View PostElectron_Volt, on 27 June 2011 - 07:28 AM, said:

If you want to copy a fuction from one .c file to another I suggest copy/paste.


lol


----------------------------


So you are trying to copy a function from one "*.c" file to another "*.c" file.

int a() {

}
int b() {

}
----------------->
int c(){

}
-----------------> copy "int c()" and paste it to destination "*.c" file



it's not going to be an easy task, since the program has to recognize and extract a function from the source file. (which is easier said than done). Are you sure you want to do this before becoming firm with more basics?--or is this an assignment--or have I understood your question incorrectly?
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 10:07 AM

Well if I understand this correctly: You want to write a program that will copy a function (declaration/definition) from one file to another.

Well to do this you will probably need to write at least a basic parser. specifically you will need to be able to recognize a function signature:

<type> name(<arguements>){

then you need to be able to know when the body starts and ends. Since the function may have multiple blocks "{ }" you will have to count opening brackets "{" and then closing brackets "}"

This is not a hard thing to do but it requires that you either use a utility like YACC (which can have a steep learning curve) OR you need to write a parser yourself. I would probably do the latter.
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 10:36 AM

recursive decent works well for this sort of thing. in this case i would relay on the fact that there is a limited number of statements that can occur outside of a function. another thing you work off of is the fact that all statements have a delimiter(s) of some sort. lets look at some of the delimiters you will have to deal with in order to do this.

'#' -- this is pre-proc starter, you can apply the same rule for all pre-proc directives. ignore until new line or if there is a back space ignore the next line, until there is no back space.

'typedef' -- this is for a typedef, you can ignore this until you reach a semi-colon (however you will need to check for structs as well)

variable declaration/namespace decorations -- these come in all shapes in forms and will be your main concern so more on this a bit later

type declarations -- these are delimited by brackets {}, however becuase things with brackets can be in side of these declarations you will need to make sure and match the correct bracket.

i don't think i covered it all but i cover most of it.

now when you identify a type it can either be the type of a global variable or the return type of a function. to identify a function you need to read ahead a bit to see if it is a function. once you have found the '(' you know it's a function so you can copy the rest of over (again matching brackets of the function).
Was This Post Helpful? 0
  • +
  • -

#7 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 10:47 AM

View PostNickDMax, on 27 June 2011 - 12:07 PM, said:

Well to do this you will probably need to write at least a basic parser. specifically you will need to be able to recognize a function signature:

<type> name(<arguements>){


Yes.. so

1 -- supposing that <type> can be any single word, (because return type can be a user-defined structure, not just char, int, string, etc.)
2 -- there's a <name>,
3 -- there's an opening and closing bracket "()",
4 -- there's an opening and closing "{}",

it will be a function.

But then, I think amit2011 wants to select which functions to copy, so then, it'll become more complicated.




Are you sure you want to do this?
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 11:33 AM

It really is not as complicated as it sounds -- In this thread I demonstrate a basic parser to parse out function calls -- from something like this it should not be too hard to use this kind of structure to parse out functions...
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1743
  • View blog
  • Posts: 3,360
  • Joined: 30-May 10

Re: how to copy a function to another function in c language

Posted 27 June 2011 - 12:09 PM

> or an other way......
That depends on why you're trying to do this.
It would certainly help if you could explain that much.

On one level, it looks like an extremely messy way of sharing common code.

Most people would put func1() into say common.c, then all you do is
gcc prog1.c common.c
gcc prog2.c common.c



It seems from your code that you were just trying to "point" to a function, and somehow scribble out the source code directly from the compiled binary. This simply isn't going to happen in any nice way.
Was This Post Helpful? 0
  • +
  • -

#10 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: how to copy a function to another function in c language

Posted 28 June 2011 - 12:01 AM

View Postsungchoiok, on 27 June 2011 - 10:47 AM, said:

View PostNickDMax, on 27 June 2011 - 12:07 PM, said:

Well to do this you will probably need to write at least a basic parser. specifically you will need to be able to recognize a function signature:

<type> name(<arguements>){


Yes.. so

1 -- supposing that <type> can be any single word, (because return type can be a user-defined structure, not just char, int, string, etc.)
2 -- there's a <name>,
3 -- there's an opening and closing bracket "()",
4 -- there's an opening and closing "{}",

it will be a function.

But then, I think amit2011 wants to select which functions to copy, so then, it'll become more complicated.




Are you sure you want to do this?





YES SIR, ................if case 1 selected then copy the whole function1 to new file filenew.c
if case 2 selected then copy the whole function2 to new file filenew.c
help sirrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr..............


replies r haunting me....

ok..................this seems very difficult....after looking at ur replies...........



..2nd option...if u all can help...
calling same function func1 in two differnt .c files............I think this can solve my prblm.............


am i right????
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to copy a function to another function in c language

Posted 28 June 2011 - 06:20 AM

The problem of code reuse (Calling the same function from multiple .c files) has been tackled and we have a solution: Object files and linkers.

Basically the say compiling a program works is that you pass some file names to the compiler lets say: file1.c file2.c

the compiler take file1.c and passes it to the pre-compiler. The precompiler will paste into file1.c any files that have been #included (for for example #include "myHeader.h") will cause the file myHEader.h to be pasted into file1.c -- this is an important step.

Now you would think that you would want to put your shared function into myHeader.h but no. If you do that then if you ever #include the myHeader twice in one program (even two different files) then you will have errors, generally we don't #include code - we only #include type declarations ( structs, classes, unions, enums, typedefs), function declarations, global data declarations, and macros, and preprocessor logic -- in C++ you also #include template code but that is more of an advanced topic.

Once the preprocessor is done it passes the, now larger file1.c + myHeader.h + standard headers back to the compiler that compiles this larger unit into an object file file1.obj (or .o).

Now the compiler does the same thing for file2.c -- the preprocessor pastes in myHeader.h and the compiler produces an object file file2.o -- file1.c and file2.c can call any "shared functions" that are declared in myHeader.h -- they can be defined (implemented) in either file1.c or file2.c but generally it is just best if you put all "reusable" functions into one file (say file2.c)

THEN the compiler passes file1.obj and file2.obj to the linker which stitches it all together with the libraries available to make file1.exe.

Example:

myHeader.h:
#ifndef MY_HEADER_H
#define MY_HEADER_H

//the above preprocessor lines ensure that the same code is not #included more than once


//by declaring things in the header file we can tell the compiler how to call functions
//   that are in a different object file -- the linker will stitch things together for
//   us so the code gets called.

int sharedFunction(const char* arg);

#endif


file1.c:
#include <stdio.h>
#include "myheader.h"

//functions that are declared only in .c files are not really sharable 
//   (not really 100% true but that is a story for another time).
int not_a_Shared_function(int value);


int main() {
    int slength = sharedFunction("How much space do I need for this string?");
    int arrayLength = not_a_Shared_function(slength);
    printf("array length = %d\n", arrayLength);
    return 0;
}

int not_a_Shared_function(int value) {
    return value + 1;
}



file2.c:
#include "myheader.h"

//Here I want to place all of my shared code that I declared in myheader.h

int sharedFunction(const char* arg) {
    const char *endp = arg;
    while (*endp != 0x00) { endp++; }
    return endp - arg;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1