Is there a way to declare a char array of a fixed size in python as in C for example
char myArray[100]
I also want to initializa all the characters with NULL.
Asked
Active
Viewed 7.3k times
3

wahab
- 797
- 2
- 6
- 24
-
Maybe this thread helps: http://stackoverflow.com/questions/6142689/initialising-an-array-of-fixed-size-in-python – Michael Faisst Jan 08 '16 at 11:09
-
Or you can check this [one](http://stackoverflow.com/questions/6376886/what-is-the-best-way-to-create-a-string-array-in-python) – MaTh Jan 08 '16 at 11:10
-
1What is the benefit you're expecting? – monkut Jan 08 '16 at 11:10
-
I have a written a module in C, which has a function that writes something to an output string passed as an argument. I declare the string in python before passing it to the function such as: `buf = 'test' myFunc(buf)` Now I expected the string to grow automatically in length depending on the output provided by the function, but it seems that the length of the string is fixed by declaration. So in this case it is 4, and if fo rexample the function writes "thisisanoutput" to it, after the function returns the value of buf is "this". – wahab Jan 08 '16 at 11:15
3 Answers
6
You can't have a fixed size string. (Python doesn't work like that). But you can easily initialize a string to 100 characters:
myArray = "\0" * 100

Martin Bonner supports Monica
- 28,528
- 3
- 51
- 88
4
You can use array (an array in python have fixed type signature, but not fixed size):
>>> import array
myArray = array.array('c', ['\0' for _ in xrange(100)])
>>> myArray
array('c', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> myArray[45]
'\x00'
Notice that i initialize the default to '\0' couse in python you must initialize it with a value, and array have not fixed size (it is dynamic) but this will do.
Another option is to initialize the array and appende the values later, so instead of full of NULL (None in python) it will be just empty and grow at your will:
>>> a = array.array('c',)
>>> a
array('c')
>>> a.append('c')
>>> a
array('c', 'c')

Netwave
- 40,134
- 6
- 50
- 93
-
This did the trick, although I had to convert the array to a string first using the tostring() function. – wahab Jan 08 '16 at 11:33
-
-
3In Python 3, `'c'` is no longer a valid [type code](https://docs.python.org/3/library/array.html). – Georg Oct 06 '18 at 05:57
2
Another way is to use numpy.chararray:
import numpy as np
myArray=np.chararray(100)
myArray[:]=0 #NULL is just a zero value
Results:
>>> myArray
chararray([b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
dtype='|S1')

Tin Luu
- 1,557
- 16
- 22
-
Any clue how to remove the byte prefixes in the array while printing it out? – shiv Apr 18 '20 at 02:15
-
@shiv Yes. You can use `stdout.buffer.write`. It takes bytes as input. – z0rberg's May 06 '22 at 16:52