简体   繁体   English

错误:无法将 'char (*)[6]' 转换为 'char**'

[英]error: cannot convert ‘char (*)[6]’ to ‘char**’

So my problem is the following, im trying to pass an array of strings and trying to find a specific one but for some reason the compiler is giving me this error and i dont get it because im clearly passing an array of strings into the function.所以我的问题如下,我试图传递一个字符串数组并试图找到一个特定的字符串,但由于某种原因,编译器给了我这个错误,我没有得到它,因为我清楚地将一个字符串数组传递到 function 中。

program:程序:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

int find_uid(char* users[],char* uid)
{
    for (int i=0;i<32;i++)
    {
        if (strcmp(users[i],uid) == 0)
            return 1;
    }
    return 0;
}

char users[32][6];

int main()
{
    char* user;
    user = new char[6];
    strcpy(user,"10014");
    strcpy(users[6],user);
    printf("Result %d",find_uid(users,"10014"))
}

For starters using strcpy in this code snippet对于在此代码段中使用strcpy的初学者

user = new char[5];
strcpy(user,"10014");

invokes undefined behavior because the dynamically allocated array does not have a space to store the terminating zero character of the string literal "10014" .调用未定义的行为,因为动态分配的数组没有空间来存储字符串文字"10014"的终止零字符。 You need to allocate an array at least of 6 elements您需要分配至少6元素的数组

user = new char[6];

The two-dimensional array users declared like二维数组用户声明为

char users[32][5];

used as a function argument is converted implicitly to a pointer to its first element of the type char ( * )[5] .用作 function 参数被隐式转换为指向其类型char ( * )[5]的第一个元素的指针。

On the other hand, the corresponding function parameter has the type char ** .另一方面,对应的 function 参数的类型为char ** These pointer types are not compatible.这些指针类型不兼容。 So the compiler issues an error.所以编译器发出错误。

You need to declare the function at least like您至少需要声明 function

int find_uid( char ( *users )[5],char* uid);

Or i is even better to declare the function like或者我更好地声明 function 之类的

bool find_uid( const char users[][5], size_t n, const char *uid );

Again the second dimension of the array should be increased再次增加数组的第二维

char users[32][6];

if you want to use the string literal "10014" in the array.如果要在数组中使用字符串文字"10014"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM