在查看Python 3.3语法规范时,我最近发现了一些有趣的东西:
funcdef: 'def' NAME parameters ['->' test] ':' suite
Run Code Online (Sandbox Code Playgroud)
Python 2中没有可选的"箭头"块,我在Python 3中找不到任何有关其含义的信息.事实证明这是正确的Python并且它被解释器接受:
def f(x) -> 123:
return x
Run Code Online (Sandbox Code Playgroud)
我认为这可能是某种先决条件语法,但是:
x在这里测试,它仍未定义,2 < 1),它都不会影响功能行为.任何习惯这种语法的人都可以解释一下吗?
这个C语法有什么用- 使用'K&R'样式函数声明?
int func (p, p2)
void* p;
int p2;
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我能够在Visual Studios 2010beta中写这个
// yes, the arguments are flipped
void f()
{
void* v = 0;
func(5, v);
}
Run Code Online (Sandbox Code Playgroud)
我不明白.这种语法有什么意义?我可以写:
int func (p, p2)
int p2;
{
return 0;
}
// and write
int func (p, p2)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它似乎唯一指定的是它使用了多少参数和返回类型.我猜没有类型的参数有点酷,但为什么允许它和int paranName函数声明后?有点奇怪.
这还是标准的C吗?
c function kernighan-and-ritchie function-declaration function-definition
我试图在一个函数中写3-4 where语句但我得到错误并且无法做到,我试图做类似的事情:
foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= samplefunct1 x
foo2= samplefunct2 x
foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
我知道代码有点无用,但我只是写了这个来举例说明我的意思.
有没有人可以帮助我?提前致谢.
syntax haskell where-clause guard-clause function-definition
关于这个错误有很多问题。但它们只与一个变量有关。
测试.h
namespace World
{
enum Objects
{
TERRAIN = 1,
BOX = 2,
SPHERE = 4,
CAPSULE = 8
};
void WorldObjects2(unsigned int mask)
{
.......
}
}
void test();
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include "test.h"
void test()
{
.......
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include "test.h"
int main()
{
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在 Visual stduio 2013 上运行这些代码时,它会引发错误。它说error LNK2005: "void __cdecl World::WorldObjects2(unsigned int)" (?WorldObjects2@World@@YAXI@Z) already defined in main.obj。我该如何纠正这个错误?
c++ namespaces compiler-errors visual-studio function-definition
我需要有关以下计数排序实现的帮助。是不是因为x的值太大了?我遇到分段错误。gdb 是这样说的:
Program received signal SIGSEGV, Segmentation fault.
___chkstk_ms () at /usr/src/debug/gcc-5.4.0- 1/libgcc/config/i386/cygwin.S:146
146 /usr/src/debug/gcc-5.4.0-1/libgcc/config/i386/cygwin.S: No such file or directory.
Run Code Online (Sandbox Code Playgroud)
这是代码片段,
void radix_sort::sort_array(int array[], int n)
{
int arrayB[n];
auto k = *std::max_element(&array[0], &array[n - 1]);
auto m = *std::min_element(&array[0], &array[n - 1]);
long int x = k - m + 1;
int arrayC[x];
for (auto i = 0; i < n; i++)
arrayC[array[i] - m]++;
for (long int i = 1; i < x; i++)
arrayC[i] = arrayC[i] + …Run Code Online (Sandbox Code Playgroud) 我们stdio.h在 C 程序中包含头文件以使用内置库函数。我曾经认为这些头文件包含我们可能在程序中使用的内置函数的函数定义。但很快就发现并非如此。
当我们打开这些头文件(例如 stdio.h)时,它只有函数原型,我在那里看不到任何函数定义。我看到这样的事情:
00133 int _EXFUN(printf, (const char *, ...));
00134 int _EXFUN(scanf, (const char *, ...));
00135 int _EXFUN(sscanf, (const char *, const char *, ...));
00136 int _EXFUN(vfprintf, (FILE *, const char *, __VALIST));
00137 int _EXFUN(vprintf, (const char *, __VALIST));
00138 int _EXFUN(vsprintf, (char *, const char *, __VALIST));
00139 int _EXFUN(vsnprintf, (char *, size_t, const char *, __VALIST));
00140 int _EXFUN(fgetc, (FILE *));
00141 char * _EXFUN(fgets, (char *, int, FILE *)); …Run Code Online (Sandbox Code Playgroud) 假设我想要最佳性能,我怎么知道在定义Cython函数时是使用def,cdef还是cpdef?
我找到了以下python函数定义:
def reverseString(self, s: 'List[str]') -> 'None':
Run Code Online (Sandbox Code Playgroud)
我不太明白'List[str]'和-> 'None'。
我发现箭头是一个函数注释,但我找不到任何对 List[str] 有用且易于理解的东西。
它只是一个注释吗?还是强制参数s的类型必须是字符串数组?
我知道你可以像这样声明一个没有任何参数的函数:
void test()
{
cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但我也见过
void test(void)
{
cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
和
void test(void*)
{
cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是: usingvoid和void*here 有什么区别?
与 C 相比,我更喜欢 C++,但这个简单的代码示例让我大吃一惊:
int foo() {
return 3;
}
int main() {
int x;
foo(&x);
return x;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/4ov9nTzjM
没有警告,没有链接问题,但此代码仍然无效。
当一些初学者用具有这个奇怪问题的代码提出问题时,我偶然发现了这一点。有人在其他网站(非英语网站)上提出了问题,我想向他提供更好的解释为什么它可以编译(并了解一些有关 C 的知识)。如果有人需要的话,他的原始代码。
我怀疑这个sis在某种程度上与隐式函数声明有关,但我不完全确定。为什么编译器不会抱怨foo使用参数调用?
这是一个程序集:
foo:
push rbp
mov rbp, rsp
mov eax, 3
pop rbp
ret
main:
push rbp
mov rbp, rsp
sub rsp, 16
lea rax, [rbp-4]
mov rdi, rax
mov eax, 0
call foo
mov eax, DWORD PTR [rbp-4]
leave
ret
Run Code Online (Sandbox Code Playgroud)
如您所见,x仍未初始化。
c ×3
c++ ×3
function ×2
python ×2
python-3.x ×2
syntax ×2
annotations ×1
built-in ×1
c++11 ×1
cython ×1
definition ×1
gcc ×1
guard-clause ×1
haskell ×1
namespaces ×1
sorting ×1
void ×1
where-clause ×1