神秘字符串 [英] Mystery String
问题描述
大家好,
这个字符串是什么?
{name =" 172.16.0.240\000\000\\ \\ 000"}
名称是用这个宣布的:
char name [16];
为什么我的数量超过16个元素?
Jonathan Shan
Hello everyone,
What is this string?
{name = "172.16.0.240\000\000\000"}
name was declared using this:
char name[16];
Why do I count more than 16 elements?
Jonathan Shan
推荐答案
Jonathan Shan发布:
Jonathan Shan posted:
大家好,
这个字符串是什么?
{name =" 172.16 .0.240\000\000\000"}
Hello everyone,
What is this string?
{name = "172.16.0.240\000\000\000"}
这实际上是一系列以空值终止的字符串一个接一个
在记忆中。它可以手动构建如下:
char *构造(无效)
{
char const str1 [] =" ; 172.16.0.240" ;;
char const str2 [] =" 00";
char const str3 [] =" 00";
char const str4 [] =" 00";
char static buf [sizeof str1 + sizeof str2 + sizeof str3 + sizeof str4];
char * p = buf;
char const * source;
for(source = str1; * p ++ = * source ++;);
for(source = str2; * p ++ = * source ++;);
for(source = str3; * p ++ = * source ++;);
for(source = str4; * p ++ = * source ++;);
返回buf;
}
This is effectively a series of null-terminated strings one after another
in memory. It could be constructed manually as follows:
char *Construct(void)
{
char const str1[] = "172.16.0.240";
char const str2[] = "00";
char const str3[] = "00";
char const str4[] = "00";
char static buf[sizeof str1 + sizeof str2 + sizeof str3 + sizeof str4];
char *p = buf;
char const *source;
for(source = str1; *p++ = *source++; );
for(source = str2; *p++ = *source++; );
for(source = str3; *p++ = *source++; );
for(source = str4; *p++ = *source++; );
return buf;
}
为什么我要计算超过16个元素?
Why do I count more than 16 elements?
" sizeof"会告诉你,你有22个元素。
" strlen"将报告字符串长度为12.
这与'null终止符'有关。
-
Frederick Gotham
"sizeof" will tell you that you have 22 elements.
"strlen" will report a string length of 12.
It''s to do with "null terminator".
--
Frederick Gotham
2006-07-25,Jonathan Shan< jo ***** @ winlab.rutgers.eduwrote:
On 2006-07-25, Jonathan Shan <jo*****@winlab.rutgers.eduwrote:
大家好,
这个字符串是什么?
{name =" 172.16.0.240\000\000\000"}
Hello everyone,
What is this string?
{name = "172.16.0.240\000\000\000"}
看起来与172.16.0.240相同结束后有一堆'0'和0s
。
Looks to be the same as "172.16.0.240" with a bunch of ''0''s and 0s
after the end.
名字是用这个宣布的:
字符名称[16];
为什么我要计算超过16个元素?
name was declared using this:
char name[16];
Why do I count more than 16 elements?
因为编写代码的人当时正在滴酸。
你在哪里找到这个?
-
Andrew Poelstra< website down>
我的服务器已关闭;你不能邮寄给我,也不能随便发帖。
Because the person who wrote the code was dropping acid at the time.
Where did you find this?
--
Andrew Poelstra <website down>
My server is down; you can''t mail
me, nor can I post convieniently.
Andrew Poelstra写道:
Andrew Poelstra wrote:
因为编写代码的人当时正在减酸。
你在哪里找到这个?
Because the person who wrote the code was dropping acid at the time.
Where did you find this?
给定一个名为new.txt的文件。文件里面是:
172.16.0.240:这里有很多文字。更多更多更多......
以下是展示问题的代码:
FILE *输入;
input = fopen(" new.txt"," r");
if(input == NULL)
{
printf(打开文件中出错\ n);
退出(1);
}
char inputname [16];
fscanf(输入,"%[^:]",inputname);
我要做的是准确到达" 172.16.0.240"进入
输入名称数组。
Jonathan Shan
Given a file called "new.txt". Inside the file is this:
172.16.0.240: alot of text here. more more more more more.....
Here is the code that exhibits the problem:
FILE *input;
input = fopen("new.txt", "r");
if (input == NULL)
{
printf("error in opening file \n");
exit(1);
}
char inputname[16];
fscanf(input, "%[^:]", inputname);
What I am trying to do is get exactly "172.16.0.240" to be inside the
inputname array.
Jonathan Shan
这篇关于神秘字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!