如果我传递的参数多于函数所需的参数,会发生什么情况?我预计被调用函数中的某些内容会被损坏,但在一些小型测试代码中一切正常。
例如:
void print()
{
int x=10;
printf("%d\n",x);
}
void main()
{
print(0,0,0,0,0);
}
Run Code Online (Sandbox Code Playgroud) 我从VB6应用程序调用C dll.dll具有如下函数调用签名.
void WINAPI geterrstr(char* foo);
Run Code Online (Sandbox Code Playgroud)
其中foo是必须返回的字符串.
在我的VB6应用程序中,我尝试使用以下语法调用我的dll,但它返回一个空字符串.
Declare Sub geterrstr Lib "technopnp.dll" (ByRef lpbuffer As String)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在Perl中建立一个压力系统来纠正学生的编程任务.我建立了一个check(<boolean>,<congrats-message>,<blame-message>)应该帮助我的功能.
当我调用它时
check(get_option('content-type') eq "text/html","good type", "bad type");
Run Code Online (Sandbox Code Playgroud)
一切都好.但如果我敢这么做的话
check(get_option('content-type') =~ m:text/html:i, "good type", "bad type");
Run Code Online (Sandbox Code Playgroud)
当正则表达式找不到匹配时它会中断.实际上它等同于a check("good type", "bad type").我只用$_[0],$_[1]等在检查()函数,它不喜欢我了"民主基金"作为第一个参数:我真的可以赶上与错误die unless $#_ == 2.
发生了什么 ?我一起工作
check((get_option('content-type') =~ m:text/html:i && 1), "good type", "bad type");
Run Code Online (Sandbox Code Playgroud)
但我很想知道这种奇怪情况的原因和原因.
-
sub check {
if ($_[0]) {
$okay++;
print STDERR "^_^ $_[1] ($okay)\n";
} else {
print STDERR ">_< $_[2]\n";
}
return $_[0];
}
Run Code Online (Sandbox Code Playgroud) 我知道它总是首先执行main()函数,然后函数调用将程序指向其他函数.如果有什么功能被称为*之前*的主要()函数?什么时候会被执行?
我有一个程序(我从互联网上下载),并在main()之前有函数调用.
现在我不知道它们是什么,如果只在main()中执行(以及在main中调用的函数).
这是该计划的片段:
static void set_level_indices (VideoParameters *p_Vid);
static void chroma_mc_setup (VideoParameters *p_Vid);
static void init_img (VideoParameters *p_Vid);
static void init_encoder (VideoParameters *p_Vid, InputParameters *p_Inp);
static int init_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_global_buffers (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_img (VideoParameters *p_Vid, InputParameters *p_Inp);
static void free_params (InputParameters *p_Inp);
static void encode_sequence (VideoParameters *p_Vid, InputParameters *p_Inp);
*(SOME FUNCTION DECLARATIONS OMITTED)*
int main(int argc, char **argv)
{
init_time();
#if MEMORY_DEBUG …Run Code Online (Sandbox Code Playgroud) 看看这个简单的代码:
class A
{};
A f(A a)
{
return a;
}
int main(void)
{
A a;
A b = f(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它创建一个局部变量a,调用一个函数f()并将其返回值赋给另一个变量b.但是我想知道函数调用期间会发生什么.
有人可以一步一步地描述我是什么,在这个过程中创建了什么对象(临时的或其他的),什么构造函数,析构函数和赋值/移动运算符被调用,什么时候?
我有这个代码片段,我在我的程序(C++和OpenCV)中到处使用.它用于计时某些操作:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
t = 1000*((double)getTickCount() - t)/getTickFrequency();
cout << "Time for LotOfComputing =" << t << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
Run Code Online (Sandbox Code Playgroud)
这就是OpenCV doc建议对代码的功能/部分进行计时的方式,它在使用几周后似乎对我有用.我测量的时间从大约1毫秒到700毫秒,我将它们舍入到毫秒.
问题是我在程序中计算了很多不同的操作,代码被这些片段弄得乱七八糟.
我想知道将这些代码行放入函数是否明智,例如:
double timing(double time){
timing = 1000*((double)getTickCount() - time)/getTickFrequency();
cout << "Time for LotOfComputing =" << timing << " milliseconds."<< endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
return timing;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以使用这个:
double t;
// Some code...
t = (double)getTickCount();
Object1.LotOfComputing();
timing(t);
Run Code Online (Sandbox Code Playgroud)
我只关心通过函数调用的执行时间......也许我只是在担心什么都没有!
我正在编写将采用整数数组的代码,然后可以将其转换为所需的基数,例如base-16.
出于某种原因,终端通过该程序并打印出来
"转换数="
这是我的代码:
#include <stdio.h>
#include <cs50.h>
int convertedNumber[64];
int base;
int digit = 0;
void getNumberAndBase(void) {
int size;
printf("How many numbers to be converted??\n");
size = GetInt();
int array[size];
for (int i = 0; i < size; i++) {
printf("Number to be converted?\n");
array[i] = GetInt();
}
printf("Base?\n");
do {
base = GetInt();
if (base < 2 || base > 16) {
printf("Bad base - must be between 2 and 16. Try again!\n");
}
} while (base …Run Code Online (Sandbox Code Playgroud) 当我们使用函数的名称来传递参数时,我们是否使用指向该函数的指针?
例:
int foo(int a, int b);
int main(void)
{
int a = foo(1,3); //foo() it's a pointer to the function foo()?
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图准确理解这段代码试图完成的内容.给出了函数中位数,但是我通过将一些东西传递给函数来添加main函数和typedef/prototypes来理解它的作用.但是我可以弄清楚是什么或如何传递一些东西.我知道这个功能是某种排序.我真正需要知道的是究竟是什么传递给函数?一个N索引数组?
谢谢你的任何指导!
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char pix_t;
pix_t median(pix_t window[N]);
int main() {
pix_t window[] = { 4, 3, 2, 1 };
pix_t output;
output = median(window[N]);
}
pix_t median(pix_t window[N])
{
pix_t t[N], z[N];
int ii, k, stage;
// copy locally
for (ii = 0; ii<N; ii++) z[ii] = window[ii];
for (stage = 1; stage <= N; stage++) {
k = (stage % 2 == 1) ? 0 : 1;
for (ii = k; ii<N - …Run Code Online (Sandbox Code Playgroud) 举例来说,我已经得到了功能funcA(),funcB(),funcC().我想打电话给每个功能,但每一个后,执行一系列其他功能,如func1(),func2(),func3(),func4().
我知道我可以:
funcA()
func1()
func2()
func3()
func4()
funcB()
....
Run Code Online (Sandbox Code Playgroud)
或者我可以:
def funcCombined():
func1()
func2()
func3()
func4()
funcA()
funcCombined()
funcB()
funcCombined()
...
Run Code Online (Sandbox Code Playgroud)
但有更好的方法吗?我尝试将它们放在一个列表中,例如:
funcs = [funcA(), funcB(), funcC()]
for func in funcs:
x = func
func1()
func2()
func3()
func4()
Run Code Online (Sandbox Code Playgroud)
但它似乎正在执行列表中的所有函数,然后在for循环中执行它们.
最好的方法是什么?