answersLogoWhite

0


Best Answer

Pointers were always difficult subject to understand. I would suggest to read several tutorials and spend some time playing with them.

Explanation:

/* create array of two pointers to integer */

int *aaa[2];

/* setting first pointer in array to point to 0x1212 location in memory */

aaa[0] = (int *)0x1212;

/* allocate memory for the second pointer and set value */

aaa[1] = malloc(sizeof(int));

*aaa[1] = 333;

/* if you want to can make first item to point to the second,

now arr[0] pointer address changed to the same as aaa[1] pointer */

aaa[0] = aaa[1];

/* print aaa[1] address and later print value */

printf("LOCATION: %X\n", (unsigned int)*(aaa + 1));

printf("VALUE: %d\n", **(aaa + 1));

/* exact the same here, but different way of how I wrote pointers */

printf("LOCATION: %X\n", (unsigned int)aaa[1]);

printf("VALUE: %d\n", *aaa[1]);

If you would do the same with 0 element of array, which points to 0x1212 memory location you would get some kind of number or Bus error if you accessed restricted memory place (for example the beginning of memory).

I hope this I did not make you more confused than you are.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

You use the assignment operator.

For example:

int * p[10]; // Declare an array of 10 pointers to int.

int x = 42; // Declare an int named x.

p[0]=&x; // Assign the address of x to the first element of the array.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Use the addressof operator (&) on the elements you want to point at.

