我对C很新,我很难得到所有这些指针的挂起。换句话说,取消引用操作符和&符号操作符。我相信,在盯着他们一段时间之后,我开始明白他们是如何工作的。例如:函数返回一个指针
int x = 1; //this creates an integer variable called x and assigns one to it
int *pointer; //this creates a pointer variable that will point to an integer
pointer = &x; //now the variable pointer points to x
*pointer = 0 //the value that pointer points to is now zero
我很确定我明白这是如何工作的。
我正在通过Kernighan和Ritchie的“The C Programming Language”阅读,但是我对第5.4节(地址算术)中的一些代码感到困惑。我应该指出,我了解地址算术,所以我的问题与此无关。下面的函数中的代码也不是我的问题。在线搜索有关此代码的问题会生成大量帖子,人们不知道if/else语句中的代码如何运行。无论如何,我的问题是关于函数名称本身。
#define ALLOCSIZE 1000
static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;
char *alloc(int n) /*return pointer to n charachters*/
{
if (allocbuf + ALLOCSIZE - allocp >=n)
{
allocp += n;
return allocp - n;
}
else
return 0;
}
同样,据我所知在函数的代码,但如何做
*alloc
工作?我明白这个函数返回一个指针。这是否意味着通过让函数名称成为一个指针,我们将返回一个指向char类型的指针?如果是这样,它指向什么?在我之前的例子中,我们有
int x = 1;
int *pointer;
pointer = &x;
这里变量“指针”指向x变量。然而。在函数alloc中不指向任何东西。任何帮助,将不胜感激。
但'char * allocp' **是一个指针,它指向'allocbuf'。 “missing”'&'在这里解释[数组名称是一个指针吗?](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer) –