旧的C(非C ++)非便携式指针转换 [英] old c (not c++) nonportable pointer conversion

查看:115
本文介绍了旧的C(非C ++)非便携式指针转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pl.Help ...
我很少使用c ...
我有一个有趣的问题,编译器在第(1),(2),(3),(4)行发出警告
可执行文件按要求工作.
我应该进行哪些更改以消除警告.
微软的c ++对于**变量来说更加讨厌.
我想返回值时使用它们
通过参数.

Pl.Help...
I use c very very infrequently...
I have a funny problem,the Compiler gives warning at lines (1),(2),(3),(4)
The executable works as required.
what changes should I make to shoo away the warnings.
Microsoft''s c++ is even more nastier for, ** variables.
I use them when i want to return values
via parameters.

static void FilterAContour(gpc_vertex * Poly,int ** pnumVertices )
{  int          c, v, CnumVertices,FnumVertices;
   BOOL  ChangeDone;
 (1)  CnumVertices=*pnumVertices;
  

   if(CnumVertices <=1){
 (2)  *pnumVertices=1;
    return;
   }     
      
    if(CnumVertices==2)
    { if((Poly[0].x==Poly[1].x) && (Poly[0].y==Poly[1].y) )
    (3)    *pnumVertices=1;
      else
   (4)    *pnumVertices=2;
       return;
     }

推荐答案

我一点也不惊讶!
切掉死木":
I''m not in the least surprised!
Cutting out the "dead wood":
static void func(int ** ppi )
{  int i;
   i = *ppi;
   *ppi = 1;
}

ppi是指向int的指针",函数的主体将其用作指向int的指针"

因此,编译器会正确抱怨.
如果调用函数使用它将您作为真正的指向整数的指针的值,该怎么办?它尝试使用整数值"1"作为指针...


您可以如何解决?使用它作为声明的类型,或更改声明以匹配用法!

ppi is a "pointer-to-a-pointer-to-an-int" and the body of your function is using it as a "pointer-to-an-int"

So the compiler correctly complains.
What if the calling function uses the value it hands you as a genuine pointer-to-a-pointer-to-an-int? It tries to use an integer value "1" as a pointer...


What can you do to fix it? Use it as the type it is declared as, or change the declaration to match the usage!


除了Griff关于修正声明的内容外,您还可以将指针传递给int而不是指向int的指针我有一个问题要问您...

您的函数不返回任何内容.但是,您使用的是out参数(有些错误)...为什么不返回顶点数,然后又少了一件麻烦的事情而弄错了?然后您的代码可能会变成类似...
In addition to what Griff said about fixing your declaration so you''re passing a pointer to int rather than a pointer to pointer to int I''ve got a question back for you...

Your function doesn''t return anything. Yet you''re using an out parameter (some what incorrectly)... Why not return the number of vertices and then you''ll have one less thing to mess around with and get wrong? Then your code could become something like...
static int FilterAContour(gpc_vertex * Poly, int numVertices )
{
  if(numVertices <=1)
   {
       return 1;
   }
   else if(numVertices==2)
   {
       return ((Poly[0].x==Poly[1].x) && (Poly[0].y==Poly[1].y) ) ? 1 : 2;
   }
   else
   {
       return -1;
   }
}

现在,我知道您在此处提供的代码可能还有更多内容,但是仅仅一小部分就可以清除所有内容.坚持使用poly等式函数(鹦鹉是否具有同等权利?太离谱了!),它变得更加简单:

Now I know there''s probably more to the code that what you''ve presented here but just in that little chunk it''s cleared up stuff no end. Stick in a poly equality function (equal rights for parrots? Outrageous!) and it becomes even simpler:

static int EqualXandY( const gpc_vertex *p1, const gpc_vertex *p2 )
{
    return (p1.x==p2.x) && (p1.y==p2.y);
}

static int FilterAContour(gpc_vertex * Poly, int numVertices )
{
  if(numVertices <=1)
   {
       return 1;
   }
   else if(numVertices==2)
   {
       return ( EqualXandY( &Poly[ 0 ], &Poly[ 1 ] ) ? 1 : 2;
   }
   else
   {
       return -1;
   }
}

我不确定这是否正确(我还没有编译),但是从我自学的角度来看,它看起来更容易阅读!

在最后的其他语言中添加的内容,目的是阻止某些编译器(正确地)抱怨某些控制路径未返回值.

I''m not sure if this is exactly correct (I haven''t compiled it) but to my self taught eye it looks a lot easier to read!

Added in the final else to stop some compilers (correctly) whinging about some control paths not returning a value.


这篇关于旧的C(非C ++)非便携式指针转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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