例如,我必须在函数中使用以下参数创建结构:一个函数指针,格式为:void(*func)(void*)和一个int id.
要创建的结构如下:
typedef struct example {
int id;
void (*func)(void *);
} Example;
Run Code Online (Sandbox Code Playgroud)
现在我的问题是当我为整个结构我的malloc空间时,我是否也为void函数指针的malloc空间,因为它是一个指针?如果是这样,怎么样,因为我无法获得无效指针的大小?谢谢.
这是我的想法:sizeof()是一个计算变量有多大的运算符.sizeof(variable type)可以计算某种类型的大小.数组中元素的数量由下式给出sizeof(<array name>) / sizeof(variable type).
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double b[] = {1 , 2 , 3 , 4 , 5};
printf("How many elements the array contains : %d\n" , sizeof(b) / sizeof(double));
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为5是正确的.
我想知道是否有更有效的方法来计算它?比方说,一个C函数.
我不明白为什么3&0x1111 = 1?看起来:
对于任何无符号的32位整数i,i和0x1111应该是i,对吗?
但是当我在ubuntu 14.04上尝试这个时,我得到了3 & 0x1111=1.为什么?
int main() {
unsigned int a =3;
printf("size of a= %lu\n",sizeof(a));
printf("value of 3 & 0x1111= %d\n",a & 0x1111);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当我运行这个代码我得到输出为4.但我的字符串长度是3为什么它给4?
#include <stdio.h>
int main(void)
{
printf("%d",sizeof("abc"));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 使用sizeof()的函数的大小始终返回1
我只是试图找到一个函数的大小.虽然使用sizeof()来查找它总是返回的大小1.即使函数是一个定义良好的函数它只返回1.我只想知道为什么它返回1.在说sizeof(功能)时它实际上考虑了什么.
考虑一个简单的交换功能.我用fun3来表示占用的地址差异.
void fun1(int a,int b)
{
int temp;
temp=a;
a=i;
i=b;
printf("%d\t %d\n",a,b);
}
void fun2()
{
}
void fun3()
{
}
void main()
{
printf("fun1\t Address : %d \t Size : %d\n",fun1,sizeof(fun1));
printf("fun2\t Address : %d \t Size : %d\n",fun2,sizeof(fun2));
printf("fun3\t Address : %d \t Size : %d\n",fun3,sizeof(fun3));
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是
fun1地址:4199248大小:1
fun2地址:4199301大小:1
fun3地址:4199307文件大小:1
fun1占用的地址数量巨大,fun2仅占用6.但两者的大小保持不变
我正在尝试用C编写一个简单的游戏,我得到了一个SEGFAULT,并且不知道为什么!
这是该程序的代码:
#include <stdio.h>
#include <string.h>
#define MAX_PLYS_PER_GAME (1024)
#define MAX_LEN (100)
typedef struct {
char positionHistory[MAX_PLYS_PER_GAME][MAX_LEN];
} Game;
void getInitialGame(Game * game) {
memset(game->positionHistory, 0, MAX_PLYS_PER_GAME*MAX_LEN*sizeof(char));
}
void printGame(Game game) {
printf("Game -> %p (%d)\n", &game, sizeof(game));
fflush(stdout);
}
int hasGameEnded(Game game) {
printGame(game);
return 0;
}
int main(int argc, char *argv[]) {
Game game;
getInitialGame(&game);
if (hasGameEnded(game))
return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我尝试用gdb调试,但结果并没有让我太过分:
C:\Users\test>gdb test.exe
GNU gdb 5.1.1 (mingw experimental)
<snip>
This GDB was configured as "mingw32"...
(gdb) …Run Code Online (Sandbox Code Playgroud) 我知道在SO上已经多次受到质疑,但我的问题是ANSI C(C89)特有的.
在C99有子符z和t,其未在ANSI下支持
使用说明p符怎么样?
在将0分配给count预期之前,先调用malloc 。但是,如果我malloc在给0赋值后调用,count它将覆盖的值count。count每次运行时,我都会打印出一个随机数。
有人可以解释为什么count会被覆盖吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct list list;
struct list {
size_t element_size;
size_t capacity;
size_t count;
void* data;
};
list* create_list(size_t element_size) {
list* list = malloc(sizeof(list));
list->element_size = element_size;
list->capacity = 8;
list->data = malloc(8 * element_size);
list->count = 0;
// If malloc is called here instead then count is overwritten
// list->data = malloc(8 * element_size);
printf("count: %zu\n", list->count);
return list;
} …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,其中#define用于替换程序中的'int',如下所示
#define type int
int main()
{
type *a,b;
}
Run Code Online (Sandbox Code Playgroud)
这是有效的吗?虽然它在我试图打印变量b的大小时给了我错误,说b是未声明的.我想知道这背后的具体原因.请告诉我.
有些用户告诉我,我隐藏了部分代码.我没有在上面的代码片段中给出printf语句.下面是printf的代码和给出错误的代码
#define type int;
int main()
{
type* a, b;
printf("%d",sizeof(b));
}
Run Code Online (Sandbox Code Playgroud)