我刚刚接受了某些面试问题.得到这个结构相关的问题,我不明白输出结果是什么,如果有人可以解释原因.
何时在结构中使用字符指针
#include <stdio.h>
struct name {
char *array;
}variable;
int main( int argc, char * argv[] ){
variable.array="hello";
printf( "%s\n", variable.array );
}
Run Code Online (Sandbox Code Playgroud)
hello打印输出,但将结构变量更改为
struct name {
char array[10];
}variable;
Run Code Online (Sandbox Code Playgroud)
编译器抛出错误"赋值中的不兼容类型"
variable.array="hello";
Run Code Online (Sandbox Code Playgroud)
我真的很困惑,我错过了这一点.为什么它会像分配问题一样显示错误?请指正,谢谢
是否也会p = (users *)malloc(sizeof(users));为播放列表结构创建内存?另外我如何使用p引用playlist.album?
struct playlist_ {
int album;
int track_num;
struct playlist_ *next;
};
struct users_ {
int user_ID;
struct playlist_ playlist;
struct users_ *next;
};
typedef struct playlist_ playlists;
typedef struct users_ users;
users *p;
p = (users *)malloc(sizeof(users));
Run Code Online (Sandbox Code Playgroud) 我必须生成一个仅在某些条件下包含某些字段的数据结构.这通常总是转化为以下内容
struct MyStruct {
int alwaysHere;
#ifdef WHATEVER
bool mightBeHere;
#endif
char somethingElse;
#if SOME_CONSTANT > SOME_VALUE
uint8_t alywasHereButDifferentSize;
#else
uint16_t alywasHereButDifferentSize;
#endif
...
};
Run Code Online (Sandbox Code Playgroud)
从我的观点来看,这看起来很难看,而且难以理解.甚至没有谈论处理这些字段的代码,通常也在ifdef下.
我正在寻找一种优雅的方法来实现相同的结果,而不会增加任何开销,但代码更具可读性.模板专业化似乎有点过分,但在我看来它是唯一的选择.
C++ 11是否会添加任何内容来处理这种情况?
任何建议将不胜感激.
这是我第一次在结构中使用结构.编译程序时遇到此错误.错误:字段'结果'的类型不完整.
错误是指这行代码. - > struct result_t results;
有什么帮助吗?:) 谢谢.
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
struct result_t results;
} student_t;
Run Code Online (Sandbox Code Playgroud)
编辑:
我改变了我的代码:
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
result_t results;
} student_t;
Run Code Online (Sandbox Code Playgroud)
我收到了一个新的编译错误.错误:下标值既不是数组也不是指针.
触发该错误的代码行如下.printf("% - 7s%-2s%d \n",student.results [i] .module_code,student.results [i] .grade,student.results [i] .mc);
在一个函数中我使用了malloc:
void name1(struct stos* s)
{
s = malloc (4 * sizeof (int));
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.但后来我使用了realloc
void name2(struct stos* s)
{
s->size = 2*(s->size);
s = realloc (s, (s->size + 1) * sizeof (int));
}
Run Code Online (Sandbox Code Playgroud)
我在valgrind中得到无效的free/delete/realloc,realloc返回NULL.
结构声明和其余程序是:
struct stos
{
int top;
int size;
int stk[];
};
void name1(struct stos* s);
void name2(struct stos* s);
int main()
{
struct stos stosik;
struct stos* s;
s = &stosik;
name1(s);
//some operations on the array and int top here
name2(s);
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我找了很长时间可能出错的地方,阅读了很多关于指针,malloc/realloc等的文章,但没有结果.如果有人能帮助我,我将非常感激.
考虑以下程序:
#include <iostream>
struct __attribute__((__packed__)) mystruct_A
{
char a;
int b;
char c;
}x;
int main()
{
std::cout<<sizeof(x)<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
从这个我明白了以下几点:
我在32位环境中,正在使用Windows 7 OS。链接问题的第一个答案说,以上代码将在32位体系结构上生成6号大小的结构。
但是,当我使用g ++ 4.8.1对其进行编译时,它的输出为9。那么,结构打包在这里不会完全发生吗?为什么输出中要多出3个字节?sizeof char始终为1。在我的编译器中,sizeof int为4。因此,压缩结构时,以上结构的sizeof应该为1 + 4 + 1 = 6。
处理器有任何作用还是仅取决于编译器?
例如,我有以下嵌套向量:
[[[0.582198689235419 -0.34713183143727 0.4685311493624731]
[-0.38928013774079284 -0.5901700383677557 -0.37573234072157]
[0.6716356761877877 -0.19645167952721243 -0.5700686091940252]]
[[0.0027162308840597005 -0.4483592764429284 -0.4766278022217257 -0.2724018313051576]
[-0.2765881229144672 -0.8030656496255356 -0.16159395457031567 -0.27432324260043084]
[-0.6154630466545907 -0.60573539482247 0.4417814561800192 -0.5559788990464504]
[0.6194560094536031 -0.3663074359460578 -0.5704311251195602 0.7194827876969362]]]
Run Code Online (Sandbox Code Playgroud)
我有以下扁平的矢量:
(0.5366343712173423
-0.816449781850872
-0.16066485785704843
0.9816561233924161
-0.09646744313584676
-0.2619662625757997
-0.9946004265996822
-0.14096299956754854
0.579260850612288
-0.827601452607939
-0.24934665032374648
-0.42272393175707873
0.11239245249400165
-0.29878238708035043
-0.61522274672097
0.8298721730401472
0.5016214138116059
0.11633537727916243
-0.0631891708267196
-0.26569217599364303
0.20900664784109668
0.2005869506108401
-0.2658279978034501
0.3383997403318165
-0.09353513546647907)
Run Code Online (Sandbox Code Playgroud)
我希望将展平的矢量转换为嵌套矢量,该矢量遵循与上面给出的嵌套矢量相同的结构.Clojure或库中有核心功能吗?我有一些关于如何解决这个问题的想法,但是它们看起来效率都很低,而且这个操作将用于大向量.
非常感谢你提前.
我正在创建一个xml来传递给API,API返回它(数据转储):
(
"Data::Dump",
{
SiteDevices => {
"device" => {
1102 => { address => "1.2.3.4", riskfactor => "1.0", riskscore => "0.0" },
1136 => { address => "1.2.3.5", riskfactor => "1.0", riskscore => "0.0" },
20491 => { address => "1.2.3.6", riskfactor => "1.0", riskscore => "0.0" },
129644 => { address => "1.2.3.7", riskfactor => "1.0", riskscore => "0.0" },
129645 => { address => "1.2.3.8", riskfactor => "1.0", riskscore => "0.0" },
130408 => { address => …Run Code Online (Sandbox Code Playgroud) 我想知道何时应该使用指向结构中的结构的指针.
我清楚地理解为什么我们使用指向链接列表中的结构的指针,但我在另一种情况下面临一个问题.
例如:
#include <stdlib.h>
#include <string.h>
#define NICK_MAX_LENGTH 100
struct deck {
size_t nb_cards;
int *cards;
}
// Should I do :
struct player {
int id;
char nick[NICK_MAX_LENGTH];
struct deck *pl_deck;
}
struct player* my_function1() {
struct player *pl = malloc(sizeof(struct player));
pl->id = 1;
strcpy(pl->nick, "Paul")
pl->pl_deck = malloc(sizeof(struct deck));
pl->pl_deck->nb_cards = 3;
for (int i = 0; i < 3; ++i)
pl->pl_deck->cards[i] = i + 1;
return pl;
}
// .. or should I do : …Run Code Online (Sandbox Code Playgroud) 我想编写一个函数,将可变数量的变量(在下面的示例中,数组sc,矩阵A,数字T)转换为包含它们的结构。相应的结构标签应为变量本身的名称。请参见下面的示例:
sc=[1 2 1 0.5 0.01 0.03];
A=[1,2,3,4;1,2,3,4];
T=2;
Run Code Online (Sandbox Code Playgroud)
我希望我的职能做到这一点:
data.sc=sc;
data.A=A;
data.T=T;
Run Code Online (Sandbox Code Playgroud)
因此输出为:
数据=
结构与字段:
Run Code Online (Sandbox Code Playgroud)sc: [1 2 1 0.5000 0.0100 0.0300] A: [2×4 double] T: 2
用于可变数量的异构参数。