Reputation: 594
I'm having trouble creating a 2D array in a struct. Here is my code so far but I'm sure it's wrong The array is stored in the char pointer in the struct but the array must be for floats so that has me confused as well:
#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;
}
Upvotes: 0
Views: 475
Reputation: 14768
I think this is what you're looking for:
#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];
}
You were allocating the memory required for the internal array and trying to use it for the structure. Instead you need to allocate the memory for the structure then provide it with a pointer to a separate allocation for its internal array base_pointer
.
Upvotes: 1
Reputation: 6403
You need to allocate some space for the array
structure first, then you can allocate the memory for your 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)];
}
Upvotes: 1
Reputation: 206567
I think this is what you are looking for:
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;
}
Upvotes: 0
Reputation: 868
How about:
#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;
}
Upvotes: 0