The Wayback Machine - https://web.archive.org/web/20240819102611/https://www.geeksforgeeks.org/whats-difference-between-array-and-array-for-int-array5/
Open In App

What’s difference between “array” and “&array” for “int array[5]” ?

Last Updated : 02 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out!

To check this, the very first thing that comes to mind is the following program.

C++




// C++ Program to check whether 'array' and '&array;'
// prints the same address
 
#include <iostream>
 
int main()
{
   int array[5];
   
   std::cout << "array = " << array << " : &array; = " << &array;
 
   return 0;
}
 
// This code is contributed by sarajadhav12052009


C




#include <stdio.h>
int main()
{
   int array[5];
 
   /* If %p is new to you, you can use %d as well */
   printf("array = %p : &array; = %p\n", array, &array;);
 
   return 0;
}


Output

array = 0x7fffce606b90 : &array = 0x7fffce606b90

So you got same address for both “array” and “&array”. Again, you are tempted to think that both are same. Well, they are not! How come a variable and its & (i.e. address-of) be same. It doesn’t look logical but we saw that both “array” and “&array” are printing same address. May be it’s too soon to conclude. The crux of this post is that even though they both are resulting in same address but they are different types of addresses. And this is the difference between “array” and “&array”.

And just to show this difference, I would suggest to take a look at the following program.

C++




// C++ Program to check that adding 1 to both 'array' and '&array;'
// prints the same address or not
 
#include <iostream>
using namespace std;
 
int main()
{
   int array[5];
  
   cout << "array = " << array << " : &array; = " << &array; << endl;
 
   cout << "array + 1 = " << array + 1 << " : &array; + 1 = " << &array; + 1;
   
   return 0;
}
 
// This code is contributed by sarajadhav12052009


C




#include <stdio.h>
 
int main()
{
   int array[5];
 
   /* If %p is new to you, you can use %d as well */
   printf("array = %p : &array; = %p\n", array, &array;);
 
   printf("array + 1 = %p : &array; + 1 = %p", array + 1, &array; + 1);
 
   return 0;
}


Output

array = 0x7ffc6ab5daa0 : &array = 0x7ffc6ab5daa0
array + 1 = 0x7ffc6ab5daa4 : &array + 1 = 0x7ffc6ab5dab4

With pointer arithmetic, we know what happens when we add an integer to a pointer. So can you guess the output of the above program without running it? Shouldn’t “array+1” and “&array+1” point to same address. Well you might be surprised 🙂

Basically, “array” is a “pointer to the first element of array” but “&array” is a “pointer to whole array of 5 int”. Since “array” is pointer to int, addition of 1 resulted in an address with increment of 4 (assuming int size in your machine is 4 bytes). Since “&array” is pointer to array of 5 ints, addition of 1 resulted in an address with increment of 4 x 5 = 20 = 0x14. Now you see why these two seemingly similar pointers are different at core level. This logic can be extended to multidimensional arrays as well. Suppose double twoDarray[5][4] is a 2D array. Here, “twoDarray” is a pointer to array of 4 int but “&twoDarray” is pointer to array of 5 rows arrays of 4 int”. If this sounds cryptic, you can always have a small program to print these after adding 1. We hope that we could clarify that any array name itself is a pointer to the first element but & (i.e. address-of) for the array name is a pointer to the whole array itself.



Similar Reads

