标签: pointers

通过引用传递const值

假设我有这个程序:

const int width = 4;

void test(int&){}

int main() {
    test(width);
}
Run Code Online (Sandbox Code Playgroud)

这将无法编译.我注意到名称(例如宽度)的常量值(也是枚举常量)不能通过引用传递.为什么会这样?

c++ pointers reference constants

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

C/C++ - 整数与指针冲突

我一直在研究一个小型的库集合,在我的n维几何向量模板类中,我遇到了两个构造函数之间的问题.构造函数VectorN( t data[n] )与构造函数冲突VectorN( t value ),我得到错误:

More than one instance of constructor ___ matches the argument list".
Run Code Online (Sandbox Code Playgroud)

我理解为什么会发生这种情况,但解决方案使我无法理解.该问题仅在我尝试使用实例化类时发生VectorN(0),但是当value0以外的任何值使用正确的构造函数时.我该怎么做才能解决这个问题?

c++ int pointers

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

为什么"a-> content"给我一个地址而不是一个值?

现在我使用gm:s引擎制作了几年的游戏(我向你保证我不会使用拖放的新手,就像往常一样),我决定开始学习使用c ++本身,你知道扩展我的知识和所有好东西= D.

在这样做时,我一直在尝试将列表类作为练习项目,你知道,有一组节点链接在一起,然后循环抛出这些节点来获取索引的值,这里是我的代码,我因为代码有一个我很难理解的重大问题

