请老师指点,结构体变量的作用范围
本帖最后由 wow7jiao 于 2018-7-27 17:31 编辑#include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
struct Book getInput(struct Book book);
void printBook(struct Book book);
struct Book getInput(struct Book book)
{
printf("请输入书名:");
scanf("%s", book.title);
printf("请输入作者:");
scanf("%s", book.author);
printf("请输入售价:");
scanf("%f", &book.price);
printf("请输入出版日期:");
scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
printf("请输入出版社:");
scanf("%s", book.publisher);
return book;
}
void printBook(struct Book book)
{
printf("书名:%s\n", book.title);
printf("作者:%s\n", book.author);
printf("售价:%.2f\n", book.price);
printf("出版日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
printf("出版社:%s\n", book.publisher);
}
int main(void)
{
struct Book b1, b2;
printf("请录入第一本书的信息...\n");
b1 = getInput(b1);
putchar('\n');
printf("请录入第二本书的信息...\n");
b2 = getInput(b2);
printf("\n\n录入完毕,现在开始打印验证...\n\n");
printf("打印第一本书的信息...\n");
printBook(b1);
putchar('\n');
printf("打印第二本书的信息...\n");
printBook(b2);
return 0;
} wow7jiao 发表于 2018-7-27 17:30
1楼的是问题,上面是源码,下面是问题b1是结构体局部变量,在void printBook(struct Book book)里用book.ti ...
可见啊,怎么不可见,只不过是一个拷贝罢了。 呃,你到底要问啥? 1楼的是问题,上面是源码,下面是问题b1是结构体局部变量,在void printBook(struct Book book)里用book.title不可见吧 无符号整形 发表于 2018-7-27 17:29
呃,你到底要问啥?
亲,我想问结构体变量是放在堆还是在栈里,作用范围 无符号整形 发表于 2018-7-27 17:23
可见啊,怎么不可见,只不过是一个拷贝罢了。
请问老师,这个拷贝(从一个函数局部变量对另外一个函数的形参传递),是所有的数据,在栈对栈的拷贝,还是只拷贝一个地址过去? wow7jiao 发表于 2018-7-28 12:10
请问老师,这个拷贝(从一个函数局部变量对另外一个函数的形参传递),是所有的数据,在栈对栈的拷贝,还 ...
栈对栈,只传递地址(这个要看调用约定,一般的__cdcel和__stdcall是这样,但是__fastcall就是通过寄存器传参数) 看汇编语言
#include <stdio.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
void Fun1(struct Book book)
{
}
void Fun2(struct Book *book)
{
}
int main(void)
{
struct Book book = {0};
Fun1(book);
Fun2(&book);
return 0;
}
有符号名,看起来容易一点
int main(void)
{
01011720 55 push ebp
01011721 8B EC mov ebp,esp
01011723 81 EC AC 01 00 00 sub esp,1ACh
01011729 53 push ebx
0101172A 56 push esi
0101172B 57 push edi
0101172C 8D BD 54 FE FF FF lea edi,
01011732 B9 6B 00 00 00 mov ecx,6Bh
01011737 B8 CC CC CC CC mov eax,0CCCCCCCCh
0101173C F3 AB rep stos dword ptr es:
0101173E A1 18 90 01 01 mov eax,dword ptr
01011743 33 C5 xor eax,ebp
01011745 89 45 FC mov dword ptr ,eax
struct Book book = {0};
01011748 68 E0 00 00 00 push 0E0h
0101174D 6A 00 push 0
0101174F 8D 85 18 FF FF FF lea eax,
01011755 50 push eax
01011756 E8 68 F9 FF FF call _memset (010110C3h)
0101175B 83 C4 0C add esp,0Ch
Fun1(book);
0101175E 81 EC E0 00 00 00 sub esp,0E0h
01011764 B9 38 00 00 00 mov ecx,38h
01011769 8D B5 18 FF FF FF lea esi,
0101176F 8B FC mov edi,esp
01011771 F3 A5 rep movs dword ptr es:,dword ptr
01011773 E8 08 FB FF FF call _Fun1 (01011280h)
01011778 81 C4 E0 00 00 00 add esp,0E0h
Fun2(&book);
0101177E 8D 85 18 FF FF FF lea eax,
01011784 50 push eax
01011785 E8 FB FA FF FF call _Fun2 (01011285h)
0101178A 83 C4 04 add esp,4
return 0;
0101178D 33 C0 xor eax,eax
}
0101178F 52 push edx
01011790 8B CD mov ecx,ebp
01011792 50 push eax
01011793 8D 15 C0 17 01 01 lea edx,ds:
01011799 E8 B5 FA FF FF call @_RTC_CheckStackVars@8 (01011253h)
0101179E 58 pop eax
0101179F 5A pop edx
010117A0 5F pop edi
010117A1 5E pop esi
010117A2 5B pop ebx
010117A3 8B 4D FC mov ecx,dword ptr
010117A6 33 CD xor ecx,ebp
010117A8 E8 BA FA FF FF call @__security_check_cookie@4 (01011267h)
010117AD 81 C4 AC 01 00 00 add esp,1ACh
010117B3 3B EC cmp ebp,esp
010117B5 E8 63 F9 FF FF call __RTC_CheckEsp (0101111Dh)
010117BA 8B E5 mov esp,ebp
010117BC 5D pop ebp
010117BD C3 ret
没有符号名
int main(void)
{
01011720 55 push ebp
01011721 8B EC mov ebp,esp
01011723 81 EC AC 01 00 00 sub esp,1ACh
01011729 53 push ebx
0101172A 56 push esi
0101172B 57 push edi
0101172C 8D BD 54 FE FF FF lea edi,
01011732 B9 6B 00 00 00 mov ecx,6Bh
01011737 B8 CC CC CC CC mov eax,0CCCCCCCCh
0101173C F3 AB rep stos dword ptr es:
0101173E A1 18 90 01 01 mov eax,dword ptr ds:
01011743 33 C5 xor eax,ebp
01011745 89 45 FC mov dword ptr ,eax
struct Book book = {0};
01011748 68 E0 00 00 00 push 0E0h
0101174D 6A 00 push 0
0101174F 8D 85 18 FF FF FF lea eax,
01011755 50 push eax
01011756 E8 68 F9 FF FF call 010110C3
0101175B 83 C4 0C add esp,0Ch
Fun1(book);
0101175E 81 EC E0 00 00 00 sub esp,0E0h
01011764 B9 38 00 00 00 mov ecx,38h
01011769 8D B5 18 FF FF FF lea esi,
0101176F 8B FC mov edi,esp
01011771 F3 A5 rep movs dword ptr es:,dword ptr
01011773 E8 08 FB FF FF call 01011280
01011778 81 C4 E0 00 00 00 add esp,0E0h
Fun2(&book);
0101177E 8D 85 18 FF FF FF lea eax,
01011784 50 push eax
01011785 E8 FB FA FF FF call 01011285
0101178A 83 C4 04 add esp,4
return 0;
0101178D 33 C0 xor eax,eax
}
0101178F 52 push edx
01011790 8B CD mov ecx,ebp
01011792 50 push eax
01011793 8D 15 C0 17 01 01 lea edx,ds:
01011799 E8 B5 FA FF FF call 01011253
0101179E 58 pop eax
0101179F 5A pop edx
010117A0 5F pop edi
010117A1 5E pop esi
010117A2 5B pop ebx
010117A3 8B 4D FC mov ecx,dword ptr
010117A6 33 CD xor ecx,ebp
010117A8 E8 BA FA FF FF call 01011267
010117AD 81 C4 AC 01 00 00 add esp,1ACh
010117B3 3B EC cmp ebp,esp
010117B5 E8 63 F9 FF FF call 0101111D
010117BA 8B E5 mov esp,ebp
010117BC 5D pop ebp
010117BD C3 ret
页:
[1]