Thread: connecting c and c++ functions

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    connecting c and c++ functions

    Hi,


    I have this function which is a part of an older lib. the thing is this function accepts char***, allocates the required memory and fills it up. However i need to accept this char*** as a 2D vector of strings, but I am having hard time makeing it work. Could anyone help me with this one.


    example:

    Code:
    C:
    
    function (char*** results){
    
    results = (char ***) malloc(sizeof(char **)*num);
    for (index=0; index<num; index++) {
       results[index] = (char **)malloc(sizeof(char*)*size);
       for (i=0; i<size; i++) {
           results[index][i] = (char *)malloc(sizeof(char)*x));
           // fill
       }
    }
    
    
    C++:
    
    vector<vector<string>> results;
    
    function(&results);
    cout << results[0][0]<< endl;
    thank you!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,478
    However i need to accept this char*** as a 2D vector of strings,
    Why?

    If you want to stick with std::string you would probably be better off rewriting the "fill" to handle the C++ std::string. Since you want to use a vector you won't need any of the malloc() calls, since the vector will manage it's own memory.

    If you for some reason need to stick with this function then I suggest you stick with the array of C-strings. If necessary after the function call you could convert the array of C-string to a vector<vector<string>> and then free all the associated memory.

    But without seeing more of the function I can't really tell you how to proceed.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 06-15-2011, 01:53 PM
  2. help| connecting with video
    By sdahan in forum C++ Programming
    Replies: 8
    Last Post: 08-09-2009, 10:45 AM
  3. Connecting With IP
    By Phanster in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2004, 08:01 PM
  4. Connecting to a FTP with C#???
    By gicio in forum C# Programming
    Replies: 3
    Last Post: 12-12-2002, 05:10 PM