BM Balmiki Mandal 12 Sep 2023 2 views 0 comments C Programming

What happens when you try to assign an array to a pointer without using the & operator? Provide an example to illustrate.

What Happens When You Assign an Array to a Pointer Without Using the & Operator?

When you try to assign an array to a pointer without using the & operator, the compiler will implicitly add the & operator for you. This is because the name of an array is actually a pointer to the first element of the array.

Example, the following code:

C-Programming
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
is equivalent to the following code:
 
C- Programming
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
The & operator is used to get the address of a variable. In this case, the address of the first element of the array is being assigned to the pointer.

It is important to note that you cannot assign an array to a pointer of a different type. For example, the following code will not compile:

C-Programming
int arr[5] = {1, 2, 3, 4, 5};
char *ptr = arr;

 

This is because the pointer ptr is of type char *, which can only point to an array of characters. The array arr is of type int *, which can only point to an array of integers.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026
Topic-wise C Programming Interview Questions

C Programming Interview Questions – Topic Wise1. IntroductionC programming is one of the most important langu...

AG Anvesh G 30 May 2026