C / Objective C 指针声明

发布于 2024-12-11 19:50:19 字数 991 浏览 0 评论 0原文

我有一个需要 float** 类型参数的函数,但我不知道如何声明和初始化该变量。

有人可以给我任何建议吗?更好的是,它是什么类型的变量?我已经研究了好几天了,所有搜索结果都是空的。

谢谢

编辑

这是有问题的函数

    - (OSStatus)getBuffersInner:(float**)buffers numberOfBuffers:(int)chnls samples:(int)samples
    {
        int i, j ;
        UInt32 frames ;

        // zero out requested buffers if client needs more channels than exist in the channel map
        for ( i = numberOfMappedChannels; i < chnls; i++ ) memset( buffers[i], 0, samples*sizeof( float ) ) ;

        //  place buffer pointers into bufferList, following the channelMap
        for ( i = 0; i < fileDescriptor.mChannelsPerFrame; i++ ) {
            j = inverseChannelMap[i] ;
            resampledBufferList.list.mBuffers[i].mData = ( j < 0 || j >= chnls ) ? zeros : buffers[j] ;
        }
        frames = samples ;
        return ExtAudioFileRead( audioFileRef, &frames, &resampledBufferList.list ) ;
    }

I have a function that expects a parameter of type float**, but I do not know how to declare and initialize this variable.

Can anyone give me any advice on this? Better yet, what type of variable is it? I have been looking into it for days now and any search results comes up empty.

Thanks

EDIT

Here is the function in question

    - (OSStatus)getBuffersInner:(float**)buffers numberOfBuffers:(int)chnls samples:(int)samples
    {
        int i, j ;
        UInt32 frames ;

        // zero out requested buffers if client needs more channels than exist in the channel map
        for ( i = numberOfMappedChannels; i < chnls; i++ ) memset( buffers[i], 0, samples*sizeof( float ) ) ;

        //  place buffer pointers into bufferList, following the channelMap
        for ( i = 0; i < fileDescriptor.mChannelsPerFrame; i++ ) {
            j = inverseChannelMap[i] ;
            resampledBufferList.list.mBuffers[i].mData = ( j < 0 || j >= chnls ) ? zeros : buffers[j] ;
        }
        frames = samples ;
        return ExtAudioFileRead( audioFileRef, &frames, &resampledBufferList.list ) ;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深者入戏 2024-12-18 19:50:19

这是一个指针到指针。以下是示例用法:

float f = 0.123;
float *pf = &f;
float **ppf = &pf;

cout << *pf << " == " << **ppf << endl;

您可能需要查看

This is a pointer-to-pointer. Here is a sample usage:

float f = 0.123;
float *pf = &f;
float **ppf = &pf;

cout << *pf << " == " << **ppf << endl;

You may want to look into this.

抚笙 2024-12-18 19:50:19

指针可以用于多种用途。在这种情况下,它可能用于动态内存和更改函数中的(指针)值。
通常情况下,它可能是:

void getFloat(float** fl)
{
  *fl = new float(1.2);
}

int main()
{
  float* fl;
  getFloat(&fl);
  std::cout << *fl;
  delete fl;
}

请注意,将参数作为要更改的指针传递比 C++ 更具 C 风格,尽管为了清晰起见,有些人更喜欢它而不是传递引用。
在 C++ 中也不鼓励对动态内存使用原始指针。

Pointers can be used for several things. In this case it is probably used for dynamic memory and for changing a (pointer-)value in the function.
So typically, it could be:

void getFloat(float** fl)
{
  *fl = new float(1.2);
}

int main()
{
  float* fl;
  getFloat(&fl);
  std::cout << *fl;
  delete fl;
}

Mind you that passing parameters as pointers to change is more C-style than C++, although some prefer it over passing references for clarity.
Using raw pointers for dynamic memory is also discouraged in C++.

苏别ゝ 2024-12-18 19:50:19
float **ppfloat = new float*;
*ppfloat = new float;
**ppfloat = 3.14;

delete *ppfloat; //actually deletes the float
delete ppfloat; //actually deletes the pointer to float

只是一个“指向浮动指针的指针”:只需将句子拆开即可。

float **ppfloat = new float*;
*ppfloat = new float;
**ppfloat = 3.14;

delete *ppfloat; //actually deletes the float
delete ppfloat; //actually deletes the pointer to float

Just a "pointer to a pointer to float": just break up the sentence.

网白 2024-12-18 19:50:19

在您的代码中,float** buffers 保存一个指向 float* 的指针向量。例如,在普通 C 中:

size_t numberOfBuffers = ?;
size_t bufferSize = ?;
unsigned i;

typedef float* buffer_t;

buffer_t* buffers = (buffer_t*)malloc(sizeof(buffer_t) * numberOfBuffers);

for(i=0; i<numberOfBuffers; ++i)
{
    buffers[i] = (float*)malloc(sizeof(float) * bufferSize);
}

In your code, float** buffers holds a vector of pointers to float*. For example, in plain C:

size_t numberOfBuffers = ?;
size_t bufferSize = ?;
unsigned i;

typedef float* buffer_t;

buffer_t* buffers = (buffer_t*)malloc(sizeof(buffer_t) * numberOfBuffers);

for(i=0; i<numberOfBuffers; ++i)
{
    buffers[i] = (float*)malloc(sizeof(float) * bufferSize);
}
不再让梦枯萎 2024-12-18 19:50:19

从您显示的代码来看,该函数似乎正在使用
float** 作为指向数组 [chnls] 的指针
浮动[
样本`]。在这种情况下,最好的解决方案可能是使用
类似于:

std::vector<float> data( chnls * samples );
//  Fill data...
std::vector<float*> args( chnls );
for ( int i = 0; i != args.size(); ++ i ) {
    args[i] = &data[0] + (samples * i);
}

并将 &args[0] 传递给函数。

(如果这看起来很复杂,事实确实如此。这就是我们付出的代价
必须与 C 接口。如果目标函数是 C++ 语言,则有
比使用 float** 更好的解决方案。)

From the code you show, it looks like the function is using the
float** as a pointer to an array[chnls] of pointers to
floats[
samples`]. In this case, the best solution is probably to use
something like:

std::vector<float> data( chnls * samples );
//  Fill data...
std::vector<float*> args( chnls );
for ( int i = 0; i != args.size(); ++ i ) {
    args[i] = &data[0] + (samples * i);
}

And pass &args[0] to the function.

(And if this seems complicated, it is. That's the price we pay for
having to interface C. If the target function is in C++, there are
much, much better solutions than using float**.)

豆芽 2024-12-18 19:50:19

它是 float[n][m] 矩阵。像 int main(int argc, char **argv) 之类的东西,其中 argv 是一个“字符串”数组,一个 char 数组的数组。

It is float[n][m] matrix. Something like int main(int argc, char **argv), where argv is an array of 'strings', an array of arrays of char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文