You can cast with the cast function :)

>>> import ctypes
>>> x = (ctypes.c_ulong*5)()
>>> x
<__main__.c_ulong_Array_5 object at 0x00C2DB20>
>>> ctypes.cast(x, ctypes.POINTER(ctypes.c_ulong))
<__main__.LP_c_ulong object at 0x0119FD00>
>>> 
Answer from Mark Rushakoff on Stack Overflow
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 19069736 โ€บ convert-c-array-of-pointers-to-python-array-of-structures
Convert C array of pointers to Python array of structures - Stack Overflow
March 29, 2017 - Solving this problem required careful reading of Python's ctypes reference. Once the mechanism of ctypes type translation implementation was clear, it's not so difficult to get to the desired values. The main idea about pointers is that you use their contents attribute to get to the data the pointer points to. Another useful thing to know is that pointers can be indexed like arrays (it's not validated by the interpreter, so it's your own responsibility to make sure it is indeed an array).
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointers_arrays.php
C Pointers and Arrays
Use C pointers to access and step through the elements of an array.
Discussions

Create an array of pointers in Python using ctypes - Stack Overflow
I want to create a Python-datatype using ctypes that matches the C-datatype "const char**", which resembles an array of pointers. However, I'm not able to code this in Python. The simplif... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 22, 2022
Are C arrays pointers ?
Is it because arrays are not pointers and increment operator is not defined for arrays ? That is correct. Technically speaking, even in myArray++ the array is converted to a pointer. However, that pointer does not have a location in memory โ€” it is not an "lvalue". The increment operator can only be used on mutable lvalues. It's pretty much the same reason 42++ makes no sense. 42 doesn't have a location in memory either. More on reddit.com
๐ŸŒ r/C_Programming
30
43
June 5, 2024
How do you create an array of pointers in C? - Stack Overflow
The standard defines both in C11 Standard - 6.7.6.2 Array declarators and discusses subscripting in C11 Standard - 6.5.2.1 Array subscripting. A short example using an array of pointers, assigning a pointer to each row in a 2D array to an array of pointers to int, e.g. More on stackoverflow.com
๐ŸŒ stackoverflow.com
ctypes - How to convert pointer to c array to python array - Stack Overflow
I have a C++ callback function that calls into Python using ctypes. This function's parameters are a pointer to an array of double and the number of elements. There are a lot of elements, approxi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 26, 2011
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-pointers-to-strings-in-c
Array of Pointers to Strings in C - GeeksforGeeks
November 14, 2025 - // C Program to Create an Array of Pointers to Strings #include <stdio.h> int main() { // Initialize an array of pointers to strings char* arr[4] = { "C++", "Java", "Python", "JavaScript" }; int n = sizeof(arr) / sizeof(arr[0]); // Print the ...
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-pointers-arrays
Relationship Between Arrays and Pointers in C Programming (With Examples)
In this tutorial, you'll learn about the relationship between arrays and pointers in C programming. You will also learn to access array elements using pointers with the help of examples.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_array_of_pointers.htm
Array of Pointers in C
Python TechnologiesDatabasesComputer ... array holds a collection of integer variables, an array of pointers would hold variables of pointer type....
๐ŸŒ
Qq
m.abook.qq.com โ€บ read โ€บ 1036698870 โ€บ 67
C arrays and pointers_Advanced Python Programming-QQ้˜…่ฏปๅฅณ็”Ÿ้’ๆ˜ฅ็ฝ‘
C arrays have many of the same behaviors as pointers. The arr variable, in fact, points to the memory location of the first element of the array.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-pointers-in-c
Array of Pointers in C - GeeksforGeeks
July 23, 2025 - In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data type in our C program.
๐ŸŒ
DEV Community
dev.to โ€บ missmati โ€บ pointers-arrays-strings-in-c-52h3
Pointers , Arrays & Strings in C - DEV Community
October 11, 2022 - Manipulation of strings: An array of pointers to string allows greater ease in manipulating strings and performing different operations on strings. ... Data Engineer and Data Analytics professional with experience in designing and managing cloud-based data solutions using Microsoft Azure. Skilled in implementing scalable ETL pipelines, optimizing dat ... Data Structures. #programming #algorithms #codenewbie #100daysofcode Introduction to Data Structures and Algorithms with Python...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_pointer_to_an_array.htm
Pointer to an Array in C
Python TechnologiesDatabasesComputer ... ... C - Pointers vs. Multi-dimensional Arrays ... An array name is a constant pointer to the first element of the array....
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ are c arrays pointers ?
r/C_Programming on Reddit: Are C arrays pointers ?
June 5, 2024 -

