要获取指向数组第一个和最后一个元素的指针,我们可以使用 begin 和 end 函数:https : //stackoverflow.com/a/14595314/5761266
但是,我注意到如果省略#include <iterator>. 为什么会出现这种情况,因为这些函数是在<iterator>标题中定义的?
我使用的唯一标题是<iostream>.
我使用的程序:http : //coliru.stacked-crooked.com/a/28b2b449aae19a47
我发现在头文件"gobjects.h"中声明了这样的跟随函数:
(它来自斯坦福便携式图书馆.整个图书馆的源代码和头文件可以在这里找到:https://github.com/cs50/spl/tree/master/c)
/*
* Function: getWidth
* Usage: width = getWidth(gobj);
* ------------------------------
* Returns the width of this object, which is defined to be the width of
* the bounding box.
*/
double getWidthGObject(GObject gobj);
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是函数的名称是getWidthGObject,但在注释块中它指定了用法,就像名称一样getWidth.当我在自己的代码中调用此函数时,似乎两个名称都可以正常工作.只是为了澄清getWidth这个头文件中没有另一个名为声明的函数.
所以,我的问题是,为什么我们可以用两个不同的名称来调用这个函数,而其中较短的一个似乎从未被定义过?
我想创建一个字符串数组,所以我首先使用:
char** p = malloc(sizeof(char*) * count); // count is the number of strings
Run Code Online (Sandbox Code Playgroud)
但是当我想要初始化这些字符串时,麻烦来了:
for (int i = 0; i < count; i++)
{
char* s = malloc(size_of_each_string);
*p + i = s; // THIS STEP INDUCES ERROR
while (*s++ = *input++); // initialize by copy
}
Run Code Online (Sandbox Code Playgroud)
所以我真的很困惑.*p + i似乎是指针算术,它转移到另一个指针.一般允许指针赋值(指向同一个对象).那么为什么不允许这样的任务呢?我怎么能绕过这个来完成这个任务呢?
错误消息是:表达式不可分配.