访问结构中的数组元素 [英] Accessing array elements within a structure
问题描述
嗨大家好,
a专家的简单问题,我想。
显然我在尝试访问时遇到了什么问题元素
在结构中声明的数组:
#include< stdio.h>
#include< stddef .h>
main(){
const int SIZE = 2;
struct测试{
长型;
size_t个文件[SIZE];
};
< br $>
struct test example;
example.type = 100;
example.files [0] = 2;
printf(" type%d - 文件大小%lld:\ n",example.type,
example.files [0]);
}
预期输出是
类型100 - 文件大小2:
但我得到:
类型100 - 文件大小12871933952:
(猜我正在打印内存地址或其他东西)。
我做错了什么?
干杯
Bernd
Hi folks,
a simple question for the experts, I guess.
Obviously I am doing something wrong when trying to access an element
of an array declared within a structure:
#include <stdio.h>
#include <stddef.h>
main() {
const int SIZE = 2 ;
struct test {
long type ;
size_t files[SIZE] ;
} ;
struct test example ;
example.type = 100 ;
example.files[0] = 2 ;
printf("type %d - file size %lld:\n", example.type,
example.files[0] ) ;
}
The "intended" output is
type 100 - file size 2:
but I get:
type 100 - file size 12871933952:
(guess I am printing a memory address or something).
What am I doing wrong?
Cheers
Bernd
推荐答案
" ;贝恩德" < be **** @ gmx.netwrote in message
news:18 ************************** ******** @ a1g2000h sb.googlegroups.com ...
"bernd" <be****@gmx.netwrote in message
news:18**********************************@a1g2000h sb.googlegroups.com...
嗨伙计们,
a简单对于专家的问题,我想。
显然我在尝试访问元素时做错了什么
在结构中声明的数组:
#include< stdio.h>
#include< stddef.h>
main(){
const int SIZE = 2;
struct test {
long type;
size_t个文件[SIZE];
};
struct test example;
example.type = 100;
example.files [0] = 2;
printf(" type%d - 文件大小% lld:\ n",example.type,
example.files [0]);
}
预期输出是
类型100 - 文件大小2:
但我得到:
类型100 - 文件大小12871933952:
(猜我正在打印内存地址或其他东西)。
我做错了什么?
Hi folks,
a simple question for the experts, I guess.
Obviously I am doing something wrong when trying to access an element
of an array declared within a structure:
#include <stdio.h>
#include <stddef.h>
main() {
const int SIZE = 2 ;
struct test {
long type ;
size_t files[SIZE] ;
} ;
struct test example ;
example.type = 100 ;
example.files[0] = 2 ;
printf("type %d - file size %lld:\n", example.type,
example.files[0] ) ;
}
The "intended" output is
type 100 - file size 2:
but I get:
type 100 - file size 12871933952:
(guess I am printing a memory address or something).
What am I doing wrong?
你使用错误的''printf()''格式说明符
printf(" type% d - 文件大小%lu:\ n",example.type,
(unsigned long)example.files [0]);
-Mike
You''re using the wrong ''printf()'' format specifier
printf("type %d - file size %lu:\n", example.type,
(unsigned long)example.files[0] ) ;
-Mike
bernd写道:
bernd wrote:
嗨伙计们,
a简单对于专家的问题,我想。
显然我在尝试访问元素时做错了什么
在结构中声明的数组:
#include< stdio.h>
#include< stddef.h>
main(){
const int SIZE = 2;
struct test {
long type;
size_t个文件[SIZE];
};
struct test example;
example.type = 100;
example.files [0] = 2;
printf(" type%d - file size%lld:\ n",example.type,
example.files [0]);
}
预期输出是
类型100 - 文件大小2:
但我得到:
类型100 - 文件大小12871933952:
(猜我正在打印内存地址或其他东西)。
我做错了什么?
Hi folks,
a simple question for the experts, I guess.
Obviously I am doing something wrong when trying to access an element
of an array declared within a structure:
#include <stdio.h>
#include <stddef.h>
main() {
const int SIZE = 2 ;
struct test {
long type ;
size_t files[SIZE] ;
} ;
struct test example ;
example.type = 100 ;
example.files[0] = 2 ;
printf("type %d - file size %lld:\n", example.type,
example.files[0] ) ;
}
The "intended" output is
type 100 - file size 2:
but I get:
type 100 - file size 12871933952:
(guess I am printing a memory address or something).
What am I doing wrong?
使用"%lld"转换说明符与相应的
参数不是long long int。
由于您似乎正在使用C99实现,所以
"%zd"说明者应该工作。或者你可以使用任何
转换达到你的想象,明确地将
`size_t''值转换为适当的类型,例如
printf("%d",(int)example.files [0]);
-
Er ********* @sun.com
Using the "%lld" conversion specifier with a corresponding
argument that is not a `long long int''.
Since you appear to be using a C99 implementation, the
"%zd" specifier ought to work. Or you could use whatever
conversion strikes your fancy, explicitly converting the
`size_t'' value to the appropriate type, e.g.
printf ("%d", (int)example.files[0]);
--
Er*********@sun.com
开2008年6月16日星期一12:44:59 -0400,Eric Sosman写道:
On Mon, 16 Jun 2008 12:44:59 -0400, Eric Sosman wrote:
bernd写道:
bernd wrote:
>嗨大家好,
对于专家来说这是个简单的问题。
当我试图访问一个元素时显然我做错了什么。在结构中声明的数组:
#include< stdio.h>
#include< stddef.h>
main(){
const int SIZE = 2;
结构测试{
长类型;
size_t文件[SIZE];
>};
结构测试示例;
example.ty pe = 100;
example.files [0] = 2;
printf(" type%d - 文件大小%lld:\ n",example.type,> example.files [0]);
}
[...]
>Hi folks,
a simple question for the experts, I guess.
Obviously I am doing something wrong when trying to access an element
of an array declared within a structure:
#include <stdio.h>
#include <stddef.h>
main() {
const int SIZE = 2 ;
struct test {
long type ;
size_t files[SIZE] ;
} ;
struct test example ;
example.type = 100 ;
example.files[0] = 2 ;
printf("type %d - file size %lld:\n", example.type,
example.files[0] ) ;
}
[...]
因为您似乎正在使用C99实现,[...]
Since you appear to be using a C99 implementation, [...]
我很好奇:是什么让你认为这是C99实现?
隐式int特定于C90。使用const限定对象作为
常量表达式特定于C ++。 %lld格式说明符是特定于C99的
,但没有迹象表明它实际上支持
实现。
I''m curious: what makes you think this is a C99 implementation? The
implicit int is specific to C90. The use of a const-qualified object as a
constant expression is specific to C++. The %lld format specifier is
specific to C99, but there''s no indication that it''s actually supported by
the implementation.
"%zd"说明者应该工作。
the "%zd" specifier ought to work.
在C99中,%zd可以用于与size_t对应的签名类型,但是
没有标准的查找方式这个类型是什么。 %zu可用于
C99 for size_t。
我同意你和Mike Wahler关于使用演员的建议,但我是/>
不推荐%zd或%zu作为替代解决方案,而不是
更多表明它实际上会有效。
In C99, %zd can be used for the signed type corresponding to size_t, but
there''s no standard way to find out what this type is. %zu can be used in
C99 for size_t.
I agree with your and Mike Wahler''s suggestions to use a cast, but I
wouldn''t recommend either %zd or %zu as an alternative solution without
some more indication that it will actually work.
>
这篇关于访问结构中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!