标签: pointers

将多维数组转换为c ++中的指针

我有一个如下所示的程序:

double[4][4] startMatrix;
double[4][4] inverseMatrix;
initialize(startMatrix) //this puts the information I want in startMatrix
Run Code Online (Sandbox Code Playgroud)

我现在想要计算startMatrix的反转并将其放入inverseMatrix.我有一个用于此目的的库函数,其原型如下:

void MatrixInversion(double** A, int order, double** B)
Run Code Online (Sandbox Code Playgroud)

取A的倒数并将其放入B.问题是我需要知道如何将double [4] [4]转换为双**以赋予函数.我尝试过"明显的方式":

MatrixInversion((double**)startMatrix, 4, (double**)inverseMatrix))
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.这实际上是正确的方法吗?

c++ pointers multidimensional-array

14
推荐指数
2
解决办法
2万
查看次数

相当于'this'指针

this在VBA中是否有等效的指针,以便我可以将它传递给另一个模块?

vba pointers this

14
推荐指数
2
解决办法
9292
查看次数

如何将指针值转换为QString?

出于调试目的,我经常将指针值(大多数this)输出到qDebug:

qDebug("pointer of current object = 0x%08x",this);,使用"%08x"作为格式字符串,只是this作为参数传递.

如何将指针值转换为QString?

这是我到目前为止所得到的:

char p = (char)this;
return QString("0x%1").arg(p, 8, '0');
Run Code Online (Sandbox Code Playgroud)

但编译器似乎并没有弄清楚如何处理该值.char在这种情况下铸造是否正确?或者更安全的方法是什么?

使用Visual C++和Qt 4.7.4.

编辑

qulonglong按建议使用

qulonglong p = (qulonglong)this;
return QString("0x%1").arg(p, 8, '0');
Run Code Online (Sandbox Code Playgroud)

产生编译器错误消息(错误C2666).

c++ qstring qt pointers

14
推荐指数
3
解决办法
2万
查看次数

在p = new string [0]和p = new int [0]之后,为什么删除[] p时字符串版本会崩溃?

我有两个代码块new[]delete[]:

1)

#include <string>

int main()
{
  std::string *p = new std::string[0];
  delete[] p;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

2)在这种情况下,我只是std::string改为int

int main()
{
  int *p = new int[0];

  delete[] p;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

为什么第一个程序崩溃时出现以下消息(在linux环境中):

Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

但第二个程序运行良好没有任何错误?

编辑

编译: g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

我只是使用g++没有任何参数来编译它.

如果是编译器错误,它们是否会根据标准崩溃?

c++ pointers new-operator delete-operator

14
推荐指数
1
解决办法
341
查看次数

c - 不能取位域的地址

为什么不能取位域的地址?

如何制作指向位字段的指针?

这是代码......

struct bitfield {
    unsigned int a: 1;
    unsigned int b: 1;
    unsigned int c: 1;
    unsigned int d: 1;
};

int main(void)
{
    struct bitfield pipe = {
        .a = 1, .b = 0,
        .c = 0, .d = 0
    };
    printf("%d %d %d %d\n", pipe.a,
            pipe.b, pipe.c, pipe.d);
    printf("%p\n", &pipe.a); /* OPPS HERE */
    // error: cannot take address of bit-field ...
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers syntax-error memory-address bit-fields

14
推荐指数
3
解决办法
2万
查看次数

C++指针:更改内容而不更改地址?

MyCustomObject * object=new MyCustomObject();
Run Code Online (Sandbox Code Playgroud)

假设我的许多类都使用了对象指针,但突然间我想改变指针的内容而不改变地址.

我错误地认为object = new MyCustomObject()会给对象一个新的指针地址吗?我想要新对象而不更改指针地址(是的,我将确保清理旧对象).

c++ pointers

14
推荐指数
3
解决办法
1万
查看次数

指针和字符串C++

我在教自己C++,我对指针有点困惑(特别是在下面的源代码中).但首先,我继续向你展示我所知道的内容(然后将代码与此对比,因为我觉得好像存在一些矛盾).

我知道的:

int Age = 30;
int* pointer = &Age;

cout << "The location of variable Age is: " << pointer << endl;
cout << "The value stored in this location is: " << *pointer << endl;
Run Code Online (Sandbox Code Playgroud)

指针保存内存地址.使用间接(取消引用)运算符(*),可以访问存储在指针的内存位置的内容.在本书的代码中,我无法理解......

cout << "Enter your name: ";
string name;
getline(cin, name); //gets full line up to NULL terminating character

int CharsToAllocate = name.length() + 1; //calculates length of string input
                          //adds one onto it to adjust for NULL character
char* CopyOfName = new …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

14
推荐指数
3
解决办法
4万
查看次数

如何在该引用超出范围后检测ptr是否仍引用有效引用

我正在玩弄溪流一点点,无法理解下面的内容.

这里我们有一个基本的ostream ptr,设置为不同的输出流,无论是cout,cerr还是a file.

// ostream ptr
std::ostream* outstream;

// set output ostream
void setOutput(std::ostream & os)
{
  outstream = &os; 
}

// write message to ostream
void writeData(const std::string & msg)
{    
  *outstream << msg << '\n';
}

int main (int argc, char * const argv[]) 
{
  // init to std out
  setOutput(std::cout);
  writeData("message to cout");

  setOutput(std::cerr);
  writeData("message to cerr");

  std::ofstream fileout("test.txt", std::ofstream::out | std::ofstream::app);
  setOutput(fileout);
  writeData("message to file");
  //fileout.close();

  setOutput(std::cout);
  writeData("message2 to cout"); …
Run Code Online (Sandbox Code Playgroud)

c++ pointers iostream ostream

14
推荐指数
3
解决办法
980
查看次数

__func__ 指针的两个 constexpr 实例的差异仍然是 constexpr 吗?

这是有效的 C++ 吗?

int main() {
    constexpr auto sz = __func__ - __func__;
    return sz;
}
Run Code Online (Sandbox Code Playgroud)

GCC 和 MSVC 认为可以,Clang 认为不是:Compiler Explorer


所有编译器都同意这个是可以的:Compiler Explorer

int main() {
    constexpr auto p = __func__;
    constexpr auto p2 = p;
    constexpr auto sz = p2 - p;
    return sz;
}
Run Code Online (Sandbox Code Playgroud)

Clang 再次不喜欢这个,但其他人都可以:编译器资源管理器

int main() {
    constexpr auto p = __func__;
    constexpr auto p2 = __func__;
    constexpr auto sz = p2 - p;
    return sz;
}
Run Code Online (Sandbox Code Playgroud)

这里有什么?我认为对无关指针的算术是未定义的行为,但__func__返回相同的指针,不是吗?我不确定,所以我想我可以测试一下。如果我没记错的话, …

c++ pointers language-lawyer constexpr c++17

14
推荐指数
1
解决办法
361
查看次数

带有 std::map&lt;T*, U&gt; 的程序是否具有明确定义的行为?

比较指向不相关对象的指针具有未指定的结果。

这似乎表明该程序可能具有未定义的行为,至少因为我们不能保证键类型的严格弱排序:

#include <map>

int main()
{
    int x = 0, y = 1;
    bool arbitrary = false;

    std::map<int*, bool> m{
       {&x, arbitrary},
       {&y, arbitrary}
    };
}
Run Code Online (Sandbox Code Playgroud)

更一般地说,我们可以说带有指针键的映射是一个危险的*命题吗?或者有什么特别的规则我们可以依靠?

* 从学术上讲,即;实际上,我不知道主流实现实际上会在比较任意指针时引发地狱。

c++ sorting pointers language-lawyer strict-weak-ordering

14
推荐指数
2
解决办法
273
查看次数