strcpy() 显示不兼容的整数到指针转换

问题描述 投票:0回答:1
    char arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    char var;

    // Asking input from user
    for (int l = 0; l < 3; l++)
    {
        for (int k = 1; k < 3; k++)
        {
            if (k % 2 !=0)
            {
                var = get_int("Enter position for x: ");
                strcpy(arr[l][k], "x");
            }
            else
            {
                var = get_int("Enter position for o: ");
                strcpy(arr[l][k], "o");
            }
            design(var, arr);
        }
    }
arrays/ $ make tictactoe
tictactoe.c:20:24: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with & [-Werror,-Wint-conversion]
                strcpy(arr[l][k], "x");
                       ^~~~~~~~~
                       &
/usr/include/string.h:141:39: note: passing argument to parameter '__dest' here
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
                                      ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: tictactoe] Error 1
arrays c char strcpy
1个回答
0
投票

arr
定义为
char arr[3][3]
,因此
arr[l][k]
char

strcpy
需要一个
char *
,一个指向
char
的指针,更具体地说,是一个指向一系列
char
中第一个的指针,它将向其中复制一个字符串(以 NUL 结尾的
char
序列)值)。

看起来你想要

arr[l][k] = 'x';

(这表明

{{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}
应该是
arr
的初始化器。)

© www.soinside.com 2019 - 2024. All rights reserved.