如何加入角色?

#include <stdio.h>
#include <string.h>

int main(void) {

    char rez[100] = "\0";
    char t = 97;
    char temp;
    strcpy(temp,t);
    strcat(rez,temp);

    printf("%s",rez);
    return 0;
}

我想将字符“ a”连接到结果中,但strcpy和strcat命令不起作用。 如果字符应该是ASCII码,该怎么办?

警告:传递'strcpy'的参数1会使指针从整数转换而无需强制转换[-Wint-conversion] strcpy(temp,t); 注意:预期为'char * strict',但参数为'char'类型 char * __cdecl strcpy(char * 限制 _Dest,const char * 限制 _Source);

如何加入角色?

iCMS 回答:如何加入角色?

两个函数都使用字符串,而不是单个字符。通常很方便的是从char创建一个长度为1的新字符串,并使用该字符串:

char str[] = { t,'\0' };

现在您可以将其连接起来。

或者,您可以通过将字符写入结果数组的正确位置来手动实现串联:

const len = strlen(rez);
rez[len] = t;
rez[len + 1] = '\0';

…,或者您可以使用sprintf函数:

sprintf(rez,"%s%c",rez,t);

在所有情况下,您都需要注意不要意外地在字符串的边界之外书写。

在您的特定情况下,这些都没有意义,因为rez开头是空的;您不是串联任何对象,而是将单个字符写入缓冲区。

rez[0] = t;
rez[1] = '\0'; // for good measure
,

strcatchar *期望它们的两个参数都具有类型t ,两个参数都指向以零结尾的字符串

问题在于temp&t都不是指针,也不是字符串的开头字符,因此传递&temprez都不会起作用。

如果要将单个字符附加到/** * Use character literals instead of raw ASCII codes - * it’s easier to understand,it will work as expected * on non-ASCII systems,and you’re less likely to make * a mistake. */ t = 'a'; size_t len = strlen(rez); /** * Overwrite the terminator with the value in t,* then write the terminator to the following element * (strictly not necessary in this case,but it never * hurts to make sure) */ rez[len++] = t; rez[len] = 0; ,则可以执行类似的操作

sprintf

您还可以使用sprintf( rez,t ); 库函数:

    <script>
        function popupForModifying() {
            let option = "width=820,height=400,toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no";
            window.open("{{ url_for('modify',content=content[2],title=content[0],category=content[4]) }}","",option);
        }
    </script>

    <article>
        <table class="table">
            <thead>
                <tr class="table-active">
                    <th class="col-8 text-left">{{ content[0] }}</th>
                    <th class="col-4"></th>
                </tr>
                <tr>
                    <td class="text-left">{{ content[4] }}&nbsp;&nbsp;
                        |&nbsp;&nbsp;{{ content[1] }}&nbsp;&nbsp;
                        |&nbsp;&nbsp;{{ content[3][0] }}</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="col-12 text-left">{{ content[2] }}</td>
                    <td class="col-0"></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <a class="btn btn-light" href="javascript:popupForModifying()">수정하기</a>
                    </td>
                </tr>
            </tfoot>
        </table>
    </article>
本文链接:https://www.f2er.com/1778267.html

大家都在问