我知道在C中我们不能从函数返回一个数组,而是一个指向数组的指针.但我想知道什么是特殊的东西structs使它们可以通过函数返回,即使它们可能包含数组.
为什么struct包装使以下程序有效?
#include <stdio.h>
struct data {
char buf[256];
};
struct data Foo(const char *buf);
int main(void)
{
struct data obj;
obj = Foo("This is a sentence.");
printf("%s\n", obj.buf);
return 0;
}
struct data Foo(const char *buf)
{
struct data X;
strcpy(X.buf, buf);
return X;
}
Run Code Online (Sandbox Code Playgroud) 我的理解是不应该这样做,但我相信我已经看过这样的例子(注意代码在语法上不一定正确,但想法就在那里)
typedef struct{
int a,b;
}mystruct;
Run Code Online (Sandbox Code Playgroud)
然后这是一个功能
mystruct func(int c, int d){
mystruct retval;
retval.a = c;
retval.b = d;
return retval;
}
Run Code Online (Sandbox Code Playgroud)
我明白我们应该总是返回一个指向malloc'ed结构的指针,如果我们想做这样的事情,但我很肯定我已经看到了做类似这样的事情的例子.它是否正确?就个人而言,我总是要么返回一个指向malloc'ed结构的指针,要么只是通过引用函数进行传递并修改那里的值.(因为我的理解是,一旦函数的范围结束,可以覆盖用于分配结构的任何堆栈).
让我们在问题中添加第二部分:这是否因编译器而异?如果是这样,那么桌面编译器的最新版本的行为是什么:gcc,g ++和Visual Studio?
关于此事的想法?
我有一个带*int64字段的结构类型.
type SomeType struct {
SomeField *int64
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中的某个时刻,我想声明一个这样的文字(比如,当我知道所述值应为0,或者指向0时,你知道我的意思)
instance := SomeType{
SomeField: &0,
}
Run Code Online (Sandbox Code Playgroud)
...除了这不起作用
./main.go:xx: cannot use &0 (type *int) as type *int64 in field value
Run Code Online (Sandbox Code Playgroud)
所以我试试这个
instance := SomeType{
SomeField: &int64(0),
}
Run Code Online (Sandbox Code Playgroud)
......但这也行不通
./main.go:xx: cannot take the address of int64(0)
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我能想出的唯一解决方案是使用占位符变量
var placeholder int64
placeholder = 0
instance := SomeType{
SomeField: &placeholder,
}
Run Code Online (Sandbox Code Playgroud)
注意:当它是*int而不是a时,编辑:不,不.为此表示歉意.&0语法工作正常*int64.
编辑:
显然,我的问题含糊不清.我正在寻找一种方法来说明一个*int64.这可以在构造函数中使用,或者用于表示文字结构值,或者甚至作为其他函数的参数.但帮助函数或使用不同的类型不是我正在寻找的解决方案.
我有一个C#背景.我非常喜欢像C这样的低级语言.
在C#中,struct默认情况下由编译器布局内存.编译器可以隐式地重新排序数据字段或填充字段之间的附加位.因此,我必须指定一些特殊属性来覆盖此行为以获得精确布局.
AFAIK,C struct默认情况下不重新排序或对齐a的内存布局.但是,我听说有一个很难找到的例外.
什么是C的内存布局行为?什么应该重新订购/对齐而不是?
考虑一个返回两个值的函数.我们可以写:
// Using out:
string MyFunction(string input, out int count)
// Using Tuple class:
Tuple<string, int> MyFunction(string input)
// Using struct:
MyStruct MyFunction(string input)
Run Code Online (Sandbox Code Playgroud)
哪一个是最佳实践,为什么?
这个错误意味着什么?
request for member '*******' in something not a structure or union
Run Code Online (Sandbox Code Playgroud)
在我学习C的时候,我已经好几次遇到过它,但我还没有弄清楚它意味着什么.
通常我们可以为C++结构定义一个变量,如
struct foo {
int bar;
};
Run Code Online (Sandbox Code Playgroud)
我们还可以为结构定义函数吗?我们如何使用这些功能?
我在C中发现了一种奇特的行为.请考虑以下代码:
struct s {
int a;
};
struct z {
int a;
struct s b[];
};
int main(void) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好.然后z像这样改变struct成员的顺序
struct z {
struct s b[];
int a;
};
Run Code Online (Sandbox Code Playgroud)
突然间,我们得到了编译错误field has incomplete type 'struct s []'.
这是为什么?
如何在C#中将结构转换为字节数组?
我已经定义了这样的结构:
public struct CIFSPacket
{
public uint protocolIdentifier; //The value must be "0xFF+'SMB'".
public byte command;
public byte errorClass;
public byte reserved;
public ushort error;
public byte flags;
//Here there are 14 bytes of data which is used differently among different dialects.
//I do want the flags2. However, so I'll try parsing them.
public ushort flags2;
public ushort treeId;
public ushort processId;
public ushort userId;
public ushort multiplexId;
//Trans request
public byte wordCount;//Count of parameter words defining the data …Run Code Online (Sandbox Code Playgroud) 正如标题所说:我是否需要覆盖==运营商?怎么样的.Equals()方法?我缺少什么?