您如何设计一个函数来设置两个优选传递为参数的值? [英] How do you design a function to set two values preferebly passed as params?

查看:81
本文介绍了您如何设计一个函数来设置两个优选传递为参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2,提炼问题

您如何设计一个函数来设置两个可以开始和结束为null的值?我在另一个函数中,我希望有几个以null开头的局部指针,该函数将其作为参数,并在某些条件下将它们设置为指向对象,而在其他条件下将它们保留为null条件.

我一直在尝试使用指向指针的指针,但无法使其正常工作.有没有更好的方法?如果没有,我在做什么错?

带有修改的原始问题

我想要一个带有两个以null开头的指针参数的函数,如果该函数找到了它要查找的两个对象,则这两个指针将设置为找到的对象的地址,以供调用者使用.我想做到这一点的方法是使用指向指针参数的指针,但是我还没有使它起作用.

Update 2, refining question

How do you design a function to set two values which could start and end as null? I''m in another function where I''d like to have a couple of local pointers that would start as null, which a function would take as parameters, and set them to point to objects under certain conditions and leave them null in other conditions.

I''ve been trying to use pointers to pointers but I have not been able to make it work correctly. Is there a better approach? If not, what am I doing wrong?

Original Question with edits

I would like a function that takes two pointer parameters which begin as null and if the function finds the two objects it''s looking for the, the two pointers would be set to the addresses of the found objects for use by the caller. I''m guessing the way to do this would be to use pointer to pointer params but I haven''t gotten it to work.

void ConfigureStuff()
{
    CMyObject ** left = 0; // update, what should I be doing here?
    CMyObject ** right = 0;

    // update, recently tried this and it crashes here as well
    // so I must be doing something wrong above 
    *left = 0; // crashes
    *right = 0;

    FindLeftAndRight( left, right );


}

void FindLeftAndRight( CMyObject ** left, CMyObject ** right )
{
    CMyObject * searchObjectLeft = 0;
    CMyObject * searchObjectRight = 0;

    //...
    // code that finds left and right
    //...

    if ( bFoundObjects )
    {
        *left = searchObjectLeft;  // update, crashed here
        *right = searchObjectRight;
    }

}



我究竟做错了什么?得到我想要的最简单的方法是什么?

谢谢.



What am I doing wrong? What''s the simplest way to get what I want?

Thanks.

推荐答案

void ConfigureStuff()
{
    CMyObject * left = 0;
    CMyObject * right = 0;
    FindLeftAndRight( &left, &right );

}


我在做什么错了?
几乎所有内容.它甚至不会编译.

获得我想要的最简单的方法是什么?
您不想要最简单的方法".您想要正确的方法.这取决于您想要什么.从此代码尚不清楚.

您也没有报告您的问题.您如何提供未编译的代码?您不认为这是一个问题. 我尚未使它起作用"不能用作问题报告.

是的,您的双指针是修改内部指针并将其返回给调用方的一种方式,这没有错.由于某种原因,您为两个参数都返回了null指针(零),但是我想您应该在"//..."下对其进行计算.

不过,我看起来您还没有准备好进行C编码. 1)函数缺少返回类型(偶数void); 2)函数FindLeftAndRight在声明之前使用,因此无法编译;"声明之后,应该删除,因为在代码声明之后,代码块消失了; 3)function ConfigureStuff在名称后需要()"; 4)您需要定义CMyObject,如果您认为它是一个类(如MFC样式名称所示),请记住C中没有任何类; 4)变量bFoundObjects如果未声明/初始化; 5)您似乎缺少计算所需的重要输入和返回参数……我需要继续吗?..

—SA
What am I doing wrong?
Pretty much everything. It would not even compile.

What''s the simplest way to get what I want?
You don''t want the "simplest way". You want the right way. It depends on what you want. From this code, it is not clear.

You also did not report your problem. How come you supply the code which does not compile? You do not consider it a problem; "I haven''t gotten it to work" cannot be uses as an issue report.

Yes, your double pointer is the way to modify the inner pointer and return it to the caller, nothing wrong with it. By some reason you return null pointer (zero) for both parameters, but I guess you should calculate them under "//...".

Nevertheless, I looks like you''re not ready for C coding just yet. 1) Functions lack return type (even void); 2) function FindLeftAndRight is used before declaration, so it cannot compile, ";" after declaration should be removed, because after it the code block goes; 3) function ConfigureStuff needs "()" after the name; 4) you need to define CMyObject, if you think it''s a class (as the MFC-style name suggests), remember there are no classes in C; 4) variable bFoundObjects if not declared/initialized; 5) it looks like you lack important input and return parameter needed for calculations… do I need to continue?..

—SA


您是否尝试过调试?它到底在哪里崩溃?
变量"left"和"right"进入功能时的值是什么?

您如何调用此方法?
Have you tried to Debug it? Where exactly it crashes?
What is the value of variable ''left'' and ''right'' when it enters the funtion?

How have you called this method?


这篇关于您如何设计一个函数来设置两个优选传递为参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