C ++指针指向Java [英] C++ Pointers to Pointers in Java

查看:121
本文介绍了C ++指针指向Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Java菜鸟。我已经能够掌握将C / C ++指针转换为Java引用的概念,这已经相当顺利了。

I am a Java noob. I have been able to grasp the concept of converting C/C++ pointers into Java references and this has gone fairly smoothly.

我点击了一段指向指针的代码(即** ptr)。我需要取消引用指针并更改它指向的指针的值(即* ptr =&newthing;)

I hit a piece of code that has pointers to pointers (ie **ptr). I need to dereference the pointer and change the value of the pointer it is pointing to (ie *ptr = &newthing;)

这在java中看起来有点难度。有没有人对如何解决这个问题有任何想法?快速谷歌搜索没有任何结果。

This seems alot tougher in java. Does anyone have any ideas on how to solve this issue? Quick google search turned up nothing.

这是C ++中的代码示例。我想在java中使用类似的东西,但是ptr_to_ptr变量是个问题:

Here's a code example in C++. I want to get something similar working in java but the ptr_to_ptr variable is a problem:

struct _coord
{
   int x;
   int y;
   _coord * next_coordinate;
} coordinate_t;   

coordinate_t buffer[100];
coordinate_t * head;
coordinate_t ** ptr_to_ptr;

if (wednesday)
{
    ptr_to_ptr = &head;
}
else
{
    ptr_to_ptr = &head->next_coordinate;
}
*ptr_to_ptr = &buffer[3];   // <<<---- HOW DO YOU MAKE THIS WORK?


推荐答案

我会回答我自己的问题。我找不到任何简单的方法来指向java中的指针(引用引用)。所以我重构了代码以使用引用数组和数组索引而不是指针。我认为这将有效(它很容易编码),但我还没有测试过。

I'll answer my own question. I could not find any easy way to do a pointer to a pointer (reference to a reference) in java. So I refactored the code to use arrays of references, and array indices instead of pointers. I think this will work (it coded easily) but I have not tested it yet.

这篇关于C ++指针指向Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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