UV0
UV0

Reputation: 33

how int *arr[] is array of pointers to int but char *arr[] is array of pointers to string?

In C

int *arr[]

is clearly an array of pointers to int but for

char *arr[]

is array of pointers to strings, how is that so. Then how does compiler differentiate in this two. I read this question-

Char* array of chars, but int* not array of ints?

but with array of pointers to strings we are talking about big memory which compiler will have to create separately for strings and then to array of pointer do compiler treat variables separately somehow?

Upvotes: 0

Views: 133

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311166

No, the compiler does not differentiate them in general.

Consider the declarations

int a[] = { 1, 2, 3 };
int * pa[] = { a, a + 1,  a + 2 };

and

char s[] = { '1', '2', '3' };
char * ps[] = { s, s + 1, s + 2 };

As you can see there is no principal difference.

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    int a[] = { 1, 2, 3 };
    int * pa[] = { a, a + 1,  a + 2 };
    
    for ( size_t i = 0; i < sizeof( pa ) / sizeof( *pa ); i++ )
    {
        printf( "%d ", *pa[i] );
    }
    putchar( '\n' );
    
    char s[] = { '1', '2', '3' };
    char * ps[] = { s, s + 1, s + 2 };

    for ( size_t i = 0; i < sizeof( ps ) / sizeof( *ps ); i++ )
    {
        printf( "%c ", *ps[i] );
    }
    putchar( '\n' );
    
    return 0;
}

The program output is

1 2 3 
1 2 3 

The only difference is that the Standard declares string functions that expect an argument of the type char * that points to the first element of a string while specially for the type int * such functions are absent.

Accordingly some output functions are specially designed to output strings when their arguments have the type char * because strings have the sentinel value '\0' unlike arrays with the element type int.

Upvotes: 0

Emily-TTG
Emily-TTG

Reputation: 594

"Arrays" in C are essentially wrappers for pointers - they do have differences but for the sakes of this question you can think of them as pointers to the first element within them.

Secondly, pointers and arrays in C can be subscripted - which is to say that you can use square brackets ([]) to perform array-like access to them. Underneath the hood this is just some pointer arithmetic to get the desired element.

This means that both type* var and type var[] can be an "array" of type, but type* name can also just be a plain old pointer to some data of type type - looking at it without context you can't really tell.

So int *arr[] is either an array of int "arrays" or of pointers to individual ints

And char *arr[] is either an array of char "arrays" or of pointers to individual chars

Finally - the C compiler doesn't care about what they are provided that you syntactically use them correctly - you could access an int* as an array which isn't one and get garbage data but it's not the job of the C compiler to tell you not to.

Upvotes: 0

tadman
tadman

Reputation: 211740

int* and char* behave the same, only the latter is treated as a C string while the former is not. A C string is just a pointer to char, as in "one or more characters depending on the size of the allocated memory at the destination".

int *arr[] // Array of pointers to (at least one) int
char *arr[] // Array of pointers to (at least one) char (a.k.a. a "C string")

The compiler doesn't differentiate specifically, but all the default str-family functions only work with char*. By tradition and convention char* is how a C string is defined. It could very well have been int* under different circumstances, or even something else entirely such as how some operating systems use UTF-16 instead of 8-bit encoding.

In both cases int* x[] and char* y[] behave exactly the same.

Upvotes: 2

Related Questions