creates a new pointer object in compiled code, suitable for use with external libraries.
ToRawPointer[val]
creates a new object pointing to the initial value val.
ToRawPointer[p,val]
stores val in the pointer p.
ToRawPointer[array,offset,val]
stores val in the "CArray" array at the given offset.
ToRawPointer
creates a new pointer object in compiled code, suitable for use with external libraries.
ToRawPointer[val]
creates a new object pointing to the initial value val.
ToRawPointer[p,val]
stores val in the pointer p.
ToRawPointer[array,offset,val]
stores val in the "CArray" array at the given offset.
Details
- ToRawPointer is only available in compiled code.
- ToRawPointer allocates memory on the stack.
- A pointer generated by ToRawPointer is invalidated when the function in which it was generated returns.
- FromRawPointer can be used to extract the value of a pointer generated by ToRawPointer.
- ToRawPointer[val] creates a copy of val and returns its address. Changes to that copy will not affect val, and vice versa.
- ToRawPointer[array,offset,val] can store elements in an array.
- A pointer can be generated by dynamic memory allocation with CreateTypeInstance.
- The result of ToRawPointer[] will have the type .
- Usages of ToRawPointer have the following approximate equivalents in C:
-
p=ToRawPointer[] int x; int* p = &x; p=ToRawPointer[val] int x = val; int* p = &x; ToRawPointer[p,val] *p = val; ToRawPointer[array,offset,val] array[offset] = val; or *(array+offset) = val;
Examples
open all close allBasic Examples (1)
Scope (2)
Update (1)
ToRawPointer can update an existing pointer:
cf = FunctionCompile[Function[Typed[arg, "Integer64"],
With[{p = ToRawPointer[]}, ToRawPointer[p, arg];FromRawPointer[p]]
]]cf[4]Offset (1)
ToRawPointer can assign different elements of an array:
cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, Module[{arr},
arr = CreateTypeInstance[ "CArray"::["MachineInteger"], 2];
ToRawPointer[arr, 0, arg];
ToRawPointer[arr, 1, arg];
FromRawPointer[arr, 0] + FromRawPointer[arr, 0]
]]]cf[4]Applications (2)
Represent a library function that returns its results by modifying a pointer:
funcDec = LibraryFunctionDeclaration["addonePointer", "compilerDemoBase", {"CInt", "RawPointer"::["CInt"]} -> "CInt"];Compile a program that uses the function:
cf = FunctionCompile[funcDec,
Function[Typed[n, "CInt"], Module[{ptr},
ptr = Typed[ToRawPointer[], "RawPointer"::["CInt"]];
LibraryFunction["addonePointer"][n, ptr];
FromRawPointer[ptr]
]]]cf[12]Represent a library function that sums a "CArray":
funcDec = LibraryFunctionDeclaration["sumArray", "compilerDemoBase", {"CArray"::["CLong"], "CLong"} -> "CLong"];Compile a program that uses ToRawPointer to populate a "CArray" so it can be passed to "sumArray":
cf = FunctionCompile[funcDec,
Function[Typed[l, "PackedArray"::["MachineInteger", 1]],
Module[{array},
array = CreateTypeInstance["Managed"::["CArray"::["CLong"]], Length[l]];
Do[ToRawPointer[array, i - 1, Cast[l[[i]], "CLong", "CCast"]], {i, Length[l]}];
LibraryFunction["sumArray"][array, Cast[Length[l], "CLong", "CCast"]]
]]]cf[Range[10]]Possible Issues (2)
Because ToRawPointer[val] creates a pointer to a copy of val, functions that modify the pointer will not modify the original val.
Represent a function that modifies a pointer:
funcDec = LibraryFunctionDeclaration["incrementInput", "compilerDemoBase", {"RawPointer"::["CInt"]} -> "CInt"];Compile a program that initializes the pointer with a variable, calls the function that modifies the pointer and then returns the original variable:
cf = FunctionCompile[funcDec,
Function[Typed[n, "CInt"], Module[{ptr},
ptr = ToRawPointer[n];
LibraryFunction["incrementInput"][ptr];
n
]]]Because ToRawPointer[n] creates a pointer to a copy of n, and incrementInput only modifies the value referenced by the pointer, n remains unchanged:
cf[12]Pointers generated by ToRawPointer are invalidated when the function in which they were generated returns. Using a pointer after it has been invalidated can lead to crashes.
Define a function that returns an invalid pointer:
FunctionDeclaration[badFunction,
Typed[{} -> "RawPointer"::["MachineInteger"]]@
Function[{}, ToRawPointer[12]]
]Compiling a function that calls FromRawPointer on an invalid pointer can lead to crashes:
Function[{}, FromRawPointer[badFunction[]]]Text
Wolfram Research (2022), ToRawPointer, Wolfram Language function, https://reference.wolfram.com/language/ref/ToRawPointer.html (updated 2025).
CMS
Wolfram Language. 2022. "ToRawPointer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/ToRawPointer.html.
APA
Wolfram Language. (2022). ToRawPointer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ToRawPointer.html
BibTeX
@misc{reference.wolfram_2026_torawpointer, author="Wolfram Research", title="{ToRawPointer}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/ToRawPointer.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_torawpointer, organization={Wolfram Research}, title={ToRawPointer}, year={2025}, url={https://reference.wolfram.com/language/ref/ToRawPointer.html}, note=[Accessed: 13-June-2026]}