#include<stdio.h>
struct book
{
                char name[25] ;
                char author[25] ;
                int callno ;
} ;
int main()
{
        struct  book b1={ "Let us C", "YPK", 101 } ;


        printf("name=%s",b1.name);
        printf("\nname=%s",&b1.name);
        return 0;
}

can someone explain me why i am getting the same output while i am using two different printf() statement to print the structure member.

printf("name=%s",b1.name);
printf("\nname=%s",&b1.name);

when i run the program i get
[root@centos cpp]# ./a.out
name=Let us C
name=Let us C

printf("name=%s",b1.name);
printf("\nname=%s",&b1.name);

Technically the second statement is undefined behavior because the %s modifier expects a char pointer, but you pass a pointer to a char pointer. The types do not match. In reality it will probably work because b1.name and &b1.name should both evaluate to the same address even if the types are different. printf() will get the address and cast it to the expected type, so both printf() calls print a char pointer using the same address.

But it is not guaranteed to work, so you should not use the second printf() statement.

Aarray variable is converted to a pointer to the first item in the array. In both the cases, b1.name and &b1.name represent an address of 1st element of name member.

printf("\n%u  %u",b1.name,&b1.name);

To further what Tom Gunn said, if you changed char name[25] to char *name; then the second printf would immediately be very wrong indeed, and not just ever so slightly odd.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.