2D阵列让我很难受

时间:2017-06-14 17:00:53

标签: c++ arrays

我在阅读C ++中的2D数组时感到震惊,我们可以在声明中声明 这样一来:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="row">
    <div class="row">
        <div class="col-lg-10">
            <div class="input-group">
                <input id="inputSentence" type="text" 
                       class="form-control" placeholder="Sentence...">
                <span class="input-group-btn">
                    <button class="btn btn-secondary" 
                            type="button" id="addSentence">Add</button>
                </span>
            </div>
        </div>
    </div>
    <div class="col-lg-12">
        <div style="display: inline;">
            <div class="group-of-repeated-sections" style="display: none;">
                <div class="my-repeated-section">
                    <div id="copythis">
                        <div class="row">
                            <div class="col-lg-10">
                                <span id="sentence"></span>
                            </div>
                            <div class="col-lg-2">
                                <span>
                                    <a href="#" class="add"> + </a>
                                    <a href="#" class="remove"> - </a>
                                </span>
                            </div>
                        </div>
                        <div class="my-repeated-section-form">
                            <div class="row">
                                <div class="col-lg-12">
                                    <input type="text" />
                                    <span>
                                        <a href="#" class="addform"> + </a>
                                        <a href="#" class="removeform"> - </a>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div style="height:25px;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="myform"></div>
<button id="save">Save</button>

我想知道为什么函数不需要第一维的大小?

我在许多论坛上问这个问题,但只是得到了如何声明这样的数组的方法。但是从来没有找到原因的答案?

1 个答案:

答案 0 :(得分:0)

因为传递给函数的数组被视为指针(对于数组的第一个元素)。

所以像

这样的参数声明
/// @description Movement logic
// Get the input
var x_input = (keyboard_check(vk_right) - keyboard_check(vk_left)) * acceleration_;

// Vector variables
var vector2_x = 0;
var vector2_y = 1;

// Horizontal movement
velocity_[vector2_x] = clamp(velocity_[vector2_x]+x_input, -max_velocity_[vector2_x], max_velocity_[vector2_x]);
var on_ground = tile_collide_at_points(collision_tile_map_id_, [bbox_left, bbox_bottom], [bbox_right-1, bbox_bottom]);
if keyboard_check(vk_right){
    if on_ground {
    sprite_index = spr_player_ground_right
    direction_=0
    }
    else {
    sprite_index = spr_player_flying_right
    direction_=0
    }
}

if keyboard_check(vk_left){
    if on_ground {
    sprite_index = spr_player_ground_left
    direction_=1
    }
    else {
    sprite_index = spr_player_flying_left
    direction_=1
    }
}

// Friction
if x_input == 0 {
    velocity_[vector2_x] = lerp(velocity_[vector2_x], 0, .2);
}


// Gravity
velocity_[vector2_y] += gravity_;

// Move and contact tiles
move_and_contact_tiles(collision_tile_map_id_, 64, velocity_);

// Jumping
var on_ground = tile_collide_at_points(collision_tile_map_id_, [bbox_left, bbox_bottom], [bbox_right-1, bbox_bottom]);
//if on_ground {
    // Jumping
    if keyboard_check(vk_space) {
        velocity_[vector2_y] = -jump_speed_;
        if direction_=0 {
        sprite_index = spr_player_flying_right
        direction_=0
        }
        else {
        sprite_index = spr_player_flying_left
        direction_=1
        }
    }
//}

等于

/// @description Movement variables
velocity_ = [0, 0];
gravity_ = 0.3;
jump_speed_ = 4;
max_velocity_ = [8, 32];
acceleration_ = 2.1;
direction_ = 0;

// Get the tilemap id
var layer_id = layer_get_id("CollisionTiles");
collision_tile_map_id_ = layer_tilemap_get_id(layer_id);

因此<div style="width:1200px;right:0px; top:100px; height:200px;background-color:lightgray;">Hello</div> 是指向float month[][X] 类型的float (*month)[X] 元素数组的指针。

这是因为“2d”数组实际上是数组的数组。 C ++实际上没有多维数组。

另请注意array of arrays is not the same as a pointer to a pointer。指针的衰减仅发生在外部数组(“第一维”)。