Calling a function from a different file

This is a discussion on Calling a function from a different file within the C Programming forums, part of the General Programming Boards category; Hello! I'm doing this homework of reversing an inputted string. However, I was just wondering, if I have two files, ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Calling a function from a different file

    Hello!

    I'm doing this homework of reversing an inputted string. However, I was just wondering, if I have two files, one main.c and the other tnirp.c, how do I run main.c so that it incorporates the function in tnirp.c?

    Here is the code for tnirp.c


    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void tnirp( char s[] ){
    
    	int length = strlen(s);
    	char s2[100];
    	int i=0;
    	int j=0;
    
    	for (i=length-1; i>=0;i=i-1){
    			s2[j]=s[i];
    			j=j+1;
            }
    
    	printf("%s\n",s2);
    
    	return;
    
    }
    and here is the code for main.c:



    Code:
    #include<stdio.h>
    #include<strings.h>
    
    void tnirp( char s[] );
    
    int main(void) {
      char s[100];
    
      printf("Enter string:\n");
      fgets(s, 100, stdin);
    
      tnirp(s);
    
      return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    How are you compiling/linking the project? The two source files need to each be compiled and then linked together to form the final executable. How you do this depends on your IDE/compiler/linker.

    BTW, you might want to make sure that you NULL terminate your s2 array in the tnirp function, you aren't doing that currently. Also, it would be nice to do a simple check in that function's loop that tests that j is less than 100 so that you don't overwrite memory if someone passes in a string that's more than 100 characters. fgets will extract the newline as well, if you want that processed by your function then you're fine, if not then you'll want to strip that character before you pass it to the tnirp function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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