所以我有几个结构......
struct myBaseStruct
{
};
struct myDerivedStruct : public myBaseStruct
{
int a, b, c, d;
unsigned char* ident;
};
myDerivedStruct* pNewStruct;
Run Code Online (Sandbox Code Playgroud)
...我希望动态分配足够的空间,以便我可以"记忆"某些数据,包括一个以零结尾的字符串.基本结构的大小显然是'1'(我假设因为它不能为零)并且派生的大小是20,这似乎是有意义的(5 x 4).
所以,我有一个大小为29的数据缓冲区,前16个字节是整数,其余13个是字符串.
如何为pNewStruct分配足够的内存,以便字符串足够?理想情况下,我只想去:
谢谢,
我有
struct Parent
{
int child1;
int child2;
char child3;
float child 4;
anotherStruct child5;
};
typedef struct
{
unsigned char x;
int y;
char z;
float a;
int b;
char c;
etc ..
} anotherStruct;
Parent myFirstParent;
Parent mySecondParent;
///I want to do a deep copy of myFirstParent into mySecondParent.
//does the follwowing work for that purpose??
memcpy (&mySecondParent, &myFirstParent, sizeof(myFirstParent);
Run Code Online (Sandbox Code Playgroud)
我正在调查答案,但同时由于极端时间限制我发布了这个问题.提前致谢.
在下面的程序中,我尝试将结构传递给函数.但我得到错误,我不明白为什么.我在这个程序中犯了什么错误?
我gcc用来编译这个c程序.
#include <stdio.h>
struct tester {
int x;
int *ptr;
};
void function(tester t);
int main() {
tester t;
t.x = 10;
t.ptr = & t.x;
function(t);
}
void function(tester t) {
printf("%d\n%p\n",t.x,t.ptr);
}
Run Code Online (Sandbox Code Playgroud)
错误:
gcc tester.c -o tester
tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in …Run Code Online (Sandbox Code Playgroud) 如果我有一个定义如下的结构:
struct image{
unsigned int width, height;
unsigned char *data;
};
Run Code Online (Sandbox Code Playgroud)
这个类型的2个变量:
struct image image1;
struct image image2;
Run Code Online (Sandbox Code Playgroud)
我想将数据从image1传输到image2的数据(假设image1有一些数据写入,而image2有数据用malloc或calloc分配).怎么做到呢?非常感谢.
我在跟随代码行时遇到错误.我试图将向量p的元素p [0],p [1],p [2],p [3]传递给函数距离.
typedef struct {
long long x,y;
} point;
long long distance (point A,point B)
{
int d1 = A.x - B.x ;
int d2 = A.y - B.y ;
long long d = d1 * d1 + d2 *d2 ;
return d ;
}
//in main function I declared vector <point> p and took input and then,
x1 = distance (p[0],p[1]) ; // this line is causing error
x2 = distance (p[1],p[2]) ; // this …Run Code Online (Sandbox Code Playgroud) 我希望扩展一个struct(bac),其中包含来自另一个结构(BT)的许多字段.这些字段的名称adds作为字符串包含在单元格数组()中.
这就是我现在所拥有的(显然没有做到这一点,解释这篇文章):
for i=1:numel(adds)
eval(genvarname('bac.',adds{i})) = eval(strcat('BT.',adds{i}));
end
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用sprintf,这似乎对我不起作用.我相信你们其中一个人知道该怎么做,因为我觉得它应该很容易.
我在C中尝试了一些基本的数据结构.我很久以后就回到了C语言.这是我的简单struct:
typedef struct
{
int data;
LLNode *next; //Error: unknown type name 'LLNode'
}LLNode;
Run Code Online (Sandbox Code Playgroud)
但它给出了编译错误,如上所示.是因为编译时编译struct器不知道存在LLNode吗?这意味着我必须首先声明LLNode之前struct.是这样的吗?如果是的话我该怎么做呢?
我有一个"对象"结构数组OBJECT_ARRAY,我必须经常转换为对象结构的每个元素的单个数组.这可以使用arrayfun.它比单纯的裁判更乏味OBJECT_ARRAY(k).item1,但这就是The Mathworks选择这样做的方式.
在今天的这种情况下,我已经使用了那些单独的数组并计算了相应的派生值newItem,对于每个元素,我需要将其添加到原始结构数组中.所以我有一个数组newItems.
是否有一种直接的方法为每个对象进行分配,OBJECT_ARRAY以便(有效地)OBJECT_ARRAY(k).newItem = newItems(k)为每个索引k?
我使用的是2015a版.
这是我的代码.它适用于C,但它的行为类似于C++中的结构.
#include<iostream>
using namespace std;
int main()
{
union myunion
{
int B;
double PI;
};
union myunion numbers;
numbers.PI = 3.14;
numbers.B = 12;
/* now storage of PI should be over-written */
cout<<numbers.PI<<endl;
cout<<numbers.B<<endl;
/* Output is 3.14 */
/* 12 */
/* Why it is behaving like Structures ? */
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在C语言中,struct Animal;第6行的含义是什么?
在C89或C99或C11中是否合法?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Run Code Online (Sandbox Code Playgroud)
谢谢!