Hello,

I'm new to C and would like to understand the differences between pointers and arrays. Everyone keeps telling me arrays are pointers but the following situation confuses me :

When i declare an array :

int myArray[] = {1,2,3,4};

I can get a pointer on the first element of this array:

int* myPointer = myArray;

When iterating in a for loop, i can use the following :

*(myPointer+i) = ... or

*(myArray+i) = ...

When iterating in a while loop, i can use my pointer :

*myPointer++ = ... but can't use

*myArray++ = ...

Is it because arrays are not pointers and increment operator is not defined for arrays ?

Thanks for your help ๐Ÿ™‚

Top answer
1 of 14
46
Is it because arrays are not pointers and increment operator is not defined for arrays ? That is correct. Technically speaking, even in myArray++ the array is converted to a pointer. However, that pointer does not have a location in memory โ€” it is not an "lvalue". The increment operator can only be used on mutable lvalues. It's pretty much the same reason 42++ makes no sense. 42 doesn't have a location in memory either.
2 of 14
20
Arrays are not pointers. Arrays decay to pointers when used in an expression. In C, you can say something like: target: puts("Help, I'm stuck in a loop!\n"); goto target; The identifier target is a label. A code label. A place where you can transfer control flow using goto. In C, an array is like a data label. That is, an array is a name for a region of memory. If you use the array in an expression, it resolves to the address of the first byte of the region. That's the "decay" part. But it isn't a pointer, and it isn't an address (pointer value). It's a name for the region. There is more about memory layout for arrays at Row-major Order . Increment operator The reason why *(myArray + i) works is that myArray is an address value. It is not an lvalue, it is an rvalue. It is a "right-hand-side value", in that it goes on the right-hand-side of the equal sign in an assignment: some_pointer = myArray; Remember that myArray is the name given to a region of memory. If you could somehow change the meaning of myArray, you would move the region of memory. That is not something that standard C supports. (Although, see the m* functions in on Linux...) On the other hand, if you declare a true pointer, like int *myPointer = myArray; You have created another region of memory -- the storage for myPointer -- that is treated as a single object (an lvalue). So you can overwrite the value, assign to it, increment or decrement it, etc. myPointer++; // myPointer == &myArray[1] Remember that this is a variable that holds an address. That is, it is typed to store the kind of value that myArray represents -- the address of the first element in a region of memory storing a list of elements. Why does this happen? Ultimately, this is all about speed. It seems obvious that if you say int i = 2; float pi = 3.14; short ary[2] = { 1, 6 }; Then whenever you write the expression i you are intending to expand that to be the value 2 (or whatever is in that memory location after you keep evaluating all these expressions!). Likewise, if you write the term pi you intend it to expand to 3.14. In theory, if you write the expression ary (with no brackets afterwards) it should expand to the two values {1, 6}. Except that C doesn't support array values like that. (C has added struct assignment, but it was not in the original standards. C has never supported array assignment in a standard.) In some other language, that might work. Perl and Python, for example, have a notion of lists, and have syntactic sugar for converting args to lists, and lists to args, and have sugar for extracting slices from lists, so they can express a lot more stuff, at the price of passing "heavier" parameters (a pointer is really cheap to pass). Instead, C chose to have arrays "decay" to the address of their first element. This would enable writing a simple interface for functions, that just takes an address (aka "pointer") argument plus maybe a size.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ access-array-pointer
C Program to Access Array Elements Using Pointer
Python JavaScript SQL Java HTML C C++ C# PHP Swift Kotlin TypeScript Go Rust Scala Dart R Ruby ยท Find Largest Number Using Dynamic Memory Allocation ยท C Program Swap Numbers in Cyclic Order Using Call by Reference ยท Access Array Elements Using Pointer ยท Multiply two Matrices by Passing Matrix to a Function ยท Find Transpose of a Matrix ยท
Top answer
1 of 3
9

How do you create an array of pointers in C?

To create an array of pointers in C, you have one option, you declare:

  type *array[CONST];  /* create CONST number of pointers to type */

With C99+ you can create a Variable Length Array (VLA) of pointers, e.g.

  type *array[var];   /* create var number of pointers to type */

