answersLogoWhite

0

You probably mean pointer variable rather than memory variable. You create an array of pointer variables just as you would an array of any other type:

#include<stdio.h>

#include<malloc.h>

// Fixed-length arrays:

void foo1 (void) {

const unsigned n = 10;

unsigned i;

// an array of n int types

int a[n];

// an array of n pointer-to-int types

int* b[n];

// assign the address of each indexed element in a[] to the corresponding pointer element in b[]

for (i=0; i<n; ++i) b[i] = &a[i];

// assign values to integers in a[] via pointers in b[]

for (i=0; i<n; ++i) *b[i] = i * 10;

// print the values in a[]

printf ("a[] = {");

for (i=0; i<n; ++i) printf("%d%s", a[i], i<n-1?", ":"");

printf ("}\n");

// print the values pointed to by b[]

printf ("b[] = {");

for (i=0; i<n; ++i) printf("%d%s", *b[i], i<n-1?", ":"");

printf ("}\n");

}

// Variable-length arrays:

void foo2 (unsigned n) {

unsigned i;

// an array of n int types

int* a = (int*) malloc (n * sizeof (int));

// an array of n pointer-to-int types

int** b = (int**) malloc (n * sizeof (int*));

// assign the address of each indexed element in a[] to the corresponding pointer element in b[]

for (i=0; i<n; ++i) b[i] = &a[i];

// assign values to integers in a[] via pointers in b[]

for (i=0; i<n; ++i) *b[i] = i * 10;

// print the values in a[]

printf ("a[] = {");

for (i=0; i<n; ++i) printf("%d%s", a[i], i<n-1?", ":"");

printf ("}\n");

// print the values pointed to by b[]

printf ("b[] = {");

for (i=0; i<n; ++i) printf("%d%s", *b[i], i<n-1?", ":"");

printf ("}\n");

// release allocated resources

free (b);

free (a);

}

int main () {

foo1 ();

foo2 (10);

return 0;

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

Does array save memory?

No, it use memory, just like any other variable.


Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


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.


What are the limitations of a variable over advantage of array?

variable cant occupy contigious memory it may take complexity in storing vast data.this becomes easy on using the array concept....


How do arrays differ from single memory position variables?

A single memory position variable can store only one value of its type. An array can store n number of values, where n is the size of the array.


You can use a variable to declare an array's size true or false?

True and false in the same time, because even so you can declare array size using notation for variables you have use constwhich makes your variable basically a constant:const int arraySize = 10;In Java, you can use any expression to define the array size, when you create the array. Once you create an Array object, however, you can't redimension it - but you can create a new Array object and destroy the old one.


What is array in C languange?

An array is a contiguous block of data in memory. When you declare an array in C you need to give it a type and a name (like a normal variable), plus you need to give it a size. // normal integer variable x int x; // array of 10 integers int x[10]; Remember that the variable x is actually just a pointer, or reference, to a point in memory. This point in memory is the start of the array, so the value at x[0] is the first value in the array, x[1] is the second, and so on. Also remember that C has no bounds checking, so you can, indeed, read any value past the maximum. x[3474] would return an integer value, but it's going to be some part of memory that is not in your array. Attempting to change this value could result in something very bad happening.


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


What are the different between array and variables?

A variable is a reference to a single object (often a number, character, or string of characters). An array is a reference to a contiguous block of memory in which zero or more individual objects.


What is an array and how do you create one How do you access information or elements contained in an array?

An array is:simply a collection of similar objectsHow you create one: (I think)Basically you receive or copy an image and place it in an array and assign it an mage Areray name.How you access info and elementsIt can be accessed by means of a variable name and an index.


When does an array behave like a pointer in C plus plus -- provide sample code to support each case?

An array never behaves like a pointer.Understand that a pointer is a variable -- no different to any other variable, other than its type. like any other variable, it is allocated its own memory (4 bytes in a 32-bit system). Those 4 bytes can store any 32-bit value, from 0x00000000 to 0xFFFFFFFF, but because it is declared to be a pointer, that value actually represents a memory location, somewhere within the 4GB address space. 0x00000000 is a reserved memory location -- what we refer to as NULL.An array is not the same as a pointer in any sense. The array name refers to the actual memory (the starting address) where the array resides. But the array name is not a variable -- unlike a pointer, it is not separate from the memory it refers to -- it is merely an alias that refers to the memory allocated to the array itself.That said, pointers and arrays can appear to be the same. For instance, the strlen() function expects you to pass a const char pointer, and yet you can pass an array name instead and it works just fine:char cArray[12];char * p = cArray;strlen( p ); // Pass a pointerstrlen( &cArray[0] ); // Pass pointer to array element via AddressOf operatorstrlen( cArray ); // Pass an array nameAll three of these functions work exactly the same so it would be easy to assume the array is a type of pointer. But it is not. To understand what's really going on here you have to understand how pointers and arrays are actually passed to function.When you pass a pointer to a function, you don't pass the pointer itself, you pass the memory address stored in the memory allocated to the pointer. In other words, pointers are passed by value, not by reference. Similarly, when you pass an array name, you pass the starting address of the array.That's all there is to it! The function's parameter, a pointer, is allocated its own memory. It is a temporary variable, local to the function. All it expects is a memory location so whether you pass a pointer or an array name, that's exactly what it receives.


Do reference variables occupy memory in c?

All references must be non-null, therefore they will always have memory allocated to them. A reference is simply a non-null memory location that stores a specific type of variable, as determined by the reference's type. The reference name is an alias (a token) for the memory location, in much the same way that an array name is an alias for the starting address of the array. A pointer variable is different in that memory must be set aside for the pointer variable itself, in addition to the memory it actually points to. On a 32-bit system, 4 bytes must be allocated to every pointer variable, to store the memory address that they point to, which could be null. But references only occupy the memory they actually refer to, which can never be null (a null reference will in fact render the program invalid).