Array of Structs in C

  • #1
B

Björn_2

Bekanntes Mitglied
Themenersteller
Dabei seit
09.07.2004
Beiträge
305
Reaktionspunkte
0
Hallo zusammen,

ich muss folgendes Problem lösen:
In main.c wird ein array von structs angelegt, dass an eine Funktion in der Datei util.c übergeben werden muss.

Aber irgendwie will das ganze nicht.

main.c
Code:
#include util.h
CARTESIAN *cartesian;

int init() {
 cartesian = (CARTESIAN *)malloc(WIDTH*HEIGHT*sizeof(CARTESIAN));
}

int main() {
 init();
 function(cartesian);
}

util.h:
Code:
typedef struct {
 int x;
 int y;
 int z;
} CARTESIAN;

int function(CARTESIAN *cartesian);

util.c:
Code:
#include util.h

int function(CARTESIAN *cartesian) {
  cartesian[0]->x = 1;
  cartesian[0]->y = 2;
  cartesian[0]->z = 3;
}

Ich habe das Beispiel so einfach wie möglich gehalten (also bitte keine Gedanken über den Sinn machen ;))

Der Compiler meldet folgendes:
Code:
util.c:207: error: base operand of `->' has non-pointer type `CARTESIAN'
util.c:208: error: base operand of `->' has non-pointer type `CARTESIAN'
util.c:209: error: base operand of `->' has non-pointer type `CARTESIAN'

Hoffe, ihr könnt mir helfen.

Gruß
Björn
 
  • #2
blöder Fehler ::)

-> durch . ersetzen
 
  • #3
Ich würd's in der Art machen:

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

typedef struct {
  int x;
  int y;
  int z;
} CARTESIAN;

// -------

CARTESIAN** allocCart(int nrows, int ncolumns) {
  /*
    Simulation eines normalen 2D-Arrays; Speicherbereich trotzdem zusammenhängend
    und effizient allokiert.
  */
  int i;
  CARTESIAN** cartesian= malloc(nrows * sizeof(CARTESIAN *));
  if(cartesian) {
    cartesian[0] = malloc(nrows * ncolumns * sizeof(CARTESIAN));
    if(cartesian[0]) {
      for(i = 1; i < nrows; i++)
        cartesian[i] = cartesian[0] + i * ncolumns;        
      return cartesian;
    } else
         free(cartesian);
  }
  return NULL;
}

void initCart(CARTESIAN** cartesian, int nrows, int ncolumns) {
  int i, j;
  for(i = 0; i < nrows; i++) {
    for(j = 0; j < ncolumns; j++) {
      cartesian[i][j].x = 0;
      cartesian[i][j].y = 0;
      cartesian[i][j].z = 0;
    }
  }
}

int function(CARTESIAN** cartesian, int nrows, int ncolumns) {

  cartesian[0][0].x =  1;
  cartesian[0][0].y =  2;
  cartesian[0][0].z =  3;

  cartesian[0][1].x = 10;
  cartesian[0][1].y = 20;
  cartesian[0][1].z = 30;

  return 0;
}

void freeCart(CARTESIAN** cartesian) {
  free(cartesian[0]);
  free(cartesian);
}

// -------

#define CART_NROWS       10
#define CART_NCOLUMNS 10

int main(void) {
  
  CARTESIAN** cartesian= allocCart(CART_NROWS, CART_NCOLUMNS);

  if(!cartesian) {
    fprintf(stderr, Out of memory\n);
    return 1;
  }

  initCart(cartesian, CART_NROWS, CART_NCOLUMNS);
  
  function(cartesian, CART_NROWS, CART_NCOLUMNS);

  freeCart(cartesian); /* an dieser Stelle natürlich optional */

  return 0;
}
 
  • #4
Hallo,

vielen Dank für deine Antwort. Mir reicht ein eindimensionales Array von CARTESIAN.

Gruß
Björn
 
  • #5
Alles klar, ein Missverständnis :).
Ich dachte nur wegen dem WIDTH*HEIGHT.
 
Thema:

Array of Structs in C

ANGEBOTE & SPONSOREN

Statistik des Forums

Themen
113.840
Beiträge
707.963
Mitglieder
51.494
Neuestes Mitglied
Flensburg45
Oben