[英]Pointer's value is not an address?
I have a CGI script (C program test.elf
) that gets called by GET method.我有一个由 GET 方法调用的 CGI 脚本(C 程序
test.elf
) 。 This means that data is appended to url address that points to the same CGI script.这意味着数据被附加到指向相同 CGI 脚本的 url 地址。 For example:
例如:
http://.../cgi/c/test.elf?m=20&n=2000
Here we send data set m=20&n=2000
which is sepparated from the rest of the url with a question mark ?
这里我们发送数据集
m=20&n=2000
,它与 url 的其余部分用问号?
. . Single variables in a data set are also sepparated with
&
.数据集中的单个变量也用
&
分隔。 So actually we are sending m=20
and n=2000
.所以实际上我们正在发送
m=20
和n=2000
。
CGI interface automatically stores sent data set m=20&n=2000
in an enviromental variable QUERY_STRING
for the time of the call and I want my CGI script to first print it's (a) value and (b) address. CGI 接口在调用时自动将发送的数据集
m=20&n=2000
在环境变量QUERY_STRING
中,我希望我的 CGI 脚本首先打印它的 (a) 值和 (b) 地址。
This is the CGI script:这是 CGI 脚本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
// getenv() returns a pointer to character (char*) and we need same type (char*) to store it.
char* data;
// We need an "array of characters" which is actualy the same as "string of characters".
// Because strcpy() can only take pointer to the start of the array we also define another
// pointer and point it to array.
char stored_enviromental_variable[1000];
char* pointer;
pointer = &stored_enviromental_variable[0];
// ASCII HTML header
printf("content-type:text/html; charset=utf-8\n\n");
printf("<h1>Multiplication result</h1>");
// Pointer now points to enviromental variable.
data = getenv("QUERY_STRING");
// We store the value of pointer
strcpy(pointer, data);
// We print some information.
printf("%s: %s<br>", "Value of the \"QUERY_STRING\"", stored_enviromental_variable);
printf("%s: %x<br>", "Address of the \"QUERY_STRING\"", data);
}
This compiles with a warning:编译时发出警告:
[ziga@localhost ~]$ gcc -Wpedantic -std=c18 -Wall -o test.elf test.c
test.c: In function ‘main’:
test.c:42:15: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘char *’ [-Wformat=]
printf("%s: %x<br>", "Address of the \"QUERY_STRING\"", data);
~^ ~~~~
%s
So it looks like this source code is causing the problem:所以看起来这个源代码导致了问题:
printf("%s: %x<br>", "Address of the \"QUERY_STRING\"", data);
Which is weird to me, because I thought that "pointer always stores location" .这对我来说很奇怪,因为我认为“指针总是存储位置” 。 So, why does
data
store a character?那么,为什么
data
存储一个字符呢? How can I remove the warning?我怎样才能消除警告?
If I call the CGI script in the browser where I get this:如果我在浏览器中调用 CGI 脚本,我会得到这个:
As the error message states, the %x
format specifier to printf
expects an unsigned int
argument, but you are instead passing in data
whose type is char *
.正如错误消息所述,
printf
的%x
格式说明符需要一个unsigned int
参数,但您传入的data
类型为char *
。
To print a pointer value you should use the %p
format specifier.要打印指针值,您应该使用
%p
格式说明符。 Also, the argument must be casted to void *
:此外,必须将参数强制转换为
void *
:
printf("%s: %p<br>", "Address of the \"QUERY_STRING\"", (void *)data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.