Nope, JavaScript doesn't give you that kind of memory level access.
The best thing you can do (which does create a copy) is abuse slice if you want to pass around parts of the array starting at a given spot. Obviously, this only works if you move forward in the array, not backward.
var arr = [1, 2, 3, 4, 5];
// Kinda, sorta but not really create a pointer at a given point in the array
// It doesn't actually refer to the original array. It creates a copy of a subsection of the array
var pretendPointerButNotAtAll = arr.slice(2);
// Almost like doing pointer arithmetic. You get the same value as doing arr[4]
// or arr + 4 in a language with pointers
var elementAtOffset = pretendPointerButNotAtAll[2];
console.log(elementAtOffset);
Answer from Mike Cluck on Stack OverflowNope, JavaScript doesn't give you that kind of memory level access.
The best thing you can do (which does create a copy) is abuse slice if you want to pass around parts of the array starting at a given spot. Obviously, this only works if you move forward in the array, not backward.
var arr = [1, 2, 3, 4, 5];
// Kinda, sorta but not really create a pointer at a given point in the array
// It doesn't actually refer to the original array. It creates a copy of a subsection of the array
var pretendPointerButNotAtAll = arr.slice(2);
// Almost like doing pointer arithmetic. You get the same value as doing arr[4]
// or arr + 4 in a language with pointers
var elementAtOffset = pretendPointerButNotAtAll[2];
console.log(elementAtOffset);
Did you want to print out the array from a certain spot to the end in the console?
var arr = [1,2,3,4];
console.log(arr);
console.log(arr.splice(2));
JS is pass-by-value, so your original assignment was this.test = the value of 1, in my example, it's this.test = the object pointed to by ptr, so when I change ptr this.test changes as well.
var foo = [],
ptr = {val: 1},
bar = function(){
this.test = ptr;
foo.push(this); // push an object (or a copy of object?) but not pointer
},
barInst = new bar(); // create new instance
// foo[0].test.val equals 1
ptr.val = 2;
// foo[0].test.val equals 2
Although if you thought that foo.push(this); was similar, it isn't. Since this is an object, the array will indeed contain "raw pointers" to objects, just like you want. You can prove this simply:
foo[0].test = 3;
// barInst.test === 3
Which shows that it is indeed a pointer to the object that was pushed onto the array
"create object method pointer"
Object.defineProperty(Object.prototype,'pointer',{
value:function(arr, val){
return eval(
"this['"+arr.join("']['")+"']"+
((val!==undefined)?("="+JSON.stringify(val)):"")
);
}
});
ex of use
var o={a:1,b:{b1:2,b2:3},c:[1,2,3]}, arr=['b','b2']
o.pointer(arr) // value 3
o.pointer(['c',0], "new_value" )
High level languages, and in particular scripting languages, tend to reference most things with pointers, and they make pointer access transparent. Javascript does this also. Most everything, even primitives like numbers and strings, are objects. Objects in javascript have properties that store things. Those properties are essentially pointers, in that they are references to other objects. Arrays are implemented in the same way, and are in fact objects with numeric properties (and a few utility methods a standard object doesn't have, such as .length, .push(), .map(), etc.). Arrays don't hav a fixed size anymore than objects do. So everything in javascript is stored in these object "buckets" that can store anything in their properties (although you can seal objects, like numbers and strings, so that they don't accidentally change).
Languages with fixed data types (C like languages for instance) implement things with fixed data structures, and the exact size is easily calculable and known. When you declare a variable, the compiler uses the type of that variable to reserve some space in memory. Javascript handles all that for you and doesn't assume anything is a fixed size, because it can't. The size of javascript objects can change at any time.
In C-Like languages, when you ask for an array, you are asking for a block of a specific size. The compiler needs to know how big that is so that it can determine where in memory to put everything, and it can use the type of objects in the array to easily calculate that. Interpreted languages use pointers behind the scenes to keep track of where everything is stored, because they can't assume it will always be in the same place, like a compiled program can. (This is somewhat of a simplification and there are caveats to this of course).
You do not have to define the array size before you assign the variables. You can go like:
let array = [];
array.push(12);
array.push("asd");
array.push({data:5});
array.forEach(element => {
console.log(element);
});
Also I think you should not think about pointers with such a high level language. The better way is to look at variables like 'primitives' and 'objects'. Here is a good read about it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
You can just place references to your functions in an array. For example:
function func1() { alert("foo"); }
function func2() { alert("bar"); }
function func3() { alert("baz"); }
var funcs = [ func1, func2, func3 ];
funcs0; // "foo"
Of course, you can just as easily use anonymous functions like this:
var funcs = [
function() { alert("foo"); },
function() { alert("bar"); },
function() { alert("baz"); } ];
funcs0; // "foo"
There are no such things as pointers in JavaScript. Therefore there's no need "dereference". You can use functions and put them in arrays however you like:
var fns = [ f1, f2, function() { console.log('!'); } ];
You can access and call them by doing:
fns2;
Yes, you can do this. I know of a few ways.
The first one would be to use eval(), but I'm not going to discuss it since I think eval() is evil.
One way to do it is to know which scope you are using. If you are using the window scope, you can always do window[myPointer][1]. If you are using a different scope, it's as easy as doing scope[myPointer][1].
If you don't have a scope or are unwilling to poison your window scope, you can always use something like this:
function blork (pointer) {
var arrays = {
number : [ 0, 1, 2, 3, 4, 5 ],
letters : [ 'a', 'b', 'c', 'd', 'e' ]
}
return arrays[pointer];
}
Edit: as noted in comments on other posts, this is not really a pointer. I assume you want to determine dynamically which array you want to use based on a string value.
I'm not sure why you would want to do that but you can use an object of arrays for this.
var myArray = ["a","b","c"];
var myObject = {"myArray": myArray};
var myPointer = "myArray";
console.log(myObject[myPointer][1]);
There is no difference between
int *p[4];
and
int *(p)[4];
Both declare p to be an array of 4 pointers.
int x;
p[0] = &x;
is valid for both.
int (*p)[4];
declares p to be a pointer to an array of 4 ints.
You can get more details on the difference between
int *p[4];
int (*p)[4];
at C pointer to array/array of pointers disambiguation.
int (*p)[4]:(*p)is an array of 4int=>ppointer to an array (array of 4int)int *p[4]=int * p[4]:pis an array of 4int *int *(p)[4]: same as the second
In your case, you should the second form.
The correct answer is:
int* arr[MAX];
int* (*pArr)[MAX] = &arr;
Or just:
int* arr [MAX];
typedef int* arr_t[MAX];
arr_t* pArr = &arr;
The last part reads as "pArr is a pointer to array of MAX elements of type pointer to int".
In C the size of array is stored in the type, not in the value. If you want this pointer to correctly handle pointer arithmetic on the arrays (in case you'd want to make a 2-D array out of those and use this pointer to iterate over it), you - often unfortunately - need to have the array size embedded in the pointer type.
Luckily, since C99 and VLAs (maybe even earlier than C99?) MAX can be specified in run-time, not compile time.
Should just be:
int* array[SIZE];
int** val = array;
There's no need to use an address-of operator on array since arrays decay into implicit pointers on the right-hand side of the assignment operator.