将struct数组复制到另一个较小的struct数组

时间:2016-10-10 19:25:13

标签: c arrays struct copy

我的struct数组有5个插槽

<?php

class dbpdo {

private $_connection;
private $_statement;

public function __construct( ) {}

public function connect( $host, $username, $password, $database ) {
    $connect = 'mysql:host='.$host.';dbname='.$database.';charset=utf8mb4';
    $this->_connection = new PDO($connect, $username, $password);
    $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->_connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}

public function __destruct() {
    if ($this->_connection)
        $this->_connection = null;
}

public function query($query){
    try {
        return $this->_connection->query($query);
    } catch(PDOException $e) {
        return "Error: " . $e->getMessage();
    }
}

public function fetch(){
    try {
        return $this->_statement->fetch();
    } catch(PDOException $e) {
        return "Error: " . $e->getMessage();
    }
}

public function prepare($query) {
    try {
        $this->_statement = $this->_connection->prepare($query);
        return 1;
    } catch(PDOException $e) {
        return "Error: " . $e->getMessage();
    }
}

public function execute($array) {
    try {
        $this->_statement->execute($array);
        return 1;
    } catch(PDOException $e) {
        return "Error: " . $e->getMessage();
    }
}

public function mysql_escape_mimic($inp) {
    if(is_array($inp))
        return array_map(__METHOD__, $inp);

    if(!empty($inp) && is_string($inp)) {
        return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
    }
    return $inp;
}
}

让我们说里面有元素,然后我做

struct router* router[5];

是否可以重新排列阵列,以便路由器[4]中的元素向上移动到路由器[3],路由器[5]移动到路由器[4]等?

1 个答案:

答案 0 :(得分:1)

试试这个

void delete(struct router** router, int which, int size) {
    int i;
    // If these pointers have no other references to them then
    // Then you should free the one being deleted at this point
    for(i = which; i < size - 1; i++) {
       router[i] = router[i + 1];
    }
    router[i] = NULL;

}