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.
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With