Home > Software design >  If int array variable return address of first element of int array then why char array variable not
If int array variable return address of first element of int array then why char array variable not

Time:01-20

Here, i attached code image how it treat in char array?

If image is not clear, then refere this code

#include <stdio.h>

int main() {
    char c[3] = {'s', 'a', 'h'};
    int a[3] = {1, 2, 3};
    printf("%c\n", c);
    printf("%d\n", a);
    return 0;
}

I tried this code but not getting expected result. Might be possible I am wrong expecting but want to know why it happening.

CodePudding user response:

In this code snippet

char c[3] = {'s', 'a', 'h'};
int a[3] = {1, 2, 3};
printf("%c\n", c);
printf("%d\n", a);

the array designators c and a used in the calls of printf are implicitly converted to pointers to their first elements of the types char * and int *. In fact you have

printf("%c\n", &c[0]);
printf("%d\n", &a[0]);

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

But the conversion specifiers c and d expect objects of the type char and int correspondingly.

You have to write instead

printf("%c\n", *c);
printf("%d\n", *a);

That is the same as

printf("%c\n", c[0]);
printf("%d\n", a[0]);

If you want to output addresses of the first elements of the arrays (that is the same values of the addresses of the whole arrays) you need to use conversion specifier p. For example

printf("%p\n", ( void * )c);
printf("%p\n", ( void * )a);

If you want to output addresses as integers you need to include headers <inttypes.h> and <stdint.h> (the last is already included in the first header) casting pointers either to the type intptr_t or uintptr_t and use appropriate macros. Here is a demonstration program.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main( void )
{
    char c[] = { 's', 'a', 'h' };
    int  a[] = { 1, 2, 3 };

    printf( "address of c is %p\n", ( void * )c );
    printf( "address of c as integer is %" PRIuPTR "\n", ( uintptr_t )c );
    printf( "address of a is %p\n", ( void * )a );
    printf( "address of a as integer is %" PRIuPTR "\n", ( uintptr_t )a );
}

The program output might look like

address of c is 00C4F8FC
address of c as integer is 12908796
address of a is 00C4F8E8
address of a as integer is 12908776

CodePudding user response:

The main reason you can't get expected result is the following.

printf("%c\n", c);

Here c is the address type not string. If you want to print the first value of char array, you should use *c, because c is the address of the first entry of an array, not the value.

But if you want to print address, print type should be %d or something else, not %c. This is why printf("%d\n", a) works.

So if you want to print values,

printf("%d\n", *a);
printf("%c\n", *c);

And if you want to print addresses, printf("%d\n", a); printf("%d\n", c);

  • Related