Thread: how can i get the result, which is a char array, from another *.cpp file?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how can i get the result, which is a char array, from another *.cpp file?

    the situation is....i have a MFC project and i have an edit box in it.

    i know how to put values to the edit box , but how can i get the result, which is a char array, from another *.cpp file?

    is there any restriction on naming the files?

    chould #include do the job?
    how should the specified *.cpp file return?(as it is a win32console application before, it return 0 at the end)

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    example.cpp:
    Code:
    #include <iostream>
    #include "function.h"
    
    using namespace std;
    
    int main()
    {
    	char* p = 0;
    
    	p=function();
    	cout<<p<<endl;
    	
    	return 0;
    }
    function.h:
    Code:
    char* function()
    {
    	char* ptext= new char[10];
    	ptext = "Hello World";
    	return ptext;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you, and can one .h file include two functions?

    thank you...seems have some clue now.....
    but can the function.h file be like this?

    Code:
    int add(int a,b)
    { 
          return a+b;
    }
    
    
    int f_call(int c, d)
    {
          int num1 = 1;
          int num2 = 2;
          int sum = add();
          return sum*2;
    }
    while in example.cpp, i just call the function f_call?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Sorry, I made a mistake. You really need .h files for function declarations and .cpp files for function definitions(see below).

    "but can the function.h file be like this?"

    Yes.

    To keep from having to define your functions in the order they're called, it's better to have a .h file with all the function declarations, and then a .cpp file with the definitions.
    myprogram.cpp
    ------------------
    #include "functions.h"


    functions.h
    ------------------
    //These can be in any order:
    char* function();
    void another_function();


    functions.cpp
    -----------------
    #include <iostream>//for cout
    #include "functions.h"
    Code:
    //The definitions can be in any order
    // since you have the .h declarations
    char* function()
    {
    	char* ptext= new char[10];
    	ptext = "Hello World";
    	return ptext;
    }
    
    void another_function();
    {
                  cout<<"Hello World";
    }
    Last edited by 7stud; 04-25-2003 at 10:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM