标签: pointers

如何判断星号的使用是否与指针有关?

很难说出星号与指针的关系.

以下是一些令我困惑的代码示例.

typedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159;  // dereference and access data member x.d_
(*p).d_ *= -1;    // another equivalent notation for accessing x.d_
Run Code Online (Sandbox Code Playgroud)

在第五行,我们有(*p).d_ *= -1;.为什么有两个星号?他们的立场意味着什么?

int x = 2;
int* p_x = &x;  // put the address of the x variable into the pointer p_x
*p_x = 4;       // change the memory at the address in p_x to be 4
assert(x == …
Run Code Online (Sandbox Code Playgroud)

c pointers

0
推荐指数
1
解决办法
133
查看次数

如何访问超出范围的变量?

class foo{
    vector<foo*>* queue;
    vector<int> pos;
    foo(vector<foo*>* queue, vector<int> pos){
        this->queue=queue;
        this->pos=pos;
    }
public:
    foo(vector<foo*>* queue){
        this->queue=queue;
    }
    void init(){
        vector<int> posNew = pos;
        //Create Binary Tree Children of the state FOO
        posNew.push_back(/* An Additional Value*/)
        foo newFoo(queue, posNew);
        queue->push_back(&newFoo);
    }//Here the variables newFoo and posNew are out of scope so they are deleted even from the queue
}

class bar{
    vector<foo*> queue; //Assume that queue has a root node added to it.
    bar(){
        for(unsigned int i=0; i<queue.size();i++){
            queue[i]->init();// Somewhere …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

0
推荐指数
1
解决办法
1518
查看次数

由双指针创建的C++ 2d数组

我想用指针和show创建2d数组然后显示它.我按增量指针(一个*)和列索引更改行.我的代码:

int ** tabl=new int *[4];
for (int i=0;i<10;i++)
    tabl[i]=new int [3];
int **ptab=tabl;//save first position of array

for (int i=0;i<4;i++)
{
    for (int j=0;j<3;j++)
    {
        *tabl[j]=rand()%10 +1;
        printf("%4d",*tabl[j]);
    }
    tabl++;
}
tabl=ptab;
cout<<endl<<endl<<endl;
for (int i=0;i<4;i++)
{
    for (int j=0;j<3;j++)
    {
        printf("%4d",*tabl[j]);
    }
    tabl++;
}
Run Code Online (Sandbox Code Playgroud)

输出:

   2   8   5   1  10   5   9   9   3   5   6   6

   2   1   9   1   9   5   9   5   6   5   6   6
Run Code Online (Sandbox Code Playgroud)

但是第二个循环中的一些数字与原始数字不同.为什么?

c++ arrays pointers

0
推荐指数
1
解决办法
47
查看次数

如何从方法中打印指针的值?

基本上我有两个函数,主要有一个指针指向c(可变内存地址).我的问题是为什么我不能从print函数调用指针并打印出字符串.另外,如果我有一个类文件并想从那里调用指针(不向print函数添加参数),该怎么办?

#include <iostream>
#include <string>
using namespace std;

static int print(){
    cout << *pointer;
}

int main() {
    string c = "Hello World!";
    string *pointer = new string;
    pointer = &c;
    print();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

0
推荐指数
1
解决办法
1280
查看次数

C++指针数组内存分配与普通数组

我正在玩C++,我意识到指针数组和常规数组之间存在显着差异.

char *myString1 = new char[1];
char myString2 [3];

myString1[0] = 'a';
myString1[1] = 'b';
myString1[2] = 'c';
myString1[3] = 'd';

myString2[0]='a';
myString2[1]='b';
myString2[2]='c';
myString2[3]='d';
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么myString1编译甚至用简单的for循环打印每个字符都没有问题,即使我只是初始化初始大小为1.

但是,myString2似乎给了我编译错误,因为我初始化了一个超出数组边界的值.

c++ arrays pointers out-of-memory

0
推荐指数
1
解决办法
101
查看次数

c ++函数返回错误的数组

我正在尝试创建一个getSortedRanks返回数组的函数.我从这个问题中复制了返回数组的格式.函数返回数组但是数组没有正确返回.

#include <stdlib.h>
#include <stdio.h>
#define familyMembers 4

int *getSortedRanks()
{
    int rankedMembers[familyMembers] = {3,4,2,1};
    return rankedMembers;
}

int main()
{
    int *sortedRanks = getSortedRanks();

    //print the returned array
    for(int i = 0; i < familyMembers; i ++)
    {
        cout << "ranked member is " << sortedRanks[i] << endl;
    }

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

当我运行它时,输出是:

ranked member is 3
ranked member is 0
ranked member is 0
ranked member is 2686744
Run Code Online (Sandbox Code Playgroud)

数组的第一个元素sortedRanks总是正确的,但其他元素则不正确.如何更正返回数组的方式?

c++ arrays pointers function

0
推荐指数
1
解决办法
65
查看次数

C++向量迭代错误

monster* monster1 = new monster("Frankenstein", "The Ugly One", BackYard);
Player* player1 = new Player("Corey", "The Chosen one", Atrium);
Player* player2 = new Player("Darth Vader", "The Evil One", Atrium);
vector<Agent*> agents;
agents.push_back(monster1);
agents.push_back(player1);
agents.push_back(player2);

while (true)
{
    vector<Agent*>::iterator it;

    for (it = agents.begin(); it < agents.end(); it++) {
        it->act();                                            // Error here
        if (it->act() == false)                               // Error here
            return 0;
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

成员引用基类型'Agent*'不是结构或联合.

我真的不明白为什么这不能导航向量.

c++ iteration pointers vector

0
推荐指数
1
解决办法
65
查看次数

指向标准C中的整数转换的指针

我在这个网站上已经阅读了几个关于这个主题的问题,但我仍然有疑问.

在标准C中,我们可以阅读:

6.3.2.3.p.5:整数可以转换为任何指针类型.[...]
6.3.2.3.p.6:任何指针类型都可以转换为整数类型.[...].

省略的文本(将放在括号[...]中)只讨论转换失败时可能发生的问题.

但是我的问题更为笼统:

  1. 我观察到整数数学数字是无限的一组值.
  2. C中的任何整数类型只能表示整数数学值的有限子集.
  3. 在第6.3.2.3.p5/p6段中,似乎标准C11假定"每个指针值都可以映射到整数数学值 ".
  4. 此外,编辑C11标准的方式似乎表明,只有当这个数学(或抽象)值无法以开发人员选择的预期整数类型表示时,无论出于何种原因,都是在操作失败时.

我的问题是:我的解释(3)是正确的吗?

另一方面,当我们定义一个大小为N的类型T的数组时, 由于我们可以对数组的位置进行整数运算, 因此很明显,至少在"本地"级别,在数组中, 我们有相应的数组内存块的 运算方式与从0N-1的数字集相同.



通过定义数组或分配的对象,我们可以确定,在C中,
内存地址可以被"本地"视为算术上等同于整数的子集.

然而,这种"局部"行为不足以得出
标准C假设存储器模型的结论,该存储器模型的存储器地址可被视为"仅一个且同一组整数"的一部分.

然而,6.3.2.3.p5/p6似乎"暗示"这个更强大的断言,尽管我并不完全确定.

另一个问题可能会带来更多亮点:

  • 是在标准C的可能性,即两个不同的有效指针开放p,q,(即:p != q)中,当转换成整数型,所得到的整数值变得相等.

c pointers c99 language-lawyer c11

0
推荐指数
1
解决办法
1118
查看次数

为什么我的if/else语句不能产生预期的结果?

我的程序有问题.我几乎已经完成了它,但最终结果并没有成功.我已经用两种方式编写了它,一种是带有switch语句,另一种是if else语句但是它们都没有用.它们都为我编译并具有干净的构建但不会按预期运行.

#include <iostream> 
#include <cstdio>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int main()
{

    int i = 0;
    const int size = 27;
    char newline = '\n';
    char *pnumber = 0;
    int selection = 0;

    cout << "PRINGING CONTENTS OF ARRAY" << endl;
    cout << "====================================================" << endl;
    char alphabet[size] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
    , 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

0
推荐指数
1
解决办法
95
查看次数

访问struct字段时Golang结构文字和指针之间的区别

在访问struct字段时,我不理解struct literal和struct指针之间的区别.有没有不同的内部行为?

type Person struct {
    Name string
}

p := &Person{Name: "Alice"}
u := Person{Name: "Bob"}

fmt.Println(p.Name)  // any difference ?
fmt.Println(u.Name)  // any difference ?
Run Code Online (Sandbox Code Playgroud)

我搜索了这个但发帖我发现所有解释了值和指针之间的区别,或者"传递值"与"传递指针"到方法.他们不是我想知道的.

struct pointers go

0
推荐指数
1
解决办法
595
查看次数

标签 统计

pointers ×10

c++ ×7

arrays ×4

c ×2

c11 ×1

c99 ×1

function ×1

go ×1

iteration ×1

language-lawyer ×1

out-of-memory ×1

struct ×1

vector ×1