简体   繁体   English

尝试将指向“ QUERY_STRING”的char指针复制到char []变量,结果错误

[英]Trying to copy char pointer to “QUERY_STRING” to a char[] variable, getting wrong result

I am working with FastCgi, trying to generate a dynamic html webpage. 我正在使用FastCgi,尝试生成动态html网页。

I am able to get the QUERY_STRING easily enough, but I am having trouble trying to copy it into a char array. 我能够很容易地获得QUERY_STRING,但是尝试将其复制到char数组时遇到了麻烦。

If there is even a shorter way of just getting the value from QUERY_STRING, please advise because I am a little over my head. 如果您只是想从QUERY_STRING获取价值,甚至还有更短的方法,请提出建议,因为我有些烦恼。

char *queryString = getenv(ENV_VARS[7]);
char newDeviceName[64];

strncpy( newDeviceName, *queryString, sizeof(*queryString) -1);
printf("------- %c ------------", newDeviceName);

This compiles with only warnings, but once i try to load the webpage, the characters are some weird Chinese looking characters. 编译时仅带有警告,但是一旦我尝试加载网页,这些字符就是一些看起来很奇怪的中文字符。 -> ፙ ->ፙ。

Thank you in advance. 先感谢您。


EDIT: More of my code 编辑:更多我的代码

const char *ENV_VARS[] = {
    "DOCUMENT_ROOT",
    "HTTP_COOKIE",
    "HTTP_HOST",
    "HTTP_REFERER",
    "HTTP_USER_AGENT",
    "HTTPS",
    "PATH",
    "QUERY_STRING",
    "REMOTE_ADDR",
    "REMOTE_HOST",
    "REMOTE_PORT",
    "REMOTE_USER",
    "REQUEST_METHOD",
    "REQUEST_URI",
    "SCRIPT_FILENAME",
    "SCRIPT_NAME",
    "SERVER_ADMIN",
    "SERVER_NAME",
    "SERVER_PORT",
    "SERVER_SOFTWARE"
};

int main(void)
{
    char deviceName[]=ADAPTERNAME;
    time_t t;
    /* Intializes random number generator */
    srand((unsigned) time(&t));

    while (FCGI_Accept() >= 0) {

        printf("Content-type: text/html \r\n\r\n");
        printf("");
        printf("<html>\n");
        printf("<script src=\"/js/scripts.js\"></script>");

        /* CODE CODE CODE */

        printf("<p> hi </p>");
        printf("<p> hi </p>");
        char *queryString = getenv(ENV_VARS[7]);
        char newDeviceName[64];
        if (queryString == NULL)
            printf("<p> +++++ERROR++++++ </p>");
        else {
            strcpy( newDeviceName, queryString);
            newDeviceName[sizeof(newDeviceName) - 1] = 0;
            printf("<p> ------- %s ------------ </p> ", newDeviceName);
} 

SOLVED: Amateur mistake, for some reason none of my new edits went into effect until after i restart my lighttpd server. 解决:业余错误,由于某种原因,直到我重新启动lighttpd服务器后,我的新编辑才生效。

The length on your strncpy is wrong [too short], the second argument is wrong, and the format string is incorrect. 您的strncpy的长度错误[太短],第二个参数错误,格式字符串不正确。

Try this: 尝试这个:

strncpy( newDeviceName, queryString, sizeof(newDeviceName) - 1);
newDeviceName[sizeof(newDeviceName) - 1] = 0;
printf("------- %s ------------", newDeviceName);

In the call to strncpy , it expects a char * for the second argument, but you pass it a char . 在对strncpy的调用中,它期望第二个参数为char * ,但您将其传递为char

Also, the size is not correct. 另外,尺寸不正确。 *queryString is a char and has size 1. Using sizeof(queryString) is not correct either because it will return the size of a pointer. *queryString是一个char ,大小为1。使用sizeof(queryString)也不正确,因为它将返回指针的大小。 What you actually want is the size of the detination buffer. 您真正想要的是目标缓冲区的大小。

In the printf call the %c format specifier expects a char but you pass it a char * . printf调用中, %c格式说明符期望使用char但是您将其传递给char * You should instead use %s which expects a char * pointing to a null terminated string. 相反,您应该使用%s ,它期望char *指向以空终止的字符串。

So what you want to do is this: 因此,您要做的是:

strncpy( newDeviceName, queryString, sizeof(newDeviceName) -1);
newDeviceName[sizeof(newDeviceName) - 1] = 0;
printf("------- %s ------------", newDeviceName);

What you want is 你想要的是

strncpy(newDeviceName, queryString, sizeof(newDeviceName)-1);
newDeviceName[63] = '\0'; // Guarantee NUL terminator
printf("----- %s -----", newDeviceName);

So multiple problems: 所以有多个问题:

  • *queryString just gets you the first character, which strncpy tries to treat as a pointer. *queryString只为您提供第一个字符, strncpy尝试将其视为指针。
  • sizeof(*queryString) is the size of a char (ie 1) sizeof(*queryString)是一个字符的大小(即1)
  • %c prints a single character, not the string %c输出单个字符,而不是字符串

Your program has undefined behavior. 您的程序具有未定义的行为。 Read those warnings issued by the compiler . 阅读编译器发出的警告 They're important. 它们很重要。


Don't dereference the pointer when you're passing the string to strncpy() . 将字符串传递给strncpy()时,请勿取消引用指针。 When you do that, you're now passing a single char . 完成此操作后,您现在要传递一个char That's converted to a pointer when it's given to strncpy() (which is where you probably get your warning, ie passing a char to a function that expects a char* ). 将其提供给strncpy()时,该指针将转换为指针(这可能是您收到警告的地方,即,将char传递给需要char*的函数)。

You also can't get the size of an array that has decayed to a pointer using sizeof . 您也无法使用sizeof获得已经衰减为指针的数组的sizeof You're just getting the size of the pointer (which is probably either 8 or 4 bytes depending on your system). 您只是得到了指针的大小(取决于您的系统,它可能是8或4个字节)。 Since you don't know the length of the string anyway, it might even be better to just use strcpy() instead of strncpy() . 由于您仍然不知道字符串的长度,因此最好只使用strcpy()而不是strncpy()


Here's what your code probably should look like: 这可能是您的代码应为的样子:

char *queryString = getenv(ENV_VARS[7]);
char newDeviceName[64];

strcpy( newDeviceName, queryString);
printf("------- %s ------------", newDeviceName); /* use %s to print strings */

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

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