Difference between int* p() and int (*p)()?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type is the pointers base type. It must be a valid C/C
2 min read
Difference between int *a and int **a in C
In C, the declarations int *a and int **a represent two different concepts related to pointers. Pointers play a fundamental role in memory management and data manipulation in C programming so it is important to have a clear understanding of them. What does int * means? This declares a pointer to an integer. It means that the variable a can store th
3 min read
Difference between "int main()" and "int main(void)" in C/C++?
Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void). Consider the following two definitions of main(). Definition 1: C/C++ Code int main() { /* */ return 0; } C/C++ Code int main() { /* */ return 0; } Definition 2: C/C++ Code int main(void) { /* */ return 0; } C
3 min read
Difference Between Unsigned Int and Signed Int in C
Integers are typically stored as 32-bit values, but in some environments, they may contain 16-bit values (or even a different number, usually the product of two powers). For example, let's examine 4-bit integers. They are small but can help illustrate a point. signed int can represent both positive and negative values, and unsigned int can only rep
2 min read
How to Use int as long long int for Competitive Programming?
Most of the time, input constraints in Competitive programming questions are bigger than the limits of int. Therefore, there is a need to use long int or even long long int. Here let us take two cases so that if a naive user writes correct logic still input is not getting accepted can get to know where the rectification is required. Case 1: Big int
3 min read
How to Initialize a Static std::map&lt;int, int&gt; in C++
In C++, std::map&lt;int, int&gt; is a commonly used container that stores key-value pairs. There are scenarios where we may want to initialize a static std::map with predefined key-value pairs that remain constant throughout the program's execution. In this article, we will learn different methods to initialize a static std::map&lt;int, int&gt; in
4 min read
Difference Between byte, short, int and long Datatype in Java
There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types such as an array, stack, queue, tries, etc. Let's c
4 min read
Difference between an Integer and int in Java with Examples
In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.Integer is a class and thus it
6 min read
Difference between Write invalidate protocol &amp; Write Update Protocol (Bus-Snooping )
Prerequisite – Cache Coherence Cache Coherence :In multi-processor systems, each processor can have its own cache &amp; when processors are allowed to update data of their individual cache's block, data will be in an inconsistent state. This problem is called cache coherence. Cache Coherence protocol :To keep the data consistent, cache coherence pr
3 min read
Difference between Time Shared Bus, Crossbar Switch &amp; Multiport Memory - Interconnection structure
Prerequisite : Detailed study about the Time Shared Bus, Crossbar Switch &amp; Multiport Memory interconnection structure. Introduction :Interconnection structure can decide the overall system's performance in a multi-processor environment. The processors must be able to share a set of main memory modules &amp; I/O devices in a multiprocessor syste
2 min read
Difference between Datagram Switching &amp; Virtual Circuit
1. Datagram switching : Datagram packet switching is a packet switching method that treats each packet, or datagram, as a separate entity. Each packet is routed via the network on its own. It is a service that does not require a connection. Because there is no specific channel for a connection session, there is no need to reserve resources. As a re
6 min read
Difference between mysqli_fetch_array() &amp; mysqli_fetch_object() in PHP
In this article, we will see the mysqli_fetch_array() &amp; mysqli_fetch_object() function in PHP. The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data. mysqli_fetch_object() function: This function returns
3 min read
Difference between &lt;figure&gt; &amp; &lt;img&gt; tags in HTML
In this article, we will learn about the &lt;figure&gt; &amp; &lt;img&gt; tag along with its implementation. We will also learn the differences between them. Let's begin the discussion with the figure tag in HTML. Table of Content HTML &lt;figure&gt; Tag: HTML &lt;img&gt; Tag: Difference between &lt;figure&gt; and &lt;img&gt; tags:HTML &lt;figure
4 min read
Assigning multiple characters in an int in C language
Consider the following C program. #include &lt;stdio.h&gt; int main(void) { int a = 'd'; printf(&quot;%d\n&quot;, a); /*OUTPUT - 100 (ASCII Code for character d)*/ int b = 'dd'; printf(&quot;%d&quot;, b); /*OUTPUT - 25700 (Explanation in detail given below)*/ return 0; } Output : 100 25700 We can easily guess that the output for 'd' is 100 as 100 i
1 min read
Convert String to int in C++
Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we can convert numbers that are stored as strings. Exa
8 min read
C++ default constructor | Built-in types for int(), float, double()
A constructor without any arguments or with default values for every argument is treated as the default constructor. C++ allows even built-in types (primitive types) to have default constructors. The function style cast int() is analogous(similar in some way) to casting 0 to the required type.It will be called by the compiler when in need(precisely
2 min read
Implicit return type int in C
Predict the output of following C program. #include &lt;stdio.h&gt; fun(int x) { return x*x; } int main(void) { printf(&quot;%d&quot;, fun(10)); return 0; } Output: 100 The important thing to note is, there is no return type for fun(), the program still compiles and runs fine in most of the C compilers. In C, if we do not specify a return type, com
1 min read
Print a long int in C using putchar() only
Write a C function print(n) that takes a long int number n as argument, and prints it on console. The only allowed library function is putchar(), no other function like itoa() or printf() is allowed. Use of loops is also not allowed. We strongly recommend to minimize the browser and try this yourself first. This is a simple trick question. Since pu
2 min read
Representation of Int Variable in Memory in C++
To understand the Representation of int variable in memory in C/C++ below is the basic program to create a variable and its size. Usually, the value of an int variable is stored in a series of bytes that are represented as the variable's representation as memory. Depending on the hardware and compiler being used, an int variable may be represented
5 min read
Floating Point Operations &amp; Associativity in C, C++ and Java
Do Floating point operations follow property of associativity? In other words, do we always get the same results for expressions "(A + B) + C" and "A + (B + C)" One may expect that floating numbers to follow the rule of associativity in programming languages as they are associative mathematically. However, this is not true in all cases. Consider be
4 min read
Difference between Difference Engine and Analytical Engine
Introduction: The development of computing technology has a rich history, with many inventions and innovations leading to the creation of the modern computer. Two such machines, the Difference Engine and Analytical Engine, were created by the English mathematician and inventor Charles Babbage in the 19th century. While these machines share some sim
7 min read
Difference between Voltage Drop and Potential Difference
Voltage Drop is defined as the decrease in the electric potential along the path of current that is flowing in an electric circuit. Voltage drop can be assigned at each point in the electric circuit that is proportional to the electrical elevation. The amount of energy delivered per second to any component in the circuit is equal to the voltage dro
4 min read
Difference Between Electric Potential and Potential Difference
The flow of electric charges is known as electricity, and it is responsible for producing electric current. An important word associated with electricity is electric potential. A potential difference is required to create the flow of electrons and hence, produce electricity. Before understanding the difference between electric potential and potenti
7 min read
C | Loops &amp; Control Structure | Question 1
#include &lt;stdio.h&gt; int main() { int i = 1024; for (; i; i &gt;&gt;= 1) printf(&quot;GeeksQuiz&quot;); return 0; } How many times will GeeksQuiz be printed in the above program? (A) 10 (B) 11 (C) Infinite (D) The program will show compile-time error Answer: (B) Explanation: In for loop, mentioning expression is optional. &gt;&gt;= is a composi
1 min read
C | Macro &amp; Preprocessor | Question 1
#include &lt;stdio.h&gt; #define PRINT(i, limit) do \ { \ if (i++ &lt; limit) \ { \ printf(&quot;GeeksQuiz\n&quot;); \ continue; \ } \ }while(1) int main() { PRINT(0, 3); return 0; } How many times GeeksQuiz is printed in the above program? (A) 1 (B) 3 (C) 4 (D) Compile-time error Answer: (D) Explanation: The PRINT macro gets expanded at the pre-pr
1 min read
C | Loops &amp; Control Structure | Question 2
C/C++ Code #include #define PRINT(i, limit) do \\ { \\ if (i++ How many times GeeksQuiz is printed in the above program ? (A)1 (B)3 (C)4 (D)Compile-time error Answer: (A)Explanation: If a macro needs to be expanded in multiple lines, it is the best practice to write those lines within do{ }while(0) to avoid macro side effects. After GeeksQuiz is pr
1 min read
C | Loops &amp; Control Structure | Question 3
What is the output of the below program? C/C++ Code #include int main() { int i = 2; switch (i) { case 0: printf("Geeks"); break; case 1: printf("Quiz"); break; default: printf("GeeksQuiz"); } return 0; } (A) Geeks (B) Quiz (C) GeeksQuiz (D) Compile-time error Answer: (C) Explanation: As i=2, so GeeksQuiz is printed. Quiz of this Question Please co
1 min read
C | Loops &amp; Control Structure | Question 4
C/C++ Code #include int main() { int i = 3; switch (i) { case 1: printf("Geeks"); break; case 1+2: printf("Quiz"); break; default: printf("GeeksQuiz"); } return 0; } What is the output of the above program? (A) Geeks (B) Quiz (C) GeeksQuiz (D) Compile-time error Answer: (B) Explanation: Expression gets evaluated in cases. The control goes to the se
1 min read
C | Loops &amp; Control Structure | Question 5
Predict the output of the below program: C/C++ Code #include #define EVEN 0 #define ODD 1 int main() { int i = 3; switch (i % 2) { case EVEN: printf("Even"); break; case ODD: printf("Odd"); break; default: printf("Default"); } return 0; } (A) Even (B) Odd (C) Default (D) Compile-time error Answer: (B) Explanation: The following C code will print 'o
1 min read
C | Loops &amp; Control Structure | Question 6
C/C++ Code #include int main() { int i; if (printf("0")) i = 3; else i = 5; printf("%d", i); return 0; } Predict the output of above program? (A) 3 (B) 5 (C) 03 (D) 05 Answer: (C) Explanation: The control first goes to the if statement where 0 is printed. The printf(\"0\") returns the number of characters being printed i.e. 1. The block under if st
1 min read
Practice Tags :
three90RightbarBannerImg