Warm tip: This article is reproduced from stackoverflow.com, please click
c python

what do two square brackets next to eachother do in C

发布于 2020-03-27 15:39:35

what do two square brackets next to eachother do in C, Is it the same as it is in python?

In python it would be

lst = [1,2,3,[1,2,3]]
lst[3][1]

and the second line would give 2.

In C does it function the same?

Questioner
gm2213
Viewed
17
Masklinn 2020-01-31 15:16

In python it would be lst = [1,2,3,[1,2,3]] lst[3][1] and that would be 2 in the second list. In C does it function the same?

Yes and no. Well more like no and yes really:

  • C array literal are nothing like Python's, and they are not bracketed, and they're statically typed
  • assuming lst is an array of arrays, lst[3][1] will indeed return the second element of the 4th, however
  • the semantics of indexing in C are very different (and a lot more error prone) than Python's
  • in fact the semantics of C arrays in general are very, very different from the semantics of Python's list, and assuming they work similarly is outright dangerous

If you have to work with C, you really should learn C not just try to muddle through by semi-random equivalence. C is not a big language, but it's neither easy nor forgiving.