The standard defines both in C11 Standard - 6.7.6.2 Array declarators and discusses subscripting in C11 Standard - 6.5.2.1 Array subscripting.

A short example using an array of pointers, assigning a pointer to each row in a 2D array to an array of pointers to int, e.g.

#include <stdio.h>
#include <stdlib.h>

#define COL 3
#define MAX 5

int main (void) {

    int arr2d[MAX][COL] = {{ 0 }},  /* simple 2D array */
        *arr[MAX] = { NULL },       /* 5 pointers to int */
        i, j, v = 0;

    for (i = 0; i < MAX; i++) {     /* fill 2D array */
        for (j = 0; j < COL; j++)
            arr2d[i][j] = v++;
        arr[i] = arr2d[i];          /* assing row-pointer to arr */
    }

    for (i = 0; i < MAX; i++) {     /* for each pointer */
        for (j = 0; j < COL; j++)   /* output COL ints */
            printf (" %4d", arr[i][j]);
        putchar ('\n');
    }
}

Example Use/Output

$ ./bin/array_ptr2int_vla
    0    1    2
    3    4    5
    6    7    8
    9   10   11
   12   13   14

Another fundamental of C is the pointer-to-pointer, but it is not an "Array", though it is routinely called a "dynamic array" and can be allocated and indexed simulating an array. The distinction between an "Array" and a collection of pointers is that with an Array, all values are guaranteed to be sequential in memory -- there is no such guarantee with a collection of pointers and the memory locations they reference.

So What Does int **arr[CONST] Declare?

In your question you posit a declaration of int** arr[5] = {0xbfjeabfbfe,0x...};, so what does that declare? You are declaring Five of something, but what? You are declaring five pointer-to-pointer-to-int. Can you do that? Sure.

So what do you do with a pointer-to-pointer-to-something? The pointer-to-poitner forms the backbone of dynamically allocated and reallocated collection of types. They are commonly termed "dynamically allocated arrays", but that is somewhat a misnomer, because there is no guarantee that all values will be sequential in memory. You will declare a given number of pointers to each int** in the array. You do not have to allocate an equal number of pointers.

