close
  • chevron_right

    C Beispiel struct und Zeiger

    DebXWoody · Wednesday, 30 June - 05:11

Hier ein Beispiel wegen einer Frage aus dem C/C++ Gruppenchat

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

typedef struct {
	int x;
	int y;
} C;

static void _set(C* c, int x, int y);

int main( int argc, char argv[]) {
	C* c = malloc(sizeof(C));
	_set(c, 10, 20);
	printf("c: x=%d y=%d\n", c->x, c->y);
	_set(c, ++(c->x), ++(c->y));
	printf("c: x=%d y=%d\n", c->x, c->y);
	free(c);
	return EXIT_SUCCESS;

}

static void _set(C* c, int x, int y) {
	assert(c);
	c->x = x;
	c->y = y;
}
  • favorite

    1 Like

    Arne