Worin liegt der Unterschied?
char a[] = "hello";
char *p = "world";
Das macht der Compiler draus
+---+---+---+---+---+---+
a: | h | e | l | l | o |\0 |
+---+---+---+---+---+---+
+-----+ +---+---+---+---+---+---+
p: | *======> | w | o | r | l | d |\0 |
+-----+ +---+---+---+---+---+---+
a[3] for the compiler means: start at the location a and move three past it and fetch the char there
p[3] means go to the location p, dereference the value there, move three past it (add three to pointer) and fetch the char there.
a[3]
is three places past (the start of) the object named a
, while p[3]
is three places past the object pointed to by p
.
ABER in Funktionen:
void foo(int *a);
und
void foo(int a[]);
sind äquivalent.
Gleiches gilt hier
void foo(int *a) {
a[98] = 0xFEADFACE;
}
void bar(int b[]) {
*(b+498) = 0xFEADFACE;
}
int * a; /* pointer to int. points nowhere in paticular right now */
int b[10]; /* array of int. Memory for 10 ints has been allocated on the stack */
foo(a); /* calls foo with parameter `int*` */
foo(b); /* also calls foo with parameter `int*` because here the name b basically
is a pointer to the first elment of the array */
Pointer auf Memory in Mikrocontrollern (via https://microcontrollerslab.com/accessing-memory-mapped-io-microcontrollers-pointer/)
Adressen
Base address of PORTF = 0x40025000
Offset address of GPIODIR = 0x400 //page number 663 TM4C123GH6PM datasheet
GPIOFDIR Physical address = 0x40025000+0x400 = 0x40025400
Pointer-Variable anlegen: Pointer zeigt direkt auf Adresse im Speicher
Unsigned int * GPIO_PORTF_DIR_R = (unsigned int*)0x40025400;
Pointer derefenzieren (Inhalt an Adresse schreiben, auf die der Zeiger zeigt)
*GPIO_PORTF_DIR_R = 0xF0;
Hier werden die ersten 4 Pins als Ausgang konfiguriert
Alternativ ein Pointer direkt auf GPIO
// write value 0xF0 to GPIOFDIR register
(* ( ( volatile unsigned int * ) 0x40025400 ) ) = 0xF0;
//read value GPIOFDIR register memory address and store it in variable data.
data = (* ( ( volatile unsigned int * ) 0x40025400 ) ) ;
#define GPIO_PORTF_DIR_R (*( ( volatile unsigned int * )0x40025400 ) )
GPIO_PORTF_DIR_R = 0xF0;
data = GPIO_PORTF_DIR_R;