The following code demonstrates an array of 10 integers and 10 separate pointers, one pointing at each element in the array. As you will see, the pointers don't provide any information you can't get from the array itself; the data in the first two columns is exactly the same as the data in the last two columns. The middle column simply shows the address of each pointer (pointer's are just variables, after all).

#include <iostream>

int main()

{

int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int * p0 = &arr[ 0 ]; printf( "&arr[0]=0x%.8x\tarr[0]=%.2d\t&p0=0x%.8x\tp0=0x%.8x\t*p0=%.2d\n", &arr[0], arr[0], &p0, p0, *p0 );

int * p1 = &arr[ 1 ]; printf( "&arr[1]=0x%.8x\tarr[1]=%.2d\t&p1=0x%.8x\tp1=0x%.8x\t*p1=%.2d\n", &arr[1], arr[1], &p1, p1, *p1 );

int * p2 = &arr[ 2 ]; printf( "&arr[2]=0x%.8x\tarr[2]=%.2d\t&p2=0x%.8x\tp2=0x%.8x\t*p2=%.2d\n", &arr[2], arr[2], &p2, p2, *p2 );

int * p3 = &arr[ 3 ]; printf( "&arr[3]=0x%.8x\tarr[3]=%.2d\t&p3=0x%.8x\tp3=0x%.8x\t*p3=%.2d\n", &arr[3], arr[3], &p3, p3, *p3 );

int * p4 = &arr[ 4 ]; printf( "&arr[4]=0x%.8x\tarr[4]=%.2d\t&p4=0x%.8x\tp4=0x%.8x\t*p4=%.2d\n", &arr[4], arr[4], &p4, p4, *p4 );

int * p5 = &arr[ 5 ]; printf( "&arr[5]=0x%.8x\tarr[5]=%.2d\t&p5=0x%.8x\tp5=0x%.8x\t*p5=%.2d\n", &arr[5], arr[5], &p5, p5, *p5 );

int * p6 = &arr[ 6 ]; printf( "&arr[6]=0x%.8x\tarr[6]=%.2d\t&p6=0x%.8x\tp6=0x%.8x\t*p6=%.2d\n", &arr[6], arr[6], &p6, p6, *p6 );

int * p7 = &arr[ 7 ]; printf( "&arr[7]=0x%.8x\tarr[7]=%.2d\t&p7=0x%.8x\tp7=0x%.8x\t*p7=%.2d\n", &arr[7], arr[7], &p7, p7, *p7 );

int * p8 = &arr[ 8 ]; printf( "&arr[8]=0x%.8x\tarr[8]=%.2d\t&p8=0x%.8x\tp8=0x%.8x\t*p8=%.2d\n", &arr[8], arr[8], &p8, p8, *p8 );

int * p9 = &arr[ 9 ]; printf( "&arr[9]=0x%.8x\tarr[9]=%.2d\t&p9=0x%.8x\tp9=0x%.8x\t*p9=%.2d\n", &arr[9], arr[9], &p9, p9, *p9 );

return( 0 );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you Assign an address to an element of pointer array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you return an array from function?

By returning a pointer to the first element of the array.


What is traversing arrays?

Traversing an array simply means visiting every element of the array exactly once. Pointers facilitates this job. A pointer is first created to contain the base address of the array thereby pointing the first element. As the pointer is incremented, it points to the very next element and so on till the last element is visited. Thus the elements are visited sequentially in the same order as they are stored in the array.


How can you access an array without using the suffix operator?

An array's name implicitly converts to a pointer to the first element of the array at the slightest provocation. Thus to access the first element of the array, the array name suffices. To access any other element in the array without using the suffix operator, use offset pointer arithmetic. For example: int a[] = {2, 4, 6, 8, 10}; int b; b = *(a+3); assert (b == 8); Here, (a+3) points to the 4th element (offset 3). Dereferencing this address returns the value of that element, in this case 8.


Does mentioning the array name gives the base address in all the contexts?

Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". There is, however, one very important distinction. While an array name is referentially the same as a pointer, it is not a pointer in that it does not occupy program referential space in the process. This means that, while you can change the value of a pointer, and thus the address to which it points, you can not change the value of an array name. This distinction is what we call R-Value (array or pointer) as opposed to L-Value (pointer only), i.e. can the object appear on the left sign of an assignment operator.


What is the base address of an array in c program?

the address of variable (pointer) that contains array

Related questions

Why does array index begins with 0 not with 1?

It is because array name implies its address and if you want to access first element of it pointer address logic is as below: Arrays first element address = array base address + 0 Arrays second element address = array base address + 1


Array of pointers?

a pointer is a variable that contains memory location of another variable.the value u assign to the pointers are memory address of other variable.


How do you return an array from function?

By returning a pointer to the first element of the array.


What is a pointer in the array?

A pointer into an array of elements of type E is a pointer to a single element of type E:typedef ..... E;E array[123];E* const pointer = &array[18]; // points to the 19th element inside 'array'An array of pointers is an array whose elements are pointers:typedef .... E;E* array[123];E** const pointer = &array[18]; // points to the 19th pointer within 'array'Referencing the name of the array variable without use of the index operator itself is a constant pointer to its first element. Therefore, the following if-clause is always true:typedef .... E;E array[123];if (array &array[N]) { // ALWAYS true ...}


What is traversing arrays?

Traversing an array simply means visiting every element of the array exactly once. Pointers facilitates this job. A pointer is first created to contain the base address of the array thereby pointing the first element. As the pointer is incremented, it points to the very next element and so on till the last element is visited. Thus the elements are visited sequentially in the same order as they are stored in the array.


What is the relationship between an array and a pointer?

All variable names are an alias for the value stored at the memory address allocated to them. To get the memory address itself, you must use the address of operator (&). The value returned from this can then be stored in a pointer variable.Arrays are different. The array name is an alias for the start address of the array, thus you do not need the address ofoperator to obtain the memory address (although you can if you want to). This means that when you pass an array name to a function, you pass the memory address of the array rather than passing the array itself (which would require the entire array to be copied, which is a highly inefficient way to pass an array). In essence, the array is passed by reference rather than by value.Consider the following code. This shows how a primitive variable name differs from the name of an array of primitive variables. The final portion shows how a pointer can be used to achieve the same results you got by accessing the array elements directly from the array name itself. This is in fact how the compiler implements arrays, using pointers, but there's no need to do this in your code. Accessing array elements directly by their index is a programming convenience.#include using namespace std;int main(){int i = 10;cout


Searching array element by using pointer in c plus plus?

The name of an array can be looked as a pointer of this array,and it points to the local memory address of the first element.So we can gave the address to a pointer.The flow is an easy example to show hou to use a pointer to print an array.#include "iostream.h"void main(){char a[]="abcdefgh";char *b=a;//afor(int i=0;i


Why can't we increment an array like a pointer?

once we initialize the array variable, the pointer points base address only &amp; it's fixed and constant pointer


How do you display the contents of the memory addresses stored in an element of pointer array?

Every element of a pointer array is a pointer, therefore to access the memory being pointed at by an element, dereference the element. For example: #include&lt;iostream&gt; int main (void) { int* a[10] = {nullptr}; for (int i=0; i&lt;10; ++i) { a[i] = new int (i+1); } std::cout &lt;&lt; "Address of array:\n"; std::cout &lt;&lt; "a = 0x" &lt;&lt; &amp;a &lt;&lt; std::endl; std::cout &lt;&lt; "Address of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = 0x" &lt;&lt; &amp;a[i] &lt;&lt; std::endl; } std::cout &lt;&lt; "Value of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = 0x" &lt;&lt; a[i] &lt;&lt; std::endl; } std::cout &lt;&lt; "Dereferenced value of each element in the array:\n"; for (int i=0; i&lt;10; ++i) { std::cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; *a[i] &lt;&lt; std::endl; } for (int i=0; i&lt;10; ++i) { delete a[i]; } }


A character Array is defined as char Array 200 150 50 find the address of cell Array 125 80 20?

It is inappropriate and non-portable to consider the address or the relative address of a particular item in an array, because different compiler implementations might be different. Even within the same implementation, there might be issues due to alignment or segmentation. That said, it is perfectly legal and defined to assign a pointer to the address of an element and then perform arithmetic on the pointer, with consistent results. You should not, however, consider the "arithmetic" value of the pointer to have any particular meaning, especially since the concept of "adding one" to such a pointer means to add by the number of bytes in a single element, not (as thought) to add by one. Such "pointer arithmetic" is not advised (or defined ??) when you step beyond the bounds of any one sub-array, again, due to possible alignment issues. Consider your example of an array of char [200][150][50]. You can theoretically think of the memory layout as simply being sequential, with the left-most index varying quickest, and that might work 99.99% of the time. I go for the 100% case and treat the pointer as an opaque object, with no consideration of its internal representation.


Why address operator is not used in the case of string input If No What is the reason?

I guess by 'string' you mean a character-array; so the answer is: an array in itself is a pointer to its first element: arr==&amp;arr[0]. (Note: it is a pointer-constantant, not a pointer-variable, so you cannot change it's value: 'arr= something' is wrong.)


How can you access an array without using the suffix operator?

An array's name implicitly converts to a pointer to the first element of the array at the slightest provocation. Thus to access the first element of the array, the array name suffices. To access any other element in the array without using the suffix operator, use offset pointer arithmetic. For example: int a[] = {2, 4, 6, 8, 10}; int b; b = *(a+3); assert (b == 8); Here, (a+3) points to the 4th element (offset 3). Dereferencing this address returns the value of that element, in this case 8.