双指针与数组指针,指针类型不兼容 [英] double pointer vs pointer to array, incompatible pointer type

查看:106
本文介绍了双指针与数组指针,指针类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有:

#define _DEFAULT_SOURCE 1
#include <stdio.h>
#include <string.h>

int main(){
    char *token, org[] = "Cats,Dogs,Mice,,,Dwarves,Elves:High,Elves:Wood";
    while((token=strsep(&org,",")))
        printf("Token: %s\n",token);
}

给出错误(指针类型不兼容):

gives err (incompatible pointer type):

/usr/include/string.h:439:14: note: expected ‘char ** restrict’ but argument is of type ‘char (*)[47]’
 extern char *strsep (char **__restrict __stringp,

  1. 我知道它是不同的类型(一个内存已初始化-> org[],但该函数希望指针未初始化任何内存),但它们具有相同的行为,因此为什么仍然抱怨?

  1. I know it is different type (one has memory initialized -> org[], but the function wants pointer without any memory initialized), but they have the same behaviour, so why it complain anyway?

并且可以解释一下,对于*strsep (char **__restrict __stringp,来说,此关键字restrict__restrict是什么意思(另一方面,我假设__stringp不是内部数据类型(因为有两个下划线),但只有一个漂亮的变量名.

And can somone explain me, what is the meaning of this keyword restrict or __restrict in case of *strsep (char **__restrict __stringp, (on the other hand, I assume the __stringp is not a internal datatype (because of double underscores) but only a fancy variable name).

我认为数组存储在堆栈中,但是strsep需要一个指向堆的指针,这可以通过先org分配org然后再分配memcpy来完成,甚至更好,复制字符串通过strdup(内部在memcpy中执行).但是无论如何,strsep想要指针指向堆而不是堆栈的指针吗?两者都只是指针,仅指向不同的地址,但这不介意.

I think an array is stored in stack, but the strsep wants a pointer that points to a heap, which could be done with having org allocated with malloc and then memcpy, or even better, copy the string via strdup (which does internally memcpy). But anyway, way does strsep wants pointer that points to heap and not to stack? Both are just pointers, point only to different addresses, but that should not mind.

推荐答案

strsep函数需要一个可修改指针的地址作为其第一个参数(或NULL,在这种情况下,它什么也没做);您将其传递给数组的(固定)地址.您可以通过声明一个单独的char*变量并为其分配org数组的(地址)来解决此问题:

The strsep function requires the address of a modifiable pointer as its first argument (or NULL, in which case it does nothing); you are passing it the (fixed) address of an array. You can fix this by declaring a separate char* variable and assigning to that the (address of the) org array:

int main()
{
    char* token, org[] = "Cats,Dogs,Mice,,,Dwarves,Elves:High,Elves:Wood";
    char* porg = org; // "porg" is a MODIFIABLE pointer initialized with the start address of the "org" array
    while ((token = strsep(&porg, ",")))
        printf("Token: %s\n", token);

    return 0;
}

Linux手册页中(用我的话说) :

From the Linux manual page (bolding mine):

如果*stringpNULL,则strsep()函数返回NULL并执行 没有其他的.否则,此函数将在 字符串*stringp,由字符串中的字节之一分隔 delim.通过用覆盖定界符来终止此令牌. 空字节('\ 0'), *stringp被更新为指向令牌之后 .在 如果未找到定界符,则将令牌视为整个 字符串*stringp*stringp制成NULL.

If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp, that is delimited by one of the bytes in the string delim. This token is terminated by overwriting the delimiter with a null byte ('\0'), and *stringp is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

关于restrict关键字的含义和用法,这可能会有所帮助:实际使用C99'restrict'关键字吗? .

On the meaning and use of the restrict keyword, maybe this will help: Realistic usage of the C99 'restrict' keyword?.

这篇关于双指针与数组指针,指针类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