我学习C语言,我很难理解指针和数组。
在我阅读的教程中,我有这一行:
char* arrP1[] = { "father","mother",NULL };
我的问题是什么是arrP1?
是指向静态字符串的指针数组:
或者它是指向字符串数组的指针:
我很困惑......什么是arrP1?
答案 0 :(得分:3)
TypeErrorTraceback (most recent call last)
<ipython-input-5-cf75ce057c42> in <module>()
----> 1 df.reindex(index = 'timestamp')
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in reindex(self, index, columns, **kwargs)
2731 def reindex(self, index=None, columns=None, **kwargs):
2732 return super(DataFrame, self).reindex(index=index, columns=columns,
-> 2733 **kwargs)
2734
2735 @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs)
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/generic.pyc in reindex(self, *args, **kwargs)
2513 # perform the reindex on the axes
2514 return self._reindex_axes(axes, level, limit, tolerance, method,
-> 2515 fill_value, copy).__finalize__(self)
2516
2517 def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
2677 if index is not None:
2678 frame = frame._reindex_index(index, method, copy, level,
-> 2679 fill_value, limit, tolerance)
2680
2681 return frame
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in _reindex_index(self, new_index, method, copy, level, fill_value, limit, tolerance)
2685 new_index, indexer = self.index.reindex(new_index, method=method,
2686 level=level, limit=limit,
-> 2687 tolerance=tolerance)
2688 return self._reindex_with_indexers({0: [new_index, indexer]},
2689 copy=copy, fill_value=fill_value,
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in reindex(self, target, method, level, limit, tolerance)
2865 target = self._simple_new(None, dtype=self.dtype, **attrs)
2866 else:
-> 2867 target = _ensure_index(target)
2868
2869 if level is not None:
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in _ensure_index(index_like, copy)
4025 index_like = copy(index_like)
4026
-> 4027 return Index(index_like)
4028
4029
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in __new__(cls, data, dtype, copy, name, fastpath, tupleize_cols, **kwargs)
324 **kwargs)
325 elif data is None or is_scalar(data):
--> 326 cls._scalar_data_error(data)
327 else:
328 if (tupleize_cols and isinstance(data, list) and data and
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in _scalar_data_error(cls, data)
676 raise TypeError('{0}(...) must be called with a collection of some '
677 'kind, {1} was passed'.format(cls.__name__,
--> 678 repr(data)))
679
680 @classmethod
TypeError: Index(...) must be called with a collection of some kind, 'time' was passed
是一个arrP
数组,在本例中是一个大小为3的数组,并且您已经指定了指向初始值为char *
的c样式字符串的指针,它们本身是空终止的字符数组。所以,你的第一个答案是正确的。
答案 1 :(得分:2)
要查找此类广告的答案,您可以使用cdecl。它很可能会回答你。
declare arrP1 as array of pointer to char
然而,有一种称为spiral rule的东西。它还可以帮助您阅读decleration。例如,
char *str[10]
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
- str is an array of 10 elements
- str is an array of 10, of pointers
- str is an array of 10, of pointers, of type char
答案 2 :(得分:1)
不确定这是否会有所帮助或使事情更加混乱,但arrP1既可以是char *也可以是char **,如下所示:
pip install iconv_codecs
有趣的事情(我刚刚发现自己)是foo2没有在其堆栈上创建数组的副本,它直接传递给arr!所有3个cout都打印相同的地址。