bitfelder/ pointer struktur in struktur
-
Hallo zusammen,
das folgende programm lässt sich zwar kompilieren aber baer ich bekomme durch zeile 50 einen speicherzugriffsfehler. ich hoffe ihr könnt mir helfenbitfelder.h
#include <stdlib.h> #include <stdio.h> #define ON 1 #define OFF 0 #define RECHTS 2 #define LINKS 1
bitfelder.c
#include "bitfelder.h" struct motoren { unsigned int onofm : 2; unsigned int directioneins : 2; unsigned int directionzwei : 2; }; struct bittype { unsigned int onofi : 1; unsigned int speed : 3; unsigned int sensor1 : 8; unsigned int sensor2 : 1; struct motoren *mbits; }; void initSteuerung (struct bittype *bits); void anzeigenMotor (struct bittype *bits); void anzeigenStatus(struct bittype bits); int main (void) { struct bittype bits; struct bittype *zeiger; zeiger = &bits; //printf ("\nbits.mbits.onofm = motor bei ON ein, bei OFF aus\nbits.mbits.direction = linksdrehend bei LINKS, rechtsdrehend bei RECHTS\n"); initSteuerung(zeiger); anzeigenMotor(zeiger); anzeigenStatus(bits); } void initSteuerung (struct bittype *bits) { bits->onofi = OFF; bits->sensor2 = OFF; bits->mbits->onofm = OFF; bits->sensor1 = 100; } void anzeigenMotor (struct bittype *bits) { int motorstatus = 0; int motordirection = 0; printf ("Motor 1 (ein:1/aus:0): \n"); scanf ("%i", &motorstatus); if (motorstatus == 1) { printf ("Motor 1 links=2/rechts=1 drehend: \n"); scanf ("%i", &motordirection); if (motordirection == 2) { bits->mbits->directioneins == 2; printf ("Motor 1 dreht links.\n\n"); } else { bits->mbits->directioneins == 1; printf ("Motor 1 dreht rechts.\n\n"); } } printf ("Motor 2 (ein:1/aus:0): \n"); scanf ("%ii", &motorstatus); if (motorstatus == 1) { printf ("Motor 2 links=2/rechts=1 drehend: \n"); scanf ("%i", &motordirection); if (motordirection == 2) { bits->mbits->directionzwei == 2; printf ("Motor 1 dreht links.\n\n"); } else { bits->mbits->directionzwei == 1; printf ("Motor 1 dreht rechts.\n\n"); } } } void anzeigenStatus(struct bittype bits) { printf ("Status der Steuerung\n"); for (int i=0; i<20;i++) {printf("-");} printf ("\n"); if (bits.onofi == ON) {printf ("Steuerung:\tEingeschaltet");} else {printf ("Steuerung:\tAusgeschaltet");} printf ("\n"); if (bits.mbits->directioneins == 2) {printf ("Motor links:\tlinksdrehend");} else if (bits.mbits->directioneins == 1) {printf ("Motor links:\trechtsdrehend");} else {printf ("Motor links:\tAUS");} printf ("\n"); if (bits.mbits->directionzwei == 2) {printf ("Motor links:\tlinksdrehend");} else if (bits.mbits->directionzwei == 1) {printf ("Motor links:\trechtsdrehend");} else {printf ("Motor links:\tAus");} printf ("\n"); printf ("Tempo:\t%i", bits.speed); printf ("\n"); printf ("Sensor1-Wert:\t%d",bits.sensor1); printf ("\n"); if (bits.sensor1 == 1) {printf ("Sensor2-Wert:\tEIN");} else {printf ("Sensor2-Wert:\tAus");} printf ("\n"); }
gruß nico
-
Das ist auch richtig so, dass du einen Fehler erhälst.
Sei froh, denn er weißt dich auf einen Fehler in deinem Code hin.
Du musst Speicher für deinen struct motoren Zeiger definieren, z.B.int main (void) { struct motoren m={OFF}; struct bittype bits={OFF,0,100,OFF,&m}; /*initSteuerung(&bits); kann entfallen, da schon passiert s.o.*/ anzeigenMotor(&bits); anzeigenStatus(bits); ... }
-
leider ist diese funktion in der aufgabenstellung vorgeschrieben
-
Besser gleich ein Objekt
motoren
(kein Zeiger) als Member in bittype deklarieren. Dann hast du auch keine Probleme mit einer vergessenen Zuweisung und Gültigkeitsbereichen.
-
so jetzt wird es kompiliert ohne einen fehler allerdings bekomme ich jetzt biem ausführen einen speicherzugriffsfehler
#include "bitfelder.h" /*struct motoren { unsigned int onofm : 2; unsigned int directioneins : 2; unsigned int directionzwei : 2; };*/ struct bittype { unsigned int onofi : 1; unsigned int sensor2 : 1; unsigned int onofm : 2; unsigned int directioneins : 2; unsigned int directionzwei : 2; unsigned int speed : 3; unsigned int sensor1 : 8; //struct motoren *mbits; }; void initSteuerung (struct bittype *bits); void anzeigenMotor (struct bittype *bits); void anzeigenStatus(struct bittype bits); int main (void) { struct bittype bits; struct bittype *zeiger = &bits; //printf ("\nbits.mbits.onofm = motor bei ON ein, bei OFF aus\nbits.mbits.direction = linksdrehend bei LINKS, rechtsdrehend bei RECHTS\n"); initSteuerung(zeiger); anzeigenMotor(zeiger); anzeigenStatus(bits); } void initSteuerung (struct bittype *bits) { bits->onofi = OFF; bits->sensor2 = OFF; bits->onofm = OFF; bits->sensor1 = 100; } void anzeigenMotor (struct bittype *bits) { int motorstatus = 0; int motordirection = 0; printf ("Motor 1 (ein:1/aus:0): \n"); scanf ("%i", &motorstatus); if (motorstatus == 1) { printf ("Motor 1 links=2/rechts=1 drehend: \n"); scanf ("%i", &motordirection); if (motordirection == 2) { bits->directioneins = 2; printf ("Motor 1 dreht links.\n\n"); } else { bits->directioneins = 1; printf ("Motor 1 dreht rechts.\n\n"); } } printf ("Motor 2 (ein:1/aus:0): \n"); scanf ("%i", &motorstatus); if (motorstatus == 1) { printf ("Motor 2 links=2/rechts=1 drehend: \n"); scanf ("%i", &motordirection); if (motordirection == 2) { bits->directionzwei = 2; printf ("Motor 1 dreht links.\n\n"); } else { bits->directionzwei = 1; printf ("Motor 1 dreht rechts.\n\n"); } } } void anzeigenStatus(struct bittype bits) { printf ("Status der Steuerung\n"); for (int i=0; i<20;i++) {printf("-");} printf ("\n"); if (bits.onofi == ON) {printf ("Steuerung:\tEingeschaltet");} else {printf ("Steuerung:\tAusgeschaltet");} printf ("\n"); if (bits.directioneins == 2) {printf ("Motor links:\tlinksdrehend");} else if (bits.directioneins == 1) {printf ("Motor links:\trechtsdrehend");} else {printf ("Motor links:\tAUS");} printf ("\n"); if (bits.directionzwei == 2) {printf ("Motor links:\tlinksdrehend");} else if (bits.directionzwei == 1) {printf ("Motor links:\trechtsdrehend");} else {printf ("Motor links:\tAus");} printf ("\n"); printf ("Tempo:\t%i", bits.speed); printf ("\n"); printf ("Sensor1-Wert:\t%d",bits.sensor1); printf ("\n"); if (bits.sensor1 == 1) {printf ("Sensor2-Wert:\tEIN");} else {printf ("Sensor2-Wert:\tAus");} printf ("\n"); }