昨天我发现了一些结构初始化代码,它让我循环.这是一个例子:
typedef struct { int first; int second; } TEST_STRUCT;
void testFunc() {
TEST_STRUCT test = {
second: 2,
first: 1
};
printf("test.first=%d test.second=%d\n", test.first, test.second);
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是(对我来说),这是输出:
-> testFunc
test.first=1 test.second=2
Run Code Online (Sandbox Code Playgroud)
如您所见,struct正确初始化.我不知道标签语句可以这样使用.我已经看到了其他几种进行结构初始化的方法,但我没有在任何在线C FAQ上找到任何这种结构初始化的例子.有人知道这是如何/为什么有效?
你如何初始化以下结构?
type Sender struct {
BankCode string
Name string
Contact struct {
Name string
Phone string
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
s := &Sender{
BankCode: "BC",
Name: "NAME",
Contact {
Name: "NAME",
Phone: "PHONE",
},
}
Run Code Online (Sandbox Code Playgroud)
没工作:
mixture of field:value and value initializers
undefined: Contact
Run Code Online (Sandbox Code Playgroud)
我试过了:
s := &Sender{
BankCode: "BC",
Name: "NAME",
Contact: Contact {
Name: "NAME",
Phone: "PHONE",
},
}
Run Code Online (Sandbox Code Playgroud)
没工作:
undefined: Contact
Run Code Online (Sandbox Code Playgroud) 我有一个结构定义为:
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
Run Code Online (Sandbox Code Playgroud)
以及指向这些结构的指针数组:
struct stasher_file *files[TOTAL_STORAGE_SIZE];
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我正在指向结构并设置其成员,并将其添加到数组中:
...
struct stasher_file *newFile;
strncpy(newFile->name, name, 32);
newFile->size = size;
newFile->start = first_free;
newFile->popularity = 0;
files[num_files] = newFile;
...
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误:取消引用指向不完整类型的指针
每当我试图访问里面的成员newFile.我究竟做错了什么?
typedef struct {
int hour;
int min;
int sec;
} counter_t;
Run Code Online (Sandbox Code Playgroud)
在代码中,我想初始化此结构的实例而不显式初始化每个成员变量.也就是说,我想做的事情如下:
counter_t counter;
counter = {10,30,47}; //doesn't work
Run Code Online (Sandbox Code Playgroud)
10:30:47
而不是
counter.hour = 10;
counter.min = 30;
counter.sec = 47;
Run Code Online (Sandbox Code Playgroud)
不记得这个的语法,并没有立即找到一种方法从谷歌搜索.
谢谢!
正如标题所说,我有这个代码:
typedef struct Book{
int id;
char title[256];
char summary[2048];
int numberOfAuthors;
struct Author *authors;
};
typedef struct Author{
char firstName[56];
char lastName[56];
};
typedef struct Books{
struct Book *arr;
int numberOfBooks;
};
Run Code Online (Sandbox Code Playgroud)
我从gcc得到这些错误:
bookstore.c:8:2: error: unknown type name ‘Author’
bookstore.c:9:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:15:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:21:2: error: unknown type name ‘Book’
bookstore.c:23:1: warning: useless storage class specifier in empty declaration [enabled …Run Code Online (Sandbox Code Playgroud) 我对结构和类之间的区别感到非常困惑,因为我似乎看到它们用于几乎相同的东西.我搜索了差异,我看到的唯一答案是默认情况下结构体有公共成员,默认情况下类有私有成员.但是,我的讲师刚刚告诉我结构不能包含成员函数.但我在互联网上看到很多线程,其中人们在结构中包含成员函数,并且具体说这样做是可以的.
我的讲师似乎坚持认为结构定义不具备功能,所以发生了什么?我唯一能想到的是,编译器可能会将结构中的函数更改为其他内容,以便它们在技术上不包含函数......这些矛盾是否有明确的答案?
我在Cortex-M4微控制器上有一些代码,并希望使用二进制协议与PC通信.目前,我正在使用具有GCC特定packed属性的压缩结构.
这是一个粗略的轮廓:
struct Sensor1Telemetry {
int16_t temperature;
uint32_t timestamp;
uint16_t voltageMv;
// etc...
} __attribute__((__packed__));
struct TelemetryPacket {
Sensor1Telemetry tele1;
Sensor2Telemetry tele2;
// etc...
} __attribute__((__packed__));
Run Code Online (Sandbox Code Playgroud)
我的问题是:
TelemetryPacket对MCU和客户端应用程序上的结构使用完全相同的定义,上述代码是否可以跨多个平台移植?(我对x86和x86_64很感兴趣,需要它在Windows,Linux和OS X上运行.)编辑:
我创建了两个TheKey类型为k1 = {17,1375984}和k2 = {17,1593144}的结构.显而易见,第二个字段中的指针是不同的.但两者都得到相同的哈希码= 346948941.预计会看到不同的哈希码.请参阅下面的代码.
struct TheKey
{
public int id;
public string Name;
public TheKey(int id, string name)
{
this.id = id;
Name = name;
}
}
static void Main() {
// assign two different strings to avoid interning
var k1 = new TheKey(17, "abc");
var k2 = new TheKey(17, new string(new[] { 'a', 'b', 'c' }));
Dump(k1); // prints the layout of a structure
Dump(k2);
Console.WriteLine("hash1={0}", k1.GetHashCode());
Console.WriteLine("hash2={0}", k2.GetHashCode());
}
unsafe static void Dump<T>(T s) …Run Code Online (Sandbox Code Playgroud) 我已经定义了一个结构,它有一个构造函数:
struct MyStruct
{
MyStruct(const int value)
: value(value)
{
}
int value;
};
Run Code Online (Sandbox Code Playgroud)
和以下对象:
int main()
{
MyStruct a (true);
MyStruct b {true};
}
Run Code Online (Sandbox Code Playgroud)
但是我没有收到任何编译错误,无论是MVS2015还是Xcode 7.3.1.
bool数据,但一段时间后,代码改变,bool变得int和介绍了几个错误.)我已经在代码中制作了两次相同的bug,如下所示:
void Foo(Guid appId, Guid accountId, Guid paymentId, Guid whateverId)
{
...
}
Guid appId = ....;
Guid accountId = ...;
Guid paymentId = ...;
Guid whateverId =....;
//BUG - parameters are swapped - but compiler compiles it
Foo(appId, paymentId, accountId, whateverId);
Run Code Online (Sandbox Code Playgroud)
好的,我想防止这些错误,所以我创建了强类型的GUID:
[ImmutableObject(true)]
public struct AppId
{
private readonly Guid _value;
public AppId(string value)
{
var val = Guid.Parse(value);
CheckValue(val);
_value = val;
}
public AppId(Guid value)
{
CheckValue(value);
_value = value;
}
private static void CheckValue(Guid value)
{ …Run Code Online (Sandbox Code Playgroud)