Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define a pointer on a array of strings in C?

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]?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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' );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading