- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
-- 学生在这里学习 C --
我被指示创建一个“[StructureType],它是一个集合结构,其中包含一个原始(常规)数组[OtherStructureType] 指针。”此外,“[StructureType] 将填充 10 个动态分配的 [OtherStructureType] 结构。”
我很难从概念上和实践上理解这一点。这是我的尝试。有需要的地方请指正...
1.) 如果我们使用 malloc() 动态分配 OtherStructureType 的实例,它将是一个指针:
typedef struct{
int age;
} OtherStructureType;
OtherStructureType * Person = malloc(1 * sizeof(OtherStructureType))
2.) 如果 StructureType 的实例有一个数组成员,那么我们可以将此指针放入该数组中:
typedef struct{
int array[n];
} StructureType;
StructureType Collection;
Collection.array = Person;
3.) 如果 1 和 2 正确,那么我们如何获取 Person 的年龄?像这样吗?
StructureType* ptr;
ptr = Collection;
ptr->array[i].age;
Edit1:我说 Instance.array = Person;当我应该写 Collection.array = Person;Edit2:第二季度和第三季度数组之间的清晰度
最佳答案
- Create a "[StructureType] that is a collection structure which contains a primitive (regular) array of [OtherStructureType] pointers."
- Also, "the [StructureType] will be filled with 10 dynamically allocated [OtherStructureType] structures."
部分答案(问题 Material ):
typedef struct
{
int age;
} OtherStructureType;
OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));
这部分代码没问题(只要赋值在函数内部)。出于风格原因,我将 Person
更改为 person
(如果类型和变量不全部以大写字母开头,则更容易区分类型和变量)。
- If an instance of StructureType has an array member, then …
问题的这一部分本质上是完全不正确的。您有一个整数数组,而不是指向 OtherStructureType
的指针数组。目前还不清楚 n
的定义是什么。要在结构定义中使用,它必须是常量,并且常量通常以全部大写字母书写(根据需要带有数字和下划线)。一般来说,这样的名称应该长于 N
(可能是 NUM_OTHERS
),但对于本例来说就足够了。另外,我通常为结构使用标签并使用 typedef
,但我将结构保留为无标签。
enum { N = 10 };
typedef struct
{
OtherStructureType *array[N];
} StructureType;
StructureType collection;
collection.array[0] = person;
只要赋值在函数内部,这就可以工作。
3.How then would we access Person's age? …
StructureType *ptr = malloc(sizeof(*ptr));
OtherStructureType *other = malloc(sizeof(*other));
ptr->array[0] = other;
ptr->array[0]->age = 21;
这些大约是可行的最小更改(请参阅下面的 nest31.c
了解 MCVE)。
我认为你应该有这样的东西:
enum { N = 10 };
typedef struct StructureType
{
int num_ptrs;
OtherStructureType *array[N];
} StructureType;
num_ptrs
元素允许您跟踪数组元素中有多少元素持有有效指针。访问无效指针会导致疯狂或崩溃,因此您需要知道有多少是有效的(假设元素 0..num_ptrs-1
是有效的,如果 num_ptrs > 0
)。如果您在不同时间分配指向 OtherStructureType
值,则这适用。
请参阅下面的 nest53.c
了解 MCVE。
If we want to have 10 people, instead of doing each one individually can we dynamically allocate an array instance of StructureType.
我responded :
是的,你可以做到,尽管你可能会:
typedef struct
{
int num_ptrs;
int num_used;
OtherStructureType *people;
} StructureType;
然后你可以使用:
StructureType st;
st.num_ptrs = 10;
st.num_used = 0;
st.people = malloc(st.num_ptrs * sizeof(st.people[0]));
(记得检查分配是否成功)。或者:
StructureType *stp = malloc(sizeof(*stp));
stp->num_ptrs = 10;
stp->num_used = 0;
stp->people = malloc(stp->num_ptrs * sizeof(stp->people[0]));
在某些变体中,您可以初始化 st
,而不是在定义它之后进行赋值,只要您在定义变量时位于函数内部即可。例如:
StructureType st = { .num_ptrs = 10, .num_used = 0,
.people = malloc(st.num_ptrs * sizeof(st.people[0]))
};
您仍然需要检查内存分配是否成功。现在您可以使用如下符号:
st.people[0].age = 21;
而不是使用:
st.people[0]->age = 21;
请注意,这两个表达式中的第一个表达式中少了一个指针访问,因此访问(略微!)更高效。
请参阅下面的 nest73.c
了解 MCVE。
nest31.c
/* SO 5276-8854 */
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int age;
} OtherStructureType;
enum { N = 10 };
typedef struct
{
OtherStructureType *array[N];
} StructureType;
int main(void)
{
OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));
StructureType collection;
collection.array[0] = person;
collection.array[0]->age = 23;
printf("Age: %d\n", collection.array[0]->age);
StructureType *ptr = malloc(sizeof(*ptr));
OtherStructureType *other = malloc(sizeof(*other));
ptr->array[0] = other;
ptr->array[0]->age = 21;
printf("Age: %d\n", ptr->array[0]->age);
free(ptr);
free(other);
free(person);
return 0;
}
nest31
的输出Age: 23
Age: 21
nest53.c
/* SO 5276-8854 */
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int age;
} OtherStructureType;
enum { N = 10 };
typedef struct StructureType
{
int num_ptrs;
OtherStructureType *array[N];
} StructureType;
int main(void)
{
OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));
StructureType collection = { 0, { 0 } };
collection.array[0] = person;
collection.num_ptrs = 1;
collection.array[0]->age = 23;
for (int i = 0; i < collection.num_ptrs; i++)
printf("Age %d: %d\n", i, collection.array[i]->age);
StructureType *ptr = malloc(sizeof(*ptr));
OtherStructureType *other = malloc(sizeof(*other));
ptr->num_ptrs = 1;
ptr->array[0] = other;
ptr->array[0]->age = 21;
for (int i = 0; i < ptr->num_ptrs; i++)
printf("Age %d: %d\n", i, ptr->array[i]->age);
free(ptr);
free(other);
free(person);
return 0;
}
nest53
的输出Age 0: 23
Age 0: 21
nest73.c
/* SO 5276-8854 */
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int age;
} OtherStructureType;
typedef struct
{
int num_ptrs;
int num_used;
OtherStructureType *people;
} StructureType;
int main(void)
{
StructureType st;
st.num_ptrs = 10;
st.people = malloc(st.num_ptrs * sizeof(st.people[0]));
st.people[0].age = 21;
st.people[1].age = 23;
st.people[2].age = 19;
st.num_used = 3;
for (int i = 0; i < st.num_used; i++)
printf("Age %d: %d\n", i, st.people[i].age);
StructureType *stp = malloc(sizeof(*stp));
stp->num_ptrs = 10;
stp->people = malloc(stp->num_ptrs * sizeof(stp->people[0]));
stp->people[0].age = 51;
stp->people[1].age = 49;
stp->people[2].age = 37;
stp->num_used = 3;
for (int i = 0; i < stp->num_used; i++)
printf("Age %d: %d\n", i, stp->people[i].age);
free(st.people);
free(stp->people);
free(stp);
return 0;
}
nest73
的输出Age 0: 21
Age 1: 23
Age 2: 19
Age 0: 51
Age 1: 49
Age 2: 37
关于c - 访问另一个结构体数组内的结构体成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52768854/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!