Tag: 指针

如何使用printf从字符串数组中打印单个字符?

比方说我有 char *names[] = { “Tom”, “Jerry” }; 我想用printf打印“杰里”中的“e”。 我的第一直觉是 printf(“%c\n”, *names[5]); 但当我应用我一直在学习的指针时,我意识到这是完全垃圾代码,因为5指的是names不存在的第五个指针,而不是“Jerry”中的“e”。 names包含的指针只会引用各自字符串中第一个字符的内存地址。 因此,我真正需要做的是在names[1]添加一个字节以指向,并在“Jerry”中打印“e”。 但我不确定如何做到这一点,或者它是否在C中被允许。 完成此任务的最佳方法是什么? 先感谢您。

访问结构变量双指针

一些代码: typedef struct _WDF_USB_DEVICE_SELECT_CONFIG_PARAMS { ULONG Size; WdfUsbTargetDeviceSelectConfigType Type; union { struct { PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; PUSB_INTERFACE_DESCRIPTOR* InterfaceDescriptors; ULONG NumInterfaceDescriptors; } Descriptor; struct { PURB Urb; } Urb; } Types; } WDF_USB_DEVICE_SELECT_CONFIG_PARAMS,* PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS; WDF_USB_DEVICE_SELECT_CONFIG_PARAMS参数; typedef struct _USB_INTERFACE_DESCRIPTOR { UCHAR bLength ; UCHAR bInterfaceClass ; UCHAR bInterfaceSubClass ; } USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR ; 能够通过 – > params.Types.Descriptor.NumInterfaceDescriptors访问NumInterfaceDescriptors 我想通过WDF_USB_DEVICE_SELECT_CONFIG_PARAMS访问bInterfaceClass。 […]

连续的内存块与malloc

我试图在一个函数调用中创建一个连续的内存块,它将内存的第一部分作为指向其他块的指针数组。 基本上,我正在尝试: int **CreateInt2D(size_t rows, size_t cols) { int **p, **p1, **end; p = (int **)SafeMalloc(rows * sizeof(int *)); cols *= sizeof(int); for (end = p + rows, p1 = p; p1 < end; ++p1) *p1 = (int *)SafeMalloc(cols); return(p); } void *SafeMalloc(size_t size) { void *vp; if ((vp = malloc(size)) == NULL) { fputs("Out […]

Arduino:无法通过gcc编译器将union struct作为指针交流传递

我正在尝试使用Arduino结构但不能通过函数调用传递结构指针。 添加主要function并使用gcc为我的计算机编译时,一切正常,但使用Arduino IDE,我得到错误。 我试过的代码是: typedef union { struct { unsigned unit :2; unsigned channel:2; unsigned status :1; unsigned group :1; unsigned remote :26; }; unsigned long data; } Signal; Signal signal; void testPassingStruct(Signal *variable) { variable->status = 1; } void setup() { signal.status = 1; testPassingStruct(&signal); } void loop() { } 错误是: structtest:2: error: variable […]

为什么字符串复制function只是指定指针不起作用?

考虑strcpy这种实现: void my_strcpy(char target[], char source[]) { target = source; } int main(void) { char target[20]; char source[] = “Source String”; my_strcpy(target, source); printf(“Target: %s”, target); return 0; } 这不起作用,它让我质疑我对C中的字符串和数组的理解。 这是我的推理: target和source实际上只是指向数组的第一个元素的指针,即。 target == &target[0]和source == &source[0] 。 当我设置target = source ,我将指针target指向指向target = source指向的相同内存地址。 现在当我printf target ,它也应该打印”Source String” 。 但事实并非如此。 有人可以解释原因吗?

何时将指针作为参数传递给结构,何时将指针传递给指向结构的指针?

我的问题是关于以下代码。 #include #include struct node { int v; struct node * left; struct node * right; }; typedef struct node Node; struct bst { Node * root; }; typedef struct bst BST; BST * bst_insert(BST * tree, int newValue); Node * bst_insert_node(Node * node, int newValue); void bst_traverseInOrder(BST * tree); void bst_traverseInOrderNode(Node * node); int […]

在C函数中对数组进行排序

所以我需要将数组传递给函数sort并对它进行排序,它在外面工作,但不能让它在函数内部工作。 需要传递的指针只是不确定如何。 #include void sort(int *number, int n) { /* Sort the given array number, of length n */ int temp = 0, j, i; for (i = 1; i < n; i++) { for (j = 0; j number[j + 1]) { temp = number[j]; number[j] = number[j + 1]; number[j + 1] = […]

如何在函数之间传递数组

有人能帮我吗? 我遇到了C程序的问题。 这里是: 在我的主要部分,我调用一个函数(func A),其中两个参数是第二个函数(fun B)和“user data”(原则上可以是单个数字,char或数组)。 这个“用户数据”也是函数B的一个参数。当“用户数据”是一个整数但是现在我需要将它用作数组时,我已经完成了所有工作。 所以目前的工作结构是这样的: static int FunB(…,void *userdata_) { int *a=userdata_; … (here I use *a that in this case will be 47) … } int main() { int b=47; funcA(…,FunB,&b) } 所以现在我希望b在main中作为一个数组(例如{3,45}),以便将更多的单个“数据”传递给函数B. 谢谢

访问双指针

typedef struct _WDF_USB_DEVICE_SELECT_CONFIG_PARAMS { ULONG尺寸; WdfUsbTargetDeviceSelectConfigType类型; 联盟{ struct { PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; PUSB_INTERFACE_DESCRIPTOR* InterfaceDescriptors; ULONG NumInterfaceDescriptors; } Descriptor; struct { PURB Urb; } Urb; struct { UCHAR NumberConfiguredPipes; WDFUSBINTERFACE ConfiguredUsbInterface; } SingleInterface; struct { UCHAR NumberInterfaces; PWDF_USB_INTERFACE_SETTING_PAIR Pairs; UCHAR NumberOfConfiguredInterfaces; } MultiInterface; }类型; } WDF_USB_DEVICE_SELECT_CONFIG_PARAMS,* PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS; WDF_USB_DEVICE_SELECT_CONFIG_PARAMS参数; typedef struct _USB_INTERFACE_DESCRIPTOR { UCHAR bLength; UCHAR bInterfaceClass; UCHAR […]

为什么这个搜索函数会返回一个指向指针的指针?

#ifndef _BST_H_ /* Returns negative (leftright). */ typedef int comparator(void* left, void* right); struct bst_node { void* data; struct bst_node* left; struct bst_node* right; }; struct bst_node* new_node(void* data); void free_node(struct bst_node* node); struct bst_node** search(struct bst_node** root, comparator compare, void* data); void insert(struct bst_node** root, comparator compare, void* data); void delete(struct bst_node** node); #endif […]