线性存储器中的2D阵列

时间:2014-07-23 17:41:46

标签: c arrays

我在结构中创建2D数组时遇到问题。这是我的代码到目前为止,但我确定它是错的数组存储在结构中的char指针,但数组必须是浮点数,所以我也很困惑:

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

#define MAX_ROWS 5000
#define MAX_COLUMNS 5000

struct array
{
        int rows;
        int columns;
        int order;
        char *base_pointer; /* pointer to array of bytes. In this array, float numbers will be stored (4 bytes each) */
};
 struct array* initialize(int rows, int columns, int order)
 {

  /* Allocate the required memory for the 2D array to store float values (Ex: 1.45) with "rows" and "columns" */
  struct array* array = (struct array*)malloc(rows * columns);
  /* Make sure the size is within 1 to MAX_ROWS and 1 to MAX_COLUMNS specified in main.c. If not return null pointer */
  array->rows = rows;
  array->columns = columns;
  array->order = order;
  /* Initialize the 2D array to the all zeroes (0.0) */
  /* Assign suitable values to all the elements of the structure and return the struct pointer */
  return array;
 }

4 个答案:

答案 0 :(得分:1)

首先需要为array结构分配一些空间,然后才能为数组分配内存。

struct array {
    /* number of rows isn't necessary, unless you want to do some boundary checking */
    unsigned int columns;
    int order;
    char* data;
};
struct array* array_createf(unsigned int rows, unsigned int columns, int order) {
    /* Allocate the required memory for the 2D array structure */
    struct array * arr = (struct array*)malloc(sizeof(struct array));
    /* Allocate some memory for array´s data */
    arr->data = (char*)malloc(rows*columns*sizeof(float));

    /* store information about the array*/
    arr->rows = rows;
    arr->columns = columns;
    arr->order = order;

    /* return */
    return arr;
}
float array_getf(struct array * arr, unsigned int row, unsigned int column) {
    return array_getfrow(row)[column];
}
void array_setf(struct array * arr, unsigned int row, unsigned int column, float item) {
    array_getfrow(row)[column] = item;
}

float * array_getfrow(struct frray * arr, unsigned int row) {
    return (float*)&arr->data[row*arr->columns*sizeof(float)];
}

答案 1 :(得分:1)

我认为这是你正在寻找的东西:

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

#define MAX_ROWS 5000
#define MAX_COLUMNS 5000

struct array
{
        int rows;
        int columns;
        int order;
        char *base_pointer; /* pointer to array of bytes. In this array, float numbers will be stored (4 bytes each) */
};
struct array* initialize(int rows, int columns, int order)
{
  int i,
      size = columns * rows * sizeof(float);
  /* Allocate the required memory for the 2D array to store float values (Ex: 1.45) with "rows" and "columns" */

  // If you want to allocate both the structure and the internal storage in one malloc:
  /*
  struct array* array = (struct array*) malloc(sizeof(struct array) + size);
  array->base_pointer = &((char*)array)[sizeof(struct array)];
  */

  struct array* array = malloc(sizeof(struct array));
  if(!array) {
    return 0; // error
  }
  array->base_pointer = malloc(size);
  if(!array->base_pointer) {
    return 0; // error
  }

  for(i = 0; i < size; i++) {
    array->base_pointer[i] = 0;
  }
  /* Make sure the size is within 1 to MAX_ROWS and 1 to MAX_COLUMNS specified in main.c. If not return null pointer */
  array->rows = rows;
  array->columns = columns;
  array->order = order;
  /* Initialize the 2D array to the all zeroes (0.0) */
  /* Assign suitable values to all the elements of the structure and return the struct pointer */
  return array;
 }

 void insert(struct array* arr, int row, int column, float value) {
    float* floatArr = (float*)arr->base_pointer;
    floatArr[(column * arr->rows) + row] = value;
 }

 float retrieve(struct array* arr, int row, int column) {
    float* floatArr = (float*)arr->base_pointer;
    return floatArr[(column * arr->rows) + row];
 }

您正在分配内部阵列所需的内存并尝试将其用于结构。相反,您需要为结构分配内存,然后为其内部数组base_pointer提供指向单独分配的指针。

答案 2 :(得分:0)

怎么样:

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

#define MAX_ROWS 5000
#define MAX_COLUMNS 5000

struct array
{
        int rows;
        int columns;
        int order;
        float base_pointer[MAX_ROWS][MAX_COLUMNS];
};

 struct array* initialize(int rows, int columns, int order)
 {

  struct array* array = malloc(sizeof(struct array));
  assert(array != NULL);
  array->rows = rows;
  array->columns = columns;
  array->order = order;

  /* Initialize the 2D array to the all zeroes (0.0) */
  memset(array->base_pointer, 0, sizeof(array->base_pointer));
  /* Assign suitable values to all the elements of the structure and return the struct pointer */
  return array;
 }

答案 3 :(得分:0)

我认为这就是你要找的东西:

struct array* initialize(int rows, int columns, int order)
{
   /* Allocate the required memory for the 2D array */
   struct array* array = malloc(sizeof(struct array));

   /* Make sure the size is within 1 to MAX_ROWS and 1 to MAX_COLUMNS specified in main.c. If not return null pointer */
   array->rows = rows;
   array->columns = columns;
   array->order = order;

   /* Allocate memory for the data of the array and initialize them to all zeroes (0.0) */
   array->base_pointer = calloc(sizeof(float)*rows*columns);

   /* Return the array */
   return array;
}