template<class type>
class ListNode
{
    public:
        type content;
        ListNode<type>* next;
        ListNode<type>* prev;
        ListNode(type content) : content(content), next(NULL), prev(NULL) {}
    protected:
    private:
};
template<class type>
class List
{
    public:
        List() : SIZE(0), start(NULL), last(NULL) {}
        unsigned int Add(type value)
        {
            if (this->SIZE == 0)
            {
                ListNode<type> a(value);
                this->start = &a;
                this->last = &a;
            }
            else
            {
                ListNode<type> a(value);
                this->last->next = &a;
                a.prev = this->last;
                this->last = &a;
            }
            this->SIZE++;
            return (this->SIZE - 1);
        }
        type Find(unsigned int …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

const指针合约只有热空气?

我目前正在开发一个项目,包括使用void指针的一些通用的链表实现.为这些列表提供了一些非常好的函数,我决定只使用元素的识别函数(const void*).在添加const关键字之后是必要的,我想到了我的代码现在是多么正确(如果我实现了之前的所有内容).

由于编译器(GCC)没有警告我,我决定接受测试.我使用"gcc -g -Wall test.c"编译了以下代码,并且没有收到GCC的任何警告.

#include <stdio.h>
#include <stdlib.h>

void testf(const void *testp){
    *((int32_t *) testp) += 1;
}

void testf2(const int32_t *testp){
    *((int32_t *) testp) += 1;
}

int main(){
    int32_t testv = 0;
    printf("%i \n", testv);
    testf(&testv);
    printf("%i \n", testv);
    testf2(&testv);
    printf("%i \n", testv);
    return 0;
} 
Run Code Online (Sandbox Code Playgroud)

输出如下:

0 
1 
2 
Run Code Online (Sandbox Code Playgroud)

我没想到C会因此而崩溃,但我希望编译器收到警告.在这个例子中我只是强制转换,在我的实际函数中,我也将const void指针赋给tmp变量.

这是一个错误吗?

鉴于今天的编译器有多复杂,我至少期待一个警告,即我正在向非const指针投射指针.如果我更改了强制转换并在那里添加了const关键字,GCC会抛出我尝试分配给只读位置的常见错误

我是否应该重新考虑我对表示const指针的函数的信任?这不是我所理解的合同:)

c methods gcc pointers const

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

在C中重用内存

我在使用和理解C中的free()函数时遇到了问题.

我尝试编写这个例子以继续重用指针,但我不明白为什么会出现错误:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


typedef struct box
{
    char message[20];
}box;


box *newBox()
{
    box *inBox;
    inBox = (box *) malloc(sizeof(box));

    return inBox;
}


int main()
{
    box *temp = NULL;


    temp = newBox();
    strcpy(temp->message,  "Hello");
    printf("%s\n", temp->message);

    free(temp);

    strcpy(temp->message,  "World");
    printf("%s\n", temp->message);

    free(temp);

    strcpy(temp->message,  "People");
    printf("%s\n", temp->message);

    return 0;   

}
Run Code Online (Sandbox Code Playgroud)

输出是:

Hello
World
memory(531,0x7fff77e9e300) malloc: *** error for object 0x7fe8ba404a90: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort …
Run Code Online (Sandbox Code Playgroud)

c memory pointers

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

如何使用指针从函数传递堆上的对象地址

我正在尝试在堆上创建一个对象,然后从调用函数传回它的地址,但我无法让它工作!

如果从main调用此函数,为什么我不能将对象的地址存储在新指针中?

Feline newFeline(int height, int weight) {
  Feline *myFeline = new Feline(height, weight);
  return *myFeline;
}

int main()
{
  Feline *f2;
  *f2 = newFeline(10, 100);
  //cout << f2->getHeight() << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到一个总线错误:10.哦,我喜欢猫.

c++ pointers

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

在C中传递结构

当我将struct指针传递给函数时,我希望在函数结束后保留​​对指针所做的更改.我不知道我做错了什么.

void webSocketHandler(struct libwebsocket_context* context)
{    
    /* some code */
    context = libwebsocket_create_context(&info);
}

sint32 s32_WSER_Init(sint32* configDataPoolIndexes, struct libwebsocket_context* context)
{
   /* some code */
  webSocketHandler(configDataPoolIndexes, context);
}
Run Code Online (Sandbox Code Playgroud)

问题是在创建它之后context不在NULL内部webSocketHandler,但如果我s32_WSER_Init在调用处理程序后尝试在内部使用它,我会得到NULL.

提前致谢.

c pointers structure function

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

指针增量如何工作

int main(void)
{
    int n1 = 2, n2 = 5;
    int *p = &n1, *q = &n2;
    *p = *(q++);
    printf("%d,%d", *p, *q);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出= 5,5

为什么值为*q5它应该有一些垃圾值?

int main(void)

{

    int n1 = 2, n2 = 5;

    int *p = &n1, *q = &n2;

    *p = *(++q);

    printf("%d,%d", *p, *q);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

输出= 2,2

这是怎么回事?任何人都可以解释优先规则如何在指针中工作?

c pointers

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

c ++使用指针访问对象的成员

我有变量mode,我用以下行声明:

StatusRecord mode;
Run Code Online (Sandbox Code Playgroud)

StatusRecord是一个struct包含不同类型的几个变量的.

我现在想要创建一个指针mode,并使用一个函数填充其属性/字段,用一些数据填充该指针.我试过这样做如下:

StatusRecord mode;
StatusRecord *modePtr = &mode;
DataStore->getModeData(*modePtr);
Run Code Online (Sandbox Code Playgroud)

在这里,我声明struct变量,创建一个指向它的指针,并使用该getModeData()函数填充该指针.但是,我现在想要使用struct ptr我刚刚填充在条件语句中的属性:

if(*modePtr->eraseSelect ==2){
    ...
}
Run Code Online (Sandbox Code Playgroud)

但我在这一行上遇到编译错误:

错误:'*'的操作数必须是指针

这是否意味着eraseSelect属性应该是指针以及'modePtr`?我该如何解决这个错误?

c++ struct pointers

0
推荐指数
3
解决办法
1485
查看次数

当我们取消引用FILE指针时会发生什么?

假设我有一个文件指针

FILE* infile = fopen("<somefilepath", "r");
Run Code Online (Sandbox Code Playgroud)

现在,当我取消引用gdb中的文件指针时,我得到

打印* infile

?$ 2 = {_flags = -72539000,_IO_read_ptr =为0x0,_IO_read_end =为0x0,
?_IO_read_base =为0x0,_IO_write_base =为0x0,_IO_write_ptr =为0x0,
?_IO_write_end =为0x0,_IO_buf_base =为0x0,_IO_buf_end =为0x0,
?_IO_save_base = 0x0,_IO_backup_base = 0x0,_IO_save_end = 0x0,_markers = 0x0 、
? _chain = 0x7ffff7dd41c0 <_IO_2_1_stderr _>,_ fileno = 3,_flags2 = 0 ,
?_old_offset = 0,_cur_column = 0,_vtable_offset = 0'\ 000',_ shortbuf =“”,
?_lock = 0x6020f0,_offset = -1,__pad1 =为0x0,__pad2 = 0x602100,__pad3 =为0x0,
?__pad4 = 0x0,__ pad5 = 0,_mode = 0,_unused2 ='\ 000'}

有人可以帮助我理解这是什么意思吗?

c pointers

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

标签 统计

pointers ×10

c ×5

c++ ×5

const ×1

constants ×1

function ×1

gcc ×1

int ×1

memory ×1

methods ×1

reference ×1

struct ×1

structure ×1