我正在创建堆栈的链表实现。在Node结构中,我有数据成员data和next。我的问题是,为什么数据成员next需要声明为 likestruct Node * next而不仅仅是 like Node *next?
template <class T>
class Stack{
private:
struct Node{
T data;
struct Node *next; //This is what the question is in reference to
};
Node *front;
Node *end;
public:
};
Run Code Online (Sandbox Code Playgroud) 我认为通常 typedef 可以这样使用:
typedef int INT
Run Code Online (Sandbox Code Playgroud)
第一个单词是 C/C++ 中的某个关键字,第二个单词是第一个单词的别名。
但最近我注意到这个说法
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS)wglGetProcAddress("glGenBuffers");
GLuint buffer;
glGenBuffers(1, &buffer);
Run Code Online (Sandbox Code Playgroud)
看起来typedef后面有三个单词,那么编译器如何解析这个语句呢?
谢谢!
我很难理解当我在结构中声明数组时会发生什么。谈论普通数组(int、float、char...类型)我知道数组的名称本身就是指向数组第一个元素的指针。但是,假设我有一个这样的结构:
struct student
{
char name[12];
int age;
}
Run Code Online (Sandbox Code Playgroud)
在main函数中假设我声明:struct student s1;
嗯,我不明白这个变量是如何在内存中分配的。年龄有 4 个字节,字符串有 10 个字节,但是 name 指针s1.name(它是指向字符串第一个元素的指针,对吗?)呢,它是否也在新结构中分配?
我正在尝试创建一个函数指针数组,以便我可以使用指针打印函数的输出。(来自《Effective C》一书的练习。)
#include <stdio.h>
#include <stdlib.h>
int a(void) { return 1; }
int b(void) { return 2; }
int c(void) { return 3; }
int main(void)
{
int(*func_arr)[3] = {&a, &b, &c};
printf("%d\n", (*func_arr)[0]);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我编译它时,我收到警告
starting.c:10:26: warning: incompatible pointer types initializing 'int (*)[3]' with an expression of type
'int (*)(void)' [-Wincompatible-pointer-types]
int(*func_arr)[3] = {&a, &b, &c};
^~
starting.c:10:30: warning: excess elements in scalar initializer
int(*func_arr)[3] = {&a, &b, &c};
^~
Run Code Online (Sandbox Code Playgroud)
而且,当我运行该程序时,我得到的输出为 -443987883,而它应该只是 1。
有谁知道这个问题的解决方案?谢谢你!
在 C 语言中,结构化类型的名称S是 ist struct S。
在 C++ 中,也可以用作struct S类型名,而不是像S平常那样使用 for
struct S {};
struct S s1; // also ok in C++
S s2; // normal way in C++
Run Code Online (Sandbox Code Playgroud)
因此,假设在 C++ 中使用struct SorS作为类型名是一个品味问题;-)
但下面的例子中有一些地方struct S是不允许的:
struct S1 {
S1(int x = 0) : x{x} {}
int x{};
};
typedef S1 S2;
template<typename T>
auto foo(T a) {
// T::_; // T is deduced to `S` in all …Run Code Online (Sandbox Code Playgroud) 我不知道这个“功能”叫什么,所以我也无法用谷歌搜索它,如果标题没有意义,我很抱歉。我最近查看了suckless dwm的源代码并看到了这段代码:(来自dwm.c)
static int (*xerrorxlib)(Display *, XErrorEvent *);
Run Code Online (Sandbox Code Playgroud)
还有这个:
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[KeyRelease] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
Run Code Online (Sandbox Code Playgroud)
这是什么void (*handler[LASTEvent]) (XEvent *)意思 ?它叫什么以及为什么用它?
我想创建一个包含其他两个相同类型const std::vector的所有元素。const std::vector由于向量是,我无法使用连接两个 std::vectors中提到的方法const将其与两个逐步连接。const std::vector
#include <iostream>
#include <vector>
int main()
{
const std::vector<int> int_a{0,1};
const std::vector<int> int_b{2,3};
const std::vector<int> all_ints;
for (int i: all_ints)
std::cout << i << ' ';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于上面的示例,我想以all_ints输出为 的方式定义0 1 2 3。
怎么可能呢?
我正在遵循教程并找到了这段代码:
data A = B | C deriving(Eq)
class K a where
f :: a -> Bool
instance K A where
f x = x == C
f _ = False
call = f B
Run Code Online (Sandbox Code Playgroud)
为什么我需要f _ = False?没有它我得到相同的结果。
我正在做哈佛 CS50,在 C 的第二节课上,老师说 %s 会自动连续打印每个额外的参数。
\n好吧,那很酷。但是如果我想多次打印第一个字符串怎么办?
\n#include <stdio.h>\n\nint main(void) {\n char firstname[5] = "Bruce";\n char lastname[5] = "Wayne";\n\n printf("Hi, %s!\\nI am your virtual butler. I will call you Master %s from now on.\\nWelcome to %s Manor!", firstname, lastname);\n}\nRun Code Online (Sandbox Code Playgroud)\n但是,它输出:
\nHi, BruceWayne!\nI am your virtual butler. I will call you Master Wayne from now on.\nWelcome to q\xef\xbf\xbd\xef\xbf\xbdU Manor!\nRun Code Online (Sandbox Code Playgroud)\n代替:
\nHi, Bruce!\nI am your virtual butler. I will call you Master Wayne from now on.\nWelcome …Run Code Online (Sandbox Code Playgroud) 根据这int foo()()()也是一个有效的声明符(显然不是),我在这里错过了什么?
ps:有一个cdecl 站点将 c 声明解密为普通单词,并将上面的 decl 翻译为“将 foo 声明为函数返回函数返回函数返回 int”