gpt4 book ai didi

c - 任何类型 vector 的中间

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:13 26 4
gpt4 key购买 nike

我有以下数据结构:

typedef struct {
size_t d; /* dimension of elements*/
TFComp fid; TFComp ord; /* fid and ord are some comparation functions*/
char *v, *s, *t; /* v points to the starting point of vector; s points to end of usable area of vector; t points to the end of available area */
} TMultime;

和声明:

TMultime *m;

char *p=m->v; /* starting point */
char *q=m->s; /* end of usable area of vector */

假设 m->d = sizeof(int);

我想找到这个 vector 的中间点;

我试过 *middle = p + (q - p)/2 * m->d;

它失败了。它给了我一个甚至不在 vector 中的值;

我需要一些帮助才能做到这一点。

最佳答案

(让我们忽略可能的可移植性问题)

指向数组物理中间位置的指针是

char *m = p + (q-p)/2;

因为 p 和 q 是指向 char 的指针。但要注意:表达式计算的是 char 数组的中间部分!获得一个指向 block 大小中间的指针 m->d,表达式为:

char *m = p + ((q-p)/(2*m->d))*m->d;

关于c - 任何类型 vector 的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com