是否可以更改wchar_t *的内容?

时间:2017-05-12 21:12:33

标签: c++ string wchar-t

我有一个函数接收名为wchar_t*的{​​{1}}参数。

我希望调整somestring的大小以包含比传递更多的元素,b因为我想要复制比原始字符串更大的元素:

wchar_t* somestring

是否可以调整void a_function(wchar_t *somestring) { // I'd like the somestring could contain the following string: somestring = L"0123456789"; // this doesn't work bebause it's another pointer } int _tmain(int argc, _TCHAR* argv[]) { wchar_t *somestring = L"0123"; a_function(somestring); // now I'd like the somestring could have another string at the same somestring variable, it should have the following characters: "0123456789" } 的大小以包含更多字符?

1 个答案:

答案 0 :(得分:2)

正如您所知,sumup只是更改了本地指针变量sum而对调用者没有任何影响。更改调用者的指针值需要传递指向此指针的指针:

int sum( int hello[], int size )
{
  int result = 0;
  for ( int i = 0; i < size; ++i )
    result += hello[i];
  return result;
}

int main( )
{
  ...
  int sumup = sum( hello, size );
  ...
}

请注意,通过覆盖指针,您将松开对字符串文字public class DBConnectorFactory { private static volatile DBConnector dBConnector = null; private static AtomicReference<DBConnector> atomicReference = new AtomicReference<>(); private DBConnectorFactory() {} public static DBConnector getDBConnector(DBConfig dBConfig) { if(dBConnector == null) { if(atomicReference.compareAndSet(null,new DBConnector(dBConfig))) dBConnector = atomicReference.get(); return atomicReference.get(); } else return dBConnector; } 的引用。 请进一步注意,数据类型现在为pkg-config --cflags gtk+3.0 ,否则不应分配字符串文字(不允许修改字符串文字的内容)。