Tag: 指针

如何正确访问此结构中的指针?

下面是2个结构的定义,然后是使用它们的简短方法体。 我不明白为什么编译器会抛出错误: physics.c:95:错误:赋值中不兼容的类型 cpBody和cpSpace是来自外部库的类型,这不是问题的一部分。 typedef struct gameBody gameBody; struct gameBody { cpBody *body; int numberOfShapes; cpShape *arrayOfShapes; //This stores an array of pointers to Shapes }; //Struct that stores the cpSpace object and the array of pointers to the body objects typedef struct gameSpace gameSpace; struct gameSpace { cpSpace *space; int numberOfObjects; gameBody *arrayOfObjects; //This […]

Calloc里面的function

看看刚刚被问过的这个问题: 对静态变量指针的不便会做这样的事情被认为是不好的做法,那么呢? char* strpart(char* string, int start, int count) { char* strtemp; int i = 0; int j = 0; int strL = strlen(string); if ( count == 0 ) { count = strL; } strtemp = (char*) calloc((count + 1), sizeof(char)); for ( i = start; i < (start+count); i++ ) { strtemp[j] = […]

在这个C拼图中要替换什么?

现在这是我从一些试卷中得到的一个愚蠢的谜题,遗憾的是我无法在过去的15分钟内弄明白。 #include int main(void){ /* */ putchar(*(wer[1]+1)); return 0; } 为了获得输出e我们应该用什么替换代替。现在我们知道putchar将int作为参数,但是这段代码假设给出一个指针。这个问题是否有效?

Nf_hook_ops在分配给hook_func -C -Linux -Netfilter时返回不兼容的指针

我试图在Ubuntu 16.04 LTS上编写自己的Netfilter内核模块,我试图将hook_func分配给nfho.hook,但是我收到以下错误: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types] nfho.hook = hook_func; 我已经看过其他解决方案,大多数都是双重检查参数,包括将*skb更改为**skb 。 我已经读过,参数可能取决于内核版本,但无法找到如何找到要传递的正确参数。 我怎样才能使它工作,我如何检查应该在我的内核版本的hook_func中传递什么参数? 完整代码: #include #include #include #include #include #include #include static struct nf_hook_ops nfho; //struct holding set of hook function options // function to be called by hook unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, […]

使用指针在C中查找2D数组的尺寸

我在C中创建一个2D数组,如下所示: int **arr; arr = malloc(rows * sizeof(int *)); for (i = 0; i < rows; i++) arr[i] = malloc(cols * sizeof(int)); 现在,我打电话给: func(arr) 在函数func ,如何计算行和列尺寸?

C中的数组赋值使用指针算法

当我使用指针算法访问特定元素时,如何更改数组中的值? #include int main() { int a[3] = {1, 1, 1}, b[3] = {2, 2, 2}; a++ = b++; // How can I get this to work so a[1] = b[1]? return 0; }

节点* node1与node * node1的区别是什么?

我有1个节点: struct node { int key; struct node *left, *right; }; 有什么区别 node *node1 VS node* node1 ?

C指针帮助:数组/指针等效

在这个玩具代码示例中: int MAX = 5; void fillArray(int** someArray, int* blah) { int i; for (i=0; i<MAX; i++) (*someArray)[i] = blah[i]; // segfault happens here } int main() { int someArray[MAX]; int blah[] = {1, 2, 3, 4, 5}; fillArray(&someArray, blah); return 0; } …我想填充数组someArray,并在函数外部保留更改。 这是一个非常大的家庭作业的一部分,这个问题解决了这个问题,而不允许我复制解决方案。 我得到一个函数签名,接受一个int **作为参数,我应该编写逻辑来填充该数组。 我的印象是fillArray()函数中的dereferencing&someArray会给我所需的数组(指向第一个元素的指针),并且使用该数组上的括号数组元素访问将为我提供需要分配的必要位置。 但是,我无法弄清楚为什么我会遇到段错误。 非常感谢!

如何定义函数指针数组并且函数没有相同的输入参数定义?

是否可以定义一个函数指针数组(并且函数没有相同的输入参数),如下面的代码所示? 如果是的话我必须把它放在函数定义int (*handler)(/*what Ihave to put here ?*/); struct handler_index { const char *name; int (*handler)(/*what Ihave to put here ?*/); }; int handler0 (int a, int b) { printf(“%d\n”,a+b); } int handler1 (int a, int b, int c) { printf(“%d\n”,a+b+c); } int handler2 (int a, int b, int c, int d) { printf(“%d\n”,a+b+c+d); } […]

函数中的struct指针范围

如果我在函数中有以下内容: struct example *A=malloc(sizeof(struct example)); 节点/内存空间是否在函数结束/离开后A点指向被销毁? 还是它留在堆里?