指向指针函数的指针--->如何返回指针

时间:2014-12-13 11:15:47

标签: c

我有这个指向指针函数的指针,它接受两个参数,并且它假设返回指向数组的指针。但是在这种情况下返回指向数组的指针的正确方法是什么?这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int **multiTable (unsigned int xs, unsigned int ys)
{
    unsigned int i, j;

    int **table = malloc( ys * sizeof(*table)); 
          // should I put malloc(ys * xs * sizeof(*table))?

    for(i = 0; i < ys; i++)
    {
        for(j = 0; j < xs; j++)
        {
           table[i][j] = (j+1) * (i+1);
        }

    }

    return table;  //what would be the proper way to return table? Is this ok?

    free(**table); //by the way, do I need this? 
}

int main()
{
    int sum = 0;
    sum = multiTable2(3,2);

    return 0;
}

返回桌子的正确方法是什么?是我写的返回表;好吧还是应该像return * table甚至返回**表?而且,我需要释放桌子吗?

2 个答案:

答案 0 :(得分:1)

我认为这就是你要做的事。

#include <stdio.h>
#include <stdlib.h>

int **multiTable(unsigned int xs, unsigned int ys)
{

    unsigned int i, j;
    int **table;

    table = malloc(ys * sizeof(int *)); /* you need space for ys integer arrays */
    if (table == NULL)
        return NULL;
    for (i = 0 ; i < ys ; i++)
    {
        table[i] = malloc(xs * sizeof(int)); /* you need space for xs integers */
        if (table[i] != NULL) /* did malloc succeed? */
        {
            for (j = 0 ; j < xs ; j++)
                table[i][j] = (j + 1) * (i + 1);
        }
    }
    return table;
}

int main()
{
    int **sum;

    sum = multiTable(3, 2);
    /* do somenthing with sum here */
    if (sum != NULL)
    {
        /* now you must use free, like this */
        unsigned int i;
        for (i = 0 ; i < 2 ; i++)
        {
            if (sum[i] != NULL)
                free(sum[i]);
        }
        free(sum);
    }
    return 0;
}

这可以做到,但你需要阅读更多内容来理解基础知识。

你的问题不是

  

如何返回指针

而是如何使用mallocfree

答案 1 :(得分:0)

  

如果我只想使用一个malloc,它会工作:int   (* table)[xs] = malloc(ys * sizeof(* table))

是的,如果根据Matt McNabb的评论调整功能,那就可以了:

<div class="form-horizontal">
        <h4>Branch</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group col-md-12">
            <div class="col-md-2">
                @Html.LabelFor(model => model.BranchName, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-3">
                @Html.EditorFor(model => model.BranchName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BranchName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group col-md-12">
            <div class="col-md-2">
                @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-3" style="float: left !important;">
                @Html.EditorFor(model => model.Active)
                @Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class=" col-md-offset-1 col-md-2">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>