日期:2014-05-17  浏览次数:20830 次

向函数传递指针也不能改变值???
C/C++ code

#include <string>
#include <iostream>
#include <occi.h>
#pragma comment (lib, "oraocci11d.lib")
using namespace std;
int  SelectFrom (char *sqlexec);
int main ()
{
    char a[] = {"SELECT * FROM AAVARX"};
    int b = SelectFrom (a);
    cout<<a<<endl;
    system ("pause");
    return 0;
}
int  SelectFrom (char *sqlexec)
{
    oracle::occi::Environment *env = oracle::occi::Environment::createEnvironment ();
    oracle::occi::Connection *coon = env ->createConnection ("boss", "zzs880905", "shop");
    oracle::occi::Statement *stmt = coon ->createStatement (sqlexec);
    oracle::occi::ResultSet *res = stmt ->executeQuery ();
    res ->next ();
    sqlexec = (char *)malloc (strlen (res ->getString (1).c_str ()));
    char *temp = strcpy (sqlexec, res ->getString (1).c_str ());
    temp = strcat (sqlexec, res ->getString (2).c_str ());
    coon ->terminateStatement (stmt);
    env ->terminateConnection (coon);
    oracle::occi::Environment::terminateEnvironment (env);
    cout<<sqlexec<<endl;
    return 0;
}




------解决方案--------------------

sqlexec = (char *)malloc (strlen (res ->getString (1).c_str ()));
// 这里改变了指针的地址,如果想从函数内部传递到函数外部,则要使用:
int SelectFrom (char **sqlexec)

你也可以注释掉malloc那句,但要保证a[]空间足够,否则容易溢出。