(note: there is no guarantee that the memory pointed to by the pointers will even be sequential, though the pointers themselves will be -- make sure you understand this distinction and what an "Array" guarantees and what pointers don't)

int** arr[5] declares five int**. You are then free to assign any address to you like to each of the five pointers, as long as the type is int**. For example, you will allocate for your pointers with something similar to:

  arr[i] = calloc (ROW, sizeof *arr[i]);  /* allocates ROW number of pointers */

Then you are free to allocate any number of int and assign that address to each pointer, e.g.

  arr[i][j] = calloc (COL, sizeof *arr[i][j]); /* allocates COL ints */

You can then loop over the integers assigning values:

  arr[i][j][k] = v++;

A short example using your int** arr[5] type allocation could be similar to:

#include <stdio.h>
#include <stdlib.h>

#define ROW 3
#define COL ROW
#define MAX 5

int main (void) {

    int **arr[MAX] = { NULL },  /* 5 pointer-to-pointer-to-int */
        i, j, k, v = 0;

    for (i = 0; i < MAX; i++) { /* allocate ROW pointers to each */
        if ((arr[i] = calloc (ROW, sizeof *arr[i])) == NULL) {
            perror ("calloc - pointers");
            return 1;
        }
        for (j = 0; j < ROW; j++) { /* allocate COL ints each pointer */
            if ((arr[i][j] = calloc (COL, sizeof *arr[i][j])) == NULL) {
                perror ("calloc - integers");
                return 1;
            }
            for (k = 0; k < COL; k++)   /* assign values to ints */
                arr[i][j][k] = v++;
        }
    }

    for (i = 0; i < MAX; i++) { /* output each pointer-to-pointer to int */
        printf ("pointer-to-pointer-to-int: %d\n\n", i);
        for (j = 0; j < ROW; j++) {     /* for each allocated pointer */
            for (k = 0; k < COL; k++)   /* output COL ints */
                printf ("  %4d", arr[i][j][k]);
            free (arr[i][j]);   /* free the ints */
            putchar ('\n');
        }
        free (arr[i]);      /* free the pointer */
        putchar ('\n');
    }

    return 0;
}

You have allocated for five simulated 2D arrays assigning the pointer to each to your array of int **arr[5], the output would be:

Example Use/Output

$ ./bin/array_ptr2ptr2int
pointer-to-pointer-to-int: 0

     0     1     2
     3     4     5
     6     7     8

pointer-to-pointer-to-int: 1

     9    10    11
    12    13    14
    15    16    17

pointer-to-pointer-to-int: 2

    18    19    20
    21    22    23
    24    25    26

pointer-to-pointer-to-int: 3

    27    28    29
    30    31    32
    33    34    35

pointer-to-pointer-to-int: 4

    36    37    38
    39    40    41
    42    43    44

Hopefully this has helped with the distinction between an array of pointers, and an array of pointers-to-pointer and shown how to declare and use each. If you have any further questions, don't hesitate to ask.

2 of 3
3

An array of pointers to ints;

int x = 1;
int y = 42;
int z = 12;

int * array[3];

array[0] = &x;
array[1] = &y;
array[2] = &z;

alternate syntax

int * array[] = {&x,&y,&z};

keeping it simple. Work upwards from there

Top answer
1 of 1
34

If Data were (c_double*DataLength.value) array then you could:

a = np.frombuffer(Data) # no copy. Changes in `a` are reflected in `Data`

If Data is a POINTER(c_double) you could get numpy array using numpy.fromiter(). It is the same loop as in your question but faster:

a = np.fromiter(Data, dtype=np.float, count=DataLength.value) # copy

To create a numpy array from POINTER(c_double) instance without copying you could use .from_address() method:

ArrayType = ctypes.c_double*DataLength.value
addr = ctypes.addressof(Data.contents)
a = np.frombuffer(ArrayType.from_address(addr))

Or

array_pointer = ctypes.cast(Data, ctypes.POINTER(ArrayType))
a = np.frombuffer(array_pointer.contents)

Both methods convert POINTER(c_double) instance to (c_double*DataLength) before passing it to numpy.frombuffer().

Cython-based solution

Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

Here's C extension module for Python (written in Cython) that provide as C API the conversion function:

cimport numpy as np
np.import_array() # initialize C API to call PyArray_SimpleNewFromData

cdef public api tonumpyarray(double* data, long long size) with gil:
    if not (data and size >= 0): raise ValueError
    cdef np.npy_intp dims = size
    #NOTE: it doesn't take ownership of `data`. You must free `data` yourself
    return np.PyArray_SimpleNewFromData(1, &dims, np.NPY_DOUBLE, <void*>data)

It could be used with ctypes as follows:

from ctypes import (PYFUNCTYPE, py_object, POINTER, c_double, c_longlong,
                    pydll, CFUNCTYPE, c_bool, cdll)

import pointer2ndarray
tonumpyarray = PYFUNCTYPE(py_object, POINTER(c_double), c_longlong)(
    ("tonumpyarray", pydll.LoadLibrary(pointer2ndarray.__file__)))

@CFUNCTYPE(c_bool, POINTER(c_double), c_longlong)
def callback(data, size):
    a = tonumpyarray(data, size)
    # call scipy functions on the `a` array here
    return True

cpplib = cdll.LoadLibrary("call_callback.so") # your C++ lib goes here
cpplib.call_callback(callback)

Where call_callback is: void call_callback(bool (*)(double *, long long)).

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_pointers_and_arrays.htm
Pointers and Arrays in C
In this code snippet, "b" is an integer pointer that stores the address of an integer variable "a" โˆ’ ... In case of an array, you can assign the address of its 0th element to the pointer.
๐ŸŒ
Vaia
vaia.com โ€บ pointer array c
Pointer Array C: Programming & Definition | Vaia
These elements form the backbone of C's functionality. Pointers and arrays in C are related but distinct concepts. A pointer is a variable that holds the memory address of another variable, while an array is a collection of elements stored in contiguous memory locations.
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ array-of-pointers-in-c
Array of Pointers in C - C Programming Tutorial - OverIQ.com
The following program demonstrates how to use an array of pointers. ... Notice how we are assigning the addresses of a, b and c. In line 9, we are assigning the address of variable a to the 0th element of the of the array. Similarly, the address of b and c is assigned to 1st and 2nd element respectively.
๐ŸŒ
guvi.in
studytonight.com โ€บ c โ€บ pointers-with-array.php
Pointer to Array in C Programming
Learn how pointers interact with arrays in C and how array names behave as pointers in memory.