Pointer

Quellen

Data Pointers
How Pointers work
Pointers and Functions
Pointers and Strings
Learn C and C++ Programming
C ++ Programming Tutorial

Zeichenketten als Arrays oder per Pointer

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 */

Laut http://www.lysator.liu.se/c/c-faq/c-2.html

    f(a)
    char a[];

​ are treated by the compiler as if they were pointers, since that is what the function will receive if an array is passed:

    f(a)
    char *a;

Noch ein Beispiel:

int x[5] = {1,2,3,4,5};
int *p;
p = x;      //Works only for arrays!
p = &x;     //Works for arrays or variables
p = &x[0];  //This one is the most obvious

Pointer zeigt auf Array-Anfang

int myarray[6] = {4,8,15,16,23,42};
int* mypointer;
mypointer = myarray;
cout << mypointer[2] << endl;

Embedded

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;

Beispiele

int main (void)
{
    int *pointer;
    int wert;
    wert = 10;
    pointer = &wert;
    return 0;
}
void quadrieren(float * zahl) {}
    *zahl = (*zahl) * 2;
}

int main (void) {
    float wert;
    wert = 2.5;
    quadrieren(&wert);
    return 0;
}