Can you assign a different address to an Array tag ?
Last Updated :
13 Nov, 2023
What is Array Tag?
Array tag allows you to store values with single memory and read elements as an array. Array Tag only points out the first element of the object.
Can we assign a different address to an Array tag?
It is not possible to assign a different address to an array tag.
We cannot assign an assignment operator to the Array Tag of that object. It can be achieved only with a pointer. We can assign a different address with the help of a pointer.
What happens if we assign a different address to Array tag?
If we assign different addresses to the array tag it can’t read the addresses of that tag. So it will not read all the addresses in the array tag.
Declaration:
int a[10];
int *pa = &a[0]
It assigns the memory address of a[0] to a pointer of type int. This type of declaration needs to be done to store different addresses but in the case of an array tag, it allows to store a set of values with a single timestamp and single quality and then read the elements back individually or as an array.
An Alternate way to assign different address to Array tag:
On retrieval, if specify only the tag name, then all elements are returned with the declaration, “ArrayTagName [ ArrayIndex ]” but different addresses storing is not possible with the array tag.
However, you can use a pointer variable to point to the array, and then assign a different address to the pointer variable. This would change the address of that the pointer points to, but not the address of that the array itself. Here’s an example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int * ptr = arr;
cout << "Address of array: " << arr << endl;
cout << "Address of pointer: " << ptr << endl;
ptr = &arr[3];
cout << "Address of pointer after reassignment: " << ptr << endl;
return 0;
}
|
C
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int * ptr = arr;
printf ( "Address of array: %p\n" , ( void *)arr);
printf ( "Address of pointer: %p\n" , ( void *)ptr);
ptr = &arr[3];
printf ( "Address of pointer after reassignment: %p\n" , ( void *)ptr);
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int [] ptr = arr;
System.out.println( "Address of array: " + arr);
System.out.println( "Address of pointer: " + ptr);
ptr = new int [] {arr[ 3 ]};
System.out.println( "Address of pointer after reassignment: " + ptr);
}
}
|
Python3
arr = [ 1 , 2 , 3 , 4 , 5 ]
ptr = arr
print ( "Address of array:" , id (arr))
print ( "Address of pointer:" , id (ptr))
ptr = arr[ 3 ]
print ( "Address of pointer after reassignment:" , id (ptr))
|
C#
using System;
class Program
{
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
Console.WriteLine($ "Address of array: {GetHashCode(arr)}" );
int index = 0;
Console.WriteLine($ "Address of pointer: {GetHashCode(arr[index])}" );
index = 3;
Console.WriteLine($ "Address of pointer after reassignment: {GetHashCode(arr[index])}" );
}
static int GetHashCode( object obj)
{
return obj.GetHashCode();
}
}
|
Javascript
const arr = [1, 2, 3, 4, 5];
let ptr = arr;
console.log( "Address of array: " + arr);
console.log( "Address of pointer: " + ptr);
ptr = [arr[3]];
console.log( "Address of pointer after reassignment: " + ptr);
|
Output
Address of array: 0x7ffcf999f600
Address of pointer: 0x7ffcf999f600
Address of pointer after reassignment: 0x7ffcf999f60c
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...