Is the & operator is essential to use as address operator [duplicate]

#include<stdio.h>
int main()
{
    int a[5]={5,10,15,20,25};
    int *p;
    int i;
    p=a;
    for (i=0;i<=5;i++)
    {
        printf("the address of the %d is = %dn",*p,p);
        p++;    
    }
return 0;
}

In my program i do not even know that what was happening there because if put the address of operator in seventh line of the code which is =(p=&a) else I do not put = (p=a) the address of operator the answer of the running time of the both the case are same.

I was expecting that may be the answer is different in both cases but on both time the answer is same.

New contributor

khushal dak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

For starters this for loop

for (i=0;i<=5;i++)
{
    printf("the address of the %d is = %dn",*p,p);
    p++;    
}

invokes undefined behavior because the valid range of elements of the array a is [0, 5 ) instead of [0, 5]. That is when i is equal to 5 the expression *p tries to access memory outside the array a that results in undefined behavior. And to output a pointer you should use conversion specifier p instead of d. Otherwise your code will have again undefined behavior.

At least you should write the for loop like

for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
    //...
}

As for your question then the address of an array and the address of its first element are the same. Though relative to your code expressions a and &a used as initializers in this statement like

p=a;

or

p=&a;

have differernt types. The expression a has the type int * while the expression &a has the type int ( * )[5]. So for this statement

p=&a;

the compiler should issue an error or warning message that there are used incompatible pointer types.

Pay attention to that arrays used in expressions with rare exceptions (as for example used with the sizeof operator) are implicitly converted to pointers to their first elements.

i do not even know that what was happening there because if put the
address of operator in seventh line of the code which is =(p=&a) else
I do not put = (p=a) the address of operator the answer of the running
time of the both the case are same.

It is because arrays decay to the reference to its first element. When you use & you get the same reference but it has a different type.

int main(void)
{
    int array[5];

    int *pointer_to_int = array;
    int *pointer_to_int1 = &array[0];

    int (*pointer_to_array)[5] = &array;

    printf("%p %p %p %p %zun", (void *)pointer_to_int, (void *)pointer_to_int1, (void *)(pointer_to_int + 1), (void *)(pointer_to_int1 + 1), sizeof(*pointer_to_int));
    printf("%p %p %zun", (void *)pointer_to_array, (void *)(pointer_to_array + 1), sizeof(*pointer_to_array));
}
  • pointer_to_int & pointer_to_int1 have type of pointer to int and reference first element of the array
  • pointer_to_array has type of pointer to array of 5 int elements.

When you run the posted program you will get the result (the actual addresses can be different):

0x7ffef9ea1320 0x7ffef9ea1320 0x7ffef9ea1324 0x7ffef9ea1324 4
0x7ffef9ea1320 0x7ffef9ea1334 20

as you can see the pointer arithmetics behaves differently.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật