我们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) 我想在ZSH中动态定义一系列函数。
例如:
#!/bin/zsh
for action in status start stop restart; do
$action() {
systemctl $action $*
}
done
Run Code Online (Sandbox Code Playgroud)
然而,这会产生四个相同的函数,它们都调用最后一个参数:
$ status libvirtd
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
Authentication is required to restart 'libvirtd.service'.
...
Run Code Online (Sandbox Code Playgroud)
有没有办法像这样动态定义这些函数?
我编写了以下代码来使用二进制搜索查找字符串中的第一个大写字母:
char first_capital(const char str[], int n)
{
int begin = 0;
int end = n - 1;
int mid;
while (begin <= end)
{
mid = (begin + end) / 2;
if (mid == 0 && isupper(str[mid]))
{
return mid;
}
else if (mid > 0 && isupper(str[mid]) && islower(str[mid - 1]))
{
return mid;
}
if (islower(str[mid]))
{
begin = mid + 1;
}
else
{
end = mid - 1;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
目前我的代码在测试时没有按预期工作。如果有人能提到我哪里出错了,那会很有帮助。
注意:输入字符串将已经排序(所有小写字母都出现在大写字母之前)。 …
我正在尝试创建一个返回二叉树的镜像副本的函数。我所说的“镜像”是指一棵树,每个左节点作为其右节点,反之亦然。
The one on the left gets copied to resemble the one on the right. This is the code of the function, with the definition of the binary nodes and "insert node" function that I use:
typedef struct bNode {
int data;
struct bNode *left;
struct bNode *right;
} bNode;
// =============================================================
bNode* reverse_tree (bNode **tree) {
bNode *copy = malloc(sizeof(bNode));
copy->data = (*tree)->data;
if (!((*tree)->right) && !((*tree)->left)){
return copy;
}
copy->left = reverse_tree(&(*tree)->right);
copy->right = reverse_tree(&(*tree)->left);
return copy;
} …Run Code Online (Sandbox Code Playgroud) 我无法找出为什么我的函数仅返回用户输入而不是输入的阶乘。
#include <stdio.h>
#include <math.h>
int factorial(int x)
{
//int x;
int sum = 1;
while (x!=0){
sum = sum * x;
x--;
}
return sum;
}
int main(){
int x;
printf("Enter value of x: ");
scanf("%i",&x);
factorial(x);
printf("sum is %i", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在MATLAB中,大致有3种方法来定义函数:非注释.m文件,.p文件和编译后的代码(例如DLL,MEX)。
在某些情况下,例如在对我们控制范围之外的某个功能进行重大更改时,知道在何处定义函数可能会有所帮助,并且我们希望尝试恢复为旧版本,以期使我们的代码再次正常工作; 或尝试反向工程某些未公开的算法时。
The which function is usually very good at identifying function definitions and their locations (which works for .m, .p and MEX), but isn't very useful when it comes to shared library functions, where (at best) it points to a comment-only documentation file:
>> which _mcheck
built-in (undocumented)
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
Run Code Online (Sandbox Code Playgroud)
If so, assuming a function found within a shared library is called during the execution of my …
matlab reverse-engineering shared-libraries built-in function-definition
在C lang FAQ中,我找到了以下代码:
void f(ipp)
int **ipp;
{
static int dummy = 5;
*ipp = &dummy;
}
Run Code Online (Sandbox Code Playgroud)
我测试了使用-Wall -std=c11 -pedantic标志进行编译,并且没有警告或错误的情况下进行了编译。这怎么可能-在函数名称及其块之间声明一个变量?
编辑:
发布几小时后,经过一系列回答,我发现问题已重复存在。我不同意关闭的决定。实际上,到目前为止,重复的问答和此处给出的答案虽然大体上是正确的,但并未具体回答我的问题。
我的查询是关于在函数名称及其块之间出现的变量声明。好的,那是原始的K&R风格,但是我仍然觉得声明的位置令人震惊。阅读了描述ANSI C89的K&R第二版书后,我知道以前的样式允许使用不同的方法来声明函数参数,但是AFAIK那本书没有显示以这种方式进行的声明。也许是这样,我已经忘记了。
我认为,最好单独就这个特定问题提出一个问题,以防将来有人被它抛弃。我的问题应该代表任何可以阐明如何决定允许在此陌生位置声明参数的人。您从C语言和所有受C语言启发的语言中获得的印象是,令牌及其大括号之间没有任何联系。我的问题提请您注意一个重要的例外情况,即使该例外已存在30/40年,也应予以强调并理解其原理。
编辑2:
现在,我发现C ++语法允许在函数名称及其块之间插入标记。即便如此,整个声明行介于两者之间的想法更为严格,值得向C新手指出,因为他们可能会遇到一些怪癖。我已经检查过,《 K&R第二版》的确没有明确提及这一点。
我需要一个需要其子类重载<<and的接口>>,但我不太确定如何重载,因为这些运算符没有作为成员函数重载:
std::istream& operator>> (std::istream& in, Student& student) {
in >> student.name >> student.group;
for (int& i : student.marks) { in >> i; }
return in;
}
Run Code Online (Sandbox Code Playgroud)
也许有办法使它成为成员函数?
我试图反转一个字符串,但它只是保持不变。除了<string.h>and之外,我不使用任何模块<stdio.h>。
void rev(s){
char i, temp;
char *sf = s;
char ri = strlen((s) - 1);
char *sl = &s[ri];
for (i = 0; i < ri; i++){
if (*sf != *sl){
temp = *sf++;
s[i] = *sl--; //
s[ri--] = temp; //those two seems to be getting new characters, but it won't
}
else {
ri--;
sf++;
sl--;
}
}
printf("%s", s);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现我的版本strcat。但是,我收到以下警告,并且我的代码在运行时崩溃。我正在传递&p对main函数p中的变量进行永久更改main。
警告:
\nnote: expected \xe2\x80\x98char **\xe2\x80\x99 but argument is of type \xe2\x80\x98char (*)[10]\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n代码:
\n#include <string.h>\n#include <stdio.h>\n\nvoid mystrcat(char **p, char *q)\n{\n while (**p != '\\0')\n {\n *p++;\n }\n\n while (*q != '\\0')\n {\n **p = *q;\n (*p)++;\n q++;\n }\n *p = '\\0';\n}\n\nint main()\n{\n char p[10] = "ravi";\n char q[12] = "ra";\n\n mystrcat(&p, q);\n printf("%s", p);\n}\nRun Code Online (Sandbox Code Playgroud)\n