我对malloc和realloc的理解出错了吗?

时间:2019-06-06 17:36:31

标签: c malloc realloc

我试图创建一个在程序运行时可以调整大小的数组。我已经了解了malloc和realloc函数,但是看来我似乎出了点问题。这是我写的函数,它根据循环产生的周期数来创建数组。

function getKeys($con, $query)
{
    $result = $con->query($query);
    $row = $result->fetch(PDO::FETCH_ASSOC);
    return array_keys($row); // This will fetch the keys from an array
}

function getValues($con, $query)
{
    $data = $con->query($query);
    $data->setFetchMode(PDO::FETCH_ASSOC);

    return $data;
}

try {
    $con = new PDO('mysql:host=localhost;dbname=snm', "root", "");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = "SELECT ID,Voornaam,Achternaam,Woonplaats,Postcode,Email,Social,Soort,Categoriebord,Categoriegame FROM gebruiker";

    //Select Box
    $data = getKeys($con, $query);
    print '<select>';
    foreach ($data as $row) {
        print " <option value='".$row[0]."'>".$row[0]."</option>"; // ID
        print " <option value='".$row[1]."'>".$row[1]."</option>"; // Voornaam
        print " <option value='".$row[2]."'>".$row[2]."</option>"; // Achternaam
    }
    print '</select>';


    //Title
    print "<table>";
    $array_keys = getKeys($con, $query);

    print " <tr>";
    foreach ($array_keys as $value) {
        print " <th>$value</th>";
    }
    print " </tr> ";

    //Values
    $data = getValues($con, $query);
    foreach ($data as $row) {
        print " <tr> ";
        foreach ($row as $name => $value) {
            print " <td>$value</td> ";
        }
        print " </tr> ";
    }
    print "</table> ";

} catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

我试图阅读我在互联网上找到的有关此文档的信息,重新分配后我无法检索到新数组。

int* flexibleArray() {
    int *arrayFlex = NULL;
    int number=0, cnt=0;
    while (number!=-1) {
        printf("\nInsert the variable: ");
        scanf("%d", &number);
        if (number==-1){
            break;
        }
        cnt+=1;
        arrayFlex = realloc(arrayFlex, cnt * sizeof(int));
        arrayFlex[cnt-1] = number;
    }
    return arrayFlex;
}

基本上,这是我正在测试该功能的地方,以查看它是否可以正常工作。

我是C的新手,对不起。 谢谢

1 个答案:

答案 0 :(得分:2)

类似的事情应该做。

int* flexibleArray() {                                                          
    int *arrayFlex = NULL;                 // needs to be a pointer             
    int number=0, cnt=0;                                                        
    while (number!=-1) {                                                        
        printf("\nInsert the variable: ");                                      
        scanf("%d", &number);                                                   
        if (number==-1){                                                        
            break;                                                              
        }                                                                       
        cnt+=1;                                                                 
        arrayFlex = realloc(arrayFlex, cnt * sizeof(int));                      
        arrayFlex[cnt-1] = number;                                              
    }                                                                           
    return arrayFlex;                                                           
}     

编辑:固定错别字。