Home > Software engineering >  How to define a pointer on a array of strings in C?
How to define a pointer on a array of strings in C?

Time:02-16

Can someone explain how i can define this array of strings?

enter image description here

a is a pointer and its points to a array of chars. So it has to be char *a[3]?

CodePudding user response:

The array is defined as an array of pointers to string literals like

char * a[3] = { "A", "B", "C" };

where instead of "A", "B", "C" you can use your own string literals.

To declare a pointer to the first element of such an array you can write

char **p = a;

Here is a demonstration program.

#include <stdio.h>

int main( void )
{
    char * a[] = { "A", "B", "C" };
    const size_t N = sizeof( a ) / sizeof( *a );

    char **p = a;

    for ( size_t i = 0; i < N; i   )
    {
        printf( "%s ", a[i] );
    }

    putchar( '\n' );

    for ( size_t i = 0; i < N; i   )
    {
        printf( "%s ", p[i] );
    }

    putchar( '\n' );
}

CodePudding user response:

char* a[3];
a[0]="Datentypen";
a[1] = "und";
a[2] = "Variablen";
  • Related