LoadRunner脚本编写(4)

发表于:2015-09-21来源:uml.org.cn作者:不详点击数: 标签:loadrunner
* 自动(auto)变量 函数中的局部变量,如不专门的声明为static存储类别,都是动态地分配存储空间的。 * 静态(static)声明局部变量 有时希望函数中的局部变量

  * 自动(auto)变量

  函数中的局部变量,如不专门的声明为static存储类别,都是动态地分配存储空间的。

  * 静态(static)声明局部变量

  有时希望函数中的局部变量的值在函数调用结束后不消失而保留,这时就应该指定局部变量为“静态局部变量”,用static关键字。

  * 寄存器(register)变量

  为了提高效率,C语言允许把局部变量的值放在CPU中的寄存器中,这种变量叫“寄存器变量”,用关键字register变量。

static int c;

int prime(register int number) //判断是否为素数
{
register int flag=1;
auto int n;
for (n=2;n<number/2 && flag==1;n++) {
if (number % n==0) flag=0;
return(flag);

}
}

demo(int a) //static、auto变量的演示函数
{
auto int b=0;
int d;
static c=3;
b=b+1;
c=c+1;
lr_output_message("demo()函数中的d=%d",d);
lr_output_message("demo()函数中的static c=%d",c);
return a+b+c;

}
Action(){
int a=2,i; //变量声明

for (i=0;i<3;i++) {
lr_output_message("demo()函数部分第%d运行情况如下:",i+1);
lr_output_message("函数demo运行结果为:%d",demo(a));
lr_output_message("-------------------\n\r");
}

//判断13是否为素数,并输出提示信息
if (prime(13)==0)
lr_output_message("13不是素数!");
else
lr_output_message("13是素数!");

lr_output_message("c=%d",c); //输入变理的值


return 0;
}

  素数:指大于1的自然数,除了1和它本身不能被其它数整除的数。prime()函数部分主要用来判断传入的数是否是素数。demo()函数用来做static和auto类型的变量演示。Action()函数用于调用与输入结果。

  运行结果:

Starting iteration 1.
Starting action Action.
Action.c(31): demo()函数部分第1运行情况如下:
Action.c(22): demo()函数中的d=51446257
Action.c(23): demo()函数中的static c=4
Action.c(32): 函数demo运行结果为:7
Action.c(33): -------------------

Action.c(31): demo()函数部分第2运行情况如下:
Action.c(22): demo()函数中的d=51446257
Action.c(23): demo()函数中的static c=5
Action.c(32): 函数demo运行结果为:8
Action.c(33): -------------------

Action.c(31): demo()函数部分第3运行情况如下:
Action.c(22): demo()函数中的d=51446257
Action.c(23): demo()函数中的static c=6
Action.c(32): 函数demo运行结果为:9
Action.c(33): -------------------

Action.c(40): 13是素数!
Action.c(42): c=0
Ending action Action.
Ending iteration 1.

  指针

  指针是C语言中广泛使用的一种数据类型,指针可以使我们的程序变得非常灵活,但也让不少程序员头痛,一不小心就会使程序出错。

  指针一般指向一个函数或一个变量。在使用一个指针时,一个程序既可以直接使用这个指针所储存的内存地址,又可以使用这个地址里储存的变量或函数的值。

  有一本很厚小说,为了便于读者找到某一段内容,我们会给某一段内容起一个小标题并标注上页数。这样找起来就非常方便了。那在内存中,小标题页数就相当于内存单元的指针,具体的小说内容就是内存单元的内容。

Action(){
int score[5]={100,98,78,55}; //一维数组
int *p=score; //一维数组指针
int sixnum[2][3]={{1,2,3},{4,5,6}}; //二维数组
int (*p1)[3]; //二维数组指针
int i,j; //定义两个变量

for (i=0;i<=4;i++) {
lr_output_message("score[%d]=%d",i,score[i]); //以下标形式标识数组
lr_output_message("*(p++)=%d",*(p++)); //以指针方式输出数组
}
lr_output_message("--------------------------");

p=score;
for (i=0;i<=4;i++) {
lr_output_message("score[%d]=%d",i,score[i]); //以下标形式标识数组
lr_output_message("*(p+%d)=%d",*(p+i)); //以指针方式输出数组
}
lr_output_message("--------------------------");

p1=sixnum;
for (i=0;i<=1;i++) {
for (j=0;j<=2;j++) {
lr_output_message("sixnum[%d][%d]=%d",i,j,sixnum[i][j]); //以下标形式标识数组
lr_output_message("*(*(p1+%d)+%d)=%d",*(*(p1+i)+j)); //以指针方式输出数组
}

}

return 0;
}

原文转自:http://www.uml.org.cn/Test/201303151.asp