将2维数组分配给struct对象

时间:2016-01-27 15:07:28

标签: c arrays pointers multidimensional-array malloc

我有二维数组。我希望将该数组分配给struct。请看一下:

这是我的结构:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Table(name = "item_refund_detail")
@Entity
public class ItemRefundDetail implements Serializable {
  private static final long serialVersionUID = 4604092182622619714L;

  RefundTransaction refundTransaction;

  @ManyToOne
  @JoinColumn(name="refund_request_id")
  public RefundTransaction getRefundTransaction() {
    return refundTransaction;
  }

  public void setRefundTransaction(RefundTransaction refundTransaction) {
    this.refundTransaction = refundTransaction;
  }

  @Id
  @Column(name = "order_id")
  String orderId;

  @Id
  @Column(name = "unique_item_id")
  String uniqueItemId;

  Double amount;

//  @Column(name = "refund_request_id")
//  String refundRequestId;

  @Id
  @Column(name = "refund_transaction_id")
  String refundTransactionId;

  @Column(name = "transaction_ref")
  String transactionRef;

  @Transient
  InstrumentType instrumentType;

  @Id
  @Column(name = "order_id")
  public String getOrderId() {
    return orderId;
  }

  @Id
  @Column(name = "unique_item_id")
  public String getUniqueItemId() {
    return uniqueItemId;
  }


  public Double getAmount() {
    return amount;
  }

//  @Column(name = "refund_request_id")
//  public String getRefundRequestId() {
//    return refundRequestId;
//  }

  @Id
  @Column(name = "refund_transaction_id")
  public String getRefundTransactionId() {
    return refundTransactionId;
  }

  @Column(name = "transaction_ref")
  public String getTransactionRef() {
    return transactionRef;
  }

  @Transient
  public InstrumentType getInstrumentType() {
    return instrumentType;
  }

  public void setOrderId(String orderId) {
    this.orderId = orderId;
  }

  public void setUniqueItemId(String uniqueItemId) {
    this.uniqueItemId = uniqueItemId;
  }

  public void setAmount(Double amount) {
    this.amount = amount;
  }

//  public void setRefundRequestId(String refundRequestId) {
//    this.refundRequestId = refundRequestId;
//  }

  public void setRefundTransactionId(String refundTransactionId) {
    this.refundTransactionId = refundTransactionId;
  }

  public void setTransactionRef(String transactionRef) {
    this.transactionRef = transactionRef;
  }

  public void setInstrumentType(InstrumentType instrumentType) {
    this.instrumentType = instrumentType;
  }



}

我希望能够分配这个:

typedef struct {
    int x;
    int y;
    char **table; //2-dim array
} some_struct;

或者这个:

const char sth1_table[2][3] = {
   { ' ', 'x', ' '},
   { 'x', 'x', 'x'},
};

到那个结构。

怎么做? 我尝试过分配:

const char sth2_table[4][2] = {
   { 'x', ' '},
   { 'x', ' '},
   { 'x', ' '},
   { 'x', 'x'},
};

然后访问:

new_sth.table = malloc(sizeof(sth1_table));
*new_sth.table = sth1_table;

但没有运气。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

对于初学者,成员typedef struct { int x; int y; char **table; // pointer } some_struct; some_struct obj; 不是二维数组。这是一个指针。

数组没有赋值运算符。您必须逐个元素地复制数组。

例如,如果你有一个结构对象

const char sth1_table[2][3] = {
   { ' ', 'x', ' '},
   { 'x', 'x', 'x'},
};

和数组

obj.table = malloc( sizeof( char *[2] ) );

for ( size_t i = 0; i < 2; i++ )
{
    obj.table[i] = malloc( sizeof( sth1_table[i] ) );
    memcpy( obj.table[i], sth1_table[i], sizeof( sth1_table[i] ) );
} 

然后你可以采取以下方式

CREATE PROCEDURE tableCreation
(
    @tableName SYSNAME
)
AS
BEGIN

    DECLARE @SQL NVARCHAR(MAX)
    SET @SQL = '
        CREATE TABLE ' + @tableName + ' (
            id INT NOT NULL
        )'

    --PRINT @SQL
    EXEC sys.sp_executesql @SQL

END
GO

EXEC dbo.tableCreation @tableName = 'Employee'

您应该记住,当您需要使用另一个二维数组作为初始化程序时,您必须释放所有已分配的内存或重新分配它。

答案 1 :(得分:0)

如果您可以使用硬编码的数组大小并且不使用malloc, 我会考虑这个例子:

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

struct Some_Struct
{
    int x;
    int y;
    char sth1_table[2][3];
    char sth2_table[4][4];
};

int main ( int argc, char *argv[] )
{
    struct Some_Struct yy;
    struct Some_Struct zz[10];

    yy.x = 1;
    yy.y = 2;

    /*
    you wanted this
    const char sth1_table[2][3] = {
        { ' ', 'a', ' '},
        { 'b', 'c', 'd'},
    };
    */

    yy.sth1_table[0][0] = '\0';   /* null character */
    yy.sth1_table[0][1] = 'a';
    yy.sth1_table[0][2] = '\0';
    yy.sth1_table[1][0] = 'b';
    yy.sth1_table[1][1] = 'c';
    yy.sth1_table[1][2] = 'd';

    for ( i = 0; i < 10; i++ )
    {
        zz[i].sth1_table[0][0] = '\0';   /* null character */
        zz[i].sth1_table[0][1] = 'a';
        zz[i].sth1_table[0][2] = '\0';
        zz[i].sth1_table[1][0] = 'b';
        zz[i].sth1_table[1][1] = 'c';
        zz[i].sth1_table[1][2] = 'd';
    }
}

如果您对运行时分配感兴趣,那么除了malloc()之外还要考虑calloc()函数。这样做时,请记住二维数组(或三维,四维,任意维)的内存分配只是一块内存,其大小是维数乘以类型大小。

char sth1_table[2][3];

char的大小是1个字节。 1字节* 2 * 3 = sth1_table的6字节内存

char sth2_table[4][4];

1字节* 4 * 4 = 16字节的内存sth2_table

然后您如何选择在分配的内存中订购数据, 无论是排第一还是第一列,完全取决于你。

答案 2 :(得分:-1)

我没有直接使用 malloc ,而是建议为内存分配定义一个宏。这使代码更容易遵循。在下面的示例中,我冒昧地重命名了一些标识符。

{{1}}
相关问题