Skip to content

Commit 88a31c9

Browse files
committed
Update example to include setting and getting ndarray elements
1 parent 19c6a7e commit 88a31c9

File tree

1 file changed

+35
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/ctor/examples/c

1 file changed

+35
-0
lines changed

lib/node_modules/@stdlib/ndarray/ctor/examples/c/example.c

+35
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,41 @@ int main() {
100100
printf( "byteLength = %lld\n", stdlib_ndarray_bytelength( x2 ) );
101101
printf( "ltr = %u\n", stdlib_ndarray_dtype_char( stdlib_ndarray_dtype( x2 ) ) );
102102

103+
// Set values in the underlying byte array:
104+
int64_t sub[] = { 0 };
105+
void *ptr = stdlib_ndarray_get_ptr( x2, sub );
106+
if ( ptr == NULL ) {
107+
printf( "Unable to resolve data pointer.\n" );
108+
exit( 1 );
109+
}
110+
*(double *)ptr = 1.0;
111+
112+
sub[ 0 ] = 1;
113+
ptr = stdlib_ndarray_get_ptr( x2, sub );
114+
if ( ptr == NULL ) {
115+
printf( "Unable to resolve data pointer.\n" );
116+
exit( 1 );
117+
}
118+
*(double *)ptr = 2.0;
119+
120+
sub[ 0 ] = 2;
121+
ptr = stdlib_ndarray_get_ptr( x2, sub );
122+
if ( ptr == NULL ) {
123+
printf( "Unable to resolve data pointer.\n" );
124+
exit( 1 );
125+
}
126+
*(double *)ptr = 3.0;
127+
128+
for ( int64_t i = 0; i < stdlib_ndarray_length( x2 ); i++ ) {
129+
ptr = stdlib_ndarray_iget_ptr( x2, i );
130+
if ( ptr == NULL ) {
131+
printf( "Unable to resolve data pointer.\n" );
132+
exit( 1 );
133+
}
134+
const double v = *(double *)ptr;
135+
printf( "data[%lld] = %f\n", i, v );
136+
}
137+
103138
// Free the allocated memory:
104139
free( x2 );
105140
}

0 commit comments

Comments
 (0)