0

The past few days I've been experimenting with Cython and I have a quick questions regarding the way c / cython handles strings as character arrays / pointers:

def function(char *string):
    for i in xrange(len(string)):
        print string[i]
        print &string[i]

Now for example, when I compile and run the code, with "abc" as an argument, I get the following answer:

97

abc

98

bc

99

c

Now my questions are:

  • Why does cython print out the ascii value for each character in string[i]?
  • Why does cython print out the suffix of the string starting at index i in &string[i]?

Thank you very much.

1
  • I believe the Cython docs recommend taking strings as str, not char *. Commented May 24, 2016 at 16:06

1 Answer 1

1

Like Python 3 bytes, when you index a char * in Cython, Cython treats the char at that index as a numeric value, not text. That's why print string[i] prints a number.

The behavior of print &string[i] is inherited from C. If the char * points to the following null-terminated string:

 |
 v
+-+-+-+-+
|a|b|c| |
+-+-+-+-+

then &string[1] is a char * that points here:

   |
   v
+-+-+-+-+
|a|b|c| |
+-+-+-+-+

which is also a null-terminated string, this one having characters bc in it. When you print it, Cython prints bc.

The Cython docs recommend not using char *s:

Generally speaking: unless you know what you are doing, avoid using C strings where possible and use Python string objects instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.