一些新手小疑点,总是不知道,望前辈们多多指点啊多谢
一些新手小问题,总是不知道,望前辈们多多指点啊,谢谢!
函数指针:
1.定义:typedef void (F1)(int p1, int p2)
typedef void (*F2)(int p1, int p2)
test (int p1, int p2);
F1 * fp1;
F2 fp2;
F1 fp3;
问: 语句
fp3 = test;
fp2 = test;
fp1 = test
是否成立?
答案:第一条语句有错,其余两天语句正确。
麻烦大家能帮我一条一条解释一下。多谢。
2.struct struct_A{
int a;
char b;
int c;
short d;
}
struct struct_B{
int a;
char b;
short c;
int d;
}
struct_A a;
struct _B b;
问:sizeof(struct_A a) = ?
sizeof(struct_A b) = ?
答案:sizeof(a) 为16,sizeof(struct_A b)为12
为什么???
3.#include <stdio.h>
struct
{
char bit1:5;
int b:8;
}a;
void main()
{
printf( "%d ",sizeof(a));
}
为什么在32位的8个字节????
先谢谢了!
------解决方案--------------------
第一个是定义的问题,类型不匹配
第二个是内存对齐的问题
第三个是内存对齐的问题
------解决方案--------------------
你咋满大街发新贴?
赶快把上个结了去~~
------解决方案--------------------
我也不知道
------解决方案--------------------
第一题:
函数指针 typedef void (*F2)(int p1, int p2); F2 fp2; 没有什么问题。
因为typedef是类型声明的重定义,不是创造什么新类型。
typedef void (F1)(int p1, int p2); 这种写法没有遇到过,期待高人
难道 void (int p1, int p2)是个类型什么的。
第二第三题目:
是内存对齐问题。 说的更具体点就是结构体成员对齐。
如果你用的是VC6编译器,按Ctrl+F7---> C/C++选项卡---> Category设置为Code Generation---> 在struct member alighment中可以设置1字节,2字节,4字节等对齐方式。
一般默认是4字节对齐,如果是4字节就是表示 不足4字节补充为4字节了。为什么默认是4字节呢,因为32机器CPU字长为4字节,CPU一下处理4字节比较容易,反而处理1字节难。
随便解释一个吧,其它的雷同
struct struct_B{
int a;
char b;
short c;
int d;
}
int(32) = 32bit 占4个字节 char+short < 32bit 占4个字节 int占4个字节
------解决方案--------------------
tongyi楼上
------解决方案--------------------
谁把第一题在讲一下?
------解决方案--------------------
typedef void (F1)(int p1, int p2); //补充分号
typedef void (*F2)(int p1, int p2);//补充分号
void test (int p1, int p2); //补充函数返回类型
void main()
{
F1 * fp1;
F2 fp2;
F1 fp3;
fp3 = test;
test=test; //编译时补充的这一行会返回与上面一行同样的错误
fp2 = test;
fp1 = test;
}
------解决方案--------------------
typedef void (F1)(int p1, int p2);
这个不是函数指针,也不是函数引用
不能作为左值
函数指针:
1.定义:typedef void (F1)(int p1, int p2)
typedef void (*F2)(int p1, int p2)
test (int p1, int p2);
F1 * fp1;
F2 fp2;
F1 fp3;
问: 语句
fp3 = test;
fp2 = test;
fp1 = test
是否成立?
答案:第一条语句有错,其余两天语句正确。
麻烦大家能帮我一条一条解释一下。多谢。
2.struct struct_A{
int a;
char b;
int c;
short d;
}
struct struct_B{
int a;
char b;
short c;
int d;
}
struct_A a;
struct _B b;
问:sizeof(struct_A a) = ?
sizeof(struct_A b) = ?
答案:sizeof(a) 为16,sizeof(struct_A b)为12
为什么???
3.#include <stdio.h>
struct
{
char bit1:5;
int b:8;
}a;
void main()
{
printf( "%d ",sizeof(a));
}
为什么在32位的8个字节????
先谢谢了!
------解决方案--------------------
第一个是定义的问题,类型不匹配
第二个是内存对齐的问题
第三个是内存对齐的问题
------解决方案--------------------
你咋满大街发新贴?
赶快把上个结了去~~
------解决方案--------------------
我也不知道
------解决方案--------------------
第一题:
函数指针 typedef void (*F2)(int p1, int p2); F2 fp2; 没有什么问题。
因为typedef是类型声明的重定义,不是创造什么新类型。
typedef void (F1)(int p1, int p2); 这种写法没有遇到过,期待高人
难道 void (int p1, int p2)是个类型什么的。
第二第三题目:
是内存对齐问题。 说的更具体点就是结构体成员对齐。
如果你用的是VC6编译器,按Ctrl+F7---> C/C++选项卡---> Category设置为Code Generation---> 在struct member alighment中可以设置1字节,2字节,4字节等对齐方式。
一般默认是4字节对齐,如果是4字节就是表示 不足4字节补充为4字节了。为什么默认是4字节呢,因为32机器CPU字长为4字节,CPU一下处理4字节比较容易,反而处理1字节难。
随便解释一个吧,其它的雷同
struct struct_B{
int a;
char b;
short c;
int d;
}
int(32) = 32bit 占4个字节 char+short < 32bit 占4个字节 int占4个字节
------解决方案--------------------
tongyi楼上
------解决方案--------------------
谁把第一题在讲一下?
------解决方案--------------------
typedef void (F1)(int p1, int p2); //补充分号
typedef void (*F2)(int p1, int p2);//补充分号
void test (int p1, int p2); //补充函数返回类型
void main()
{
F1 * fp1;
F2 fp2;
F1 fp3;
fp3 = test;
test=test; //编译时补充的这一行会返回与上面一行同样的错误
fp2 = test;
fp1 = test;
}
------解决方案--------------------
typedef void (F1)(int p1, int p2);
这个不是函数指针,也不是函数引用
不能作为左值