C 如何在内存位置获取对象?

C 如何在内存位置获取对象?,c,arrays,memory,pointers,struct,C,Arrays,Memory,Pointers,Struct,如果某个对象的内存位置存储在一个整数中,那么如何将该对象存储在内存位置 我需要如何使用它: #include <stdio.h> #include "CelGL.h" /* Stride: The size of the data structure containing the per-vertex data Offset: Offset within the structure to find this data */ // Example structs that s

如果某个对象的内存位置存储在一个整数中,那么如何将该对象存储在内存位置

我需要如何使用它:

#include <stdio.h>
#include "CelGL.h"

/*
 Stride: The size of the data structure containing the per-vertex data
 Offset: Offset within the structure to find this data
 */

// Example structs that store the data
typedef struct {
    float x;
    float y;
    float z;
} Vertex;

typedef struct {
    float x;
    float y;
    float z;
} Normal;

typedef struct {
    float r;
    float g;
    float b;
} Color;

// Info for rendering
typedef struct {
    int vertex;
    int vertexStride;
    int vertexOffset;
    int normal;
    int normalStride;
    int normalOffset;
    int index;
} RenderData;


RenderData _renderData;
int _buffer; // Memory location of array with data

// sets the integer to the memory location of the data
void celBindBuffer(int *buffer)
{
    _buffer = &buffer; // Alert:Incompatible pointer to integer conversion assigning to 'int' from 'int **'
}

void celVertexAttribPointer(CelVertexAttrib attrib, int stride/*bytes*/, int offset/*bytes*/)
{
    switch (attrib) {
        case CelVertexAttribPosition:
            _renderData.vertex = _buffer;
            _renderData.vertexStride = stride;
            _renderData.vertexOffset = offset;
            break;
        case CelVertexAttribNormal:
            _renderData.normal = _buffer;
            _renderData.normalStride = stride;
            _renderData.normalOffset = offset;
        default:
            break;
    }
}

void printVertex(Vertex v)
{
    printf("Vertex[%f,%f,%f]\n", v.x, v.y, v.z);
}

void testPrint(int size/*size in bytes*/)
{
    for (int i = 0; i < size; i++) {
        // Gets the initial location of the data in which it is stored, gets the size of the struct and multiplies it by the index and then offsets to the 
        Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset); // How can I do this?
        printVertex(v);
    }
}
指针(
int*buffer
)是包含内存地址的变量。在指针指向的地址读取内存称为取消对指针的引用。实际上,您所做的是获取指针本身的地址。因此,与此相反:

\u buffer=&buffer

。。必须取消对指针的引用才能读取该
缓冲区所指向的值
指针:

\u buffer=*buffer

我建议你们读一读这篇文章,它可以帮助你们稍微了解一下这些事情

希望能有帮助。祝你好运

更新:

在您的情况下,最好使用与传入的对象类型相同的指针。。否则,它就相当混乱。下面是使用该指针的示例:

#include <stdio.h>

typedef struct {
    double x;
    double y;
    double z;
} Vertex; /* Just to make a simple code compile.. */
typedef Vertex Normal; /* Same story... */

typedef struct {
    Vertex position;
    Normal normal;
} VertexData;

static const VertexData mesh[] = {
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {-0.000000, -0.986528, -0.475087}, {0.114078, -0.863674, -0.490890} },
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} },
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} }
};

void celBindBuffer(const VertexData *data)
{
    const Vertex *v0 = &data[0].position;
    const Normal *n0 = &data[0].normal;

    const Vertex *v1 = &data[1].position;
    const Normal *n1 = &data[1].normal;

    printf("Vertex#1: %f/%f/%f...\n", v0->x, v0->y, v0->z);
    printf("Vertex#2: %f/%f/%f...\n", v1->x, v1->y, v1->z);
}

int main(void)
{
    celBindBuffer(mesh);
    return 0;
}
#包括
类型定义结构{
双x;
双y;
双z;
}顶点;/*只是为了让一个简单的代码编译*/
typedef顶点法线;/*同样的故事*/
类型定义结构{
顶点位置;
正常正常;
}顶点数据;
静态常量顶点数据网格[]={
{ {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
{ {-0.000000, -0.986528, -0.475087}, {0.114078, -0.863674, -0.490890} },
{ {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
{ {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
{ {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} },
{ {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} },
{ {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} },
{ {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} }
};
void celBindBuffer(const VertexData*data)
{
常量顶点*v0=&数据[0]。位置;
常量Normal*n0=&数据[0]。Normal;
常量顶点*v1=&数据[1]。位置;
常量Normal*n1=&数据[1]。Normal;
printf(“顶点#1:%f/%f/%f...n”,v0->x,v0->y,v0->z);
printf(“顶点#2:%f/%f/%f...n”,v1->x,v1->y,v1->z);
}
内部主(空)
{
缓冲区(网格);
返回0;
}
正如你所看到的,一点点的工作。 但是,您的函数有一点问题-您试图做的是传递
VertexData
对象数组。但问题是函数
celBindBuffer()
不知道该数组中有多少个元素。。。必须向该函数传递一个数字,以告知向量中有多少元素。否则,您很容易遇到。

线路

_buffer = &buffer;
正在尝试将变量
缓冲区
(恰好是指针)的地址分配到整数变量
\u buffer
。换句话说,它将类型“指针到指针”赋值到一个整数变量中。(
&
操作符的意思是“的地址”)

_buffer = *buffer;

它将
缓冲区
中地址处的内容(将是整数)分配给整数
\u buffer
*
运算符表示“此地址的内容”。

变量缓冲区是一个结构数组,而不仅仅是一个值,因此我需要它的内存位置。一旦我有了它,我如何在那个位置接收对象(线:顶点v=(顶点)(_renderData.Vertex+I*_renderData.vertexStride+_renderData.vertexOffset))?所以要获得数据的位置,我会使用_buffer=(&)buffer吗;?Stas,如果不了解传递给
celBindBuffer
函数的确切内容,很难回答您的问题。。但问题是,
buffer
变量是数据的一个位置。要得到它,你不需要做任何事情。如果您想从那里读取值,只要它是POD类型,您就可以取消对它的引用,甚至可以使用指针算术来移动
缓冲区所指向的多个元素。否则,您可以使用
memcpy
复制内存区域。我想。。。你必须进一步澄清你的问题,以得到更好的答案……我添加了正在传递到method@stas:刚刚更新了我的答案。希望能有帮助。我真的认为花几个小时阅读一些关于C语言中指针和指针算法的教程是值得的。这会给你很大的回报。我现在得睡觉了。。快到星期一了,明天我得早点上班:)祝你好运!请问是什么原因使您将
\u buffer
Renderdata.vertex
声明为
int
而不是
int*
?@tia:因为我不想使用常规指针。我需要将数据的字节位置存储在内存中,以便手动控制更改的字节数。例如,如果一个I有一个指向int数组的指针,那么指针+1=指针+3字节。如果我有一个存储内存位置的int,那么指针+1=指针+1字节。那么使用
char*
void*
如何?
_buffer = *buffer;