结构体指针
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Person
{
char *name;
int age;
int height;
int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
void Person_destroy(struct Person *who)
{
assert(who != NULL);
free(who->name);
free(who);
}
void Person_print(struct Person *who)
{
printf("Name: %s\n", who->name);
printf("\tAge: %d\n", who->age);
printf("\tHeight: %d\n", who->height);
printf("\tWeight: %d\n", who->weight);
}
int main(int argc, char *argv[])
{
// make two people structures
struct Person *joe = Person_create(
"Joe Alex", 32, 64, 140);
struct Person *frank = Person_create(
"Frank Blank", 20, 72, 180);
// print them out and where they are in memory
printf("Joe is at memory location %p:\n", joe);
Person_print(joe);
printf("Frank is at memory location %p:\n", frank);
Person_print(frank);
// make everyone age 20 years and print them again
joe->age += 20;
joe->height -= 2;
joe->weight += 40;
Person_print(joe);
frank->age += 20;
frank->weight += 20;
Person_print(frank);
// destroy them both so we clean up
Person_destroy(joe);
Person_destroy(frank);
return 0;
}
Joe is at memory location 0000000000FB6A80:
Name: Joe Alex
Age: 32
Height: 64
Weight: 140
Frank is at memory location 0000000000FB6AC0:
Name: Frank Blank
Age: 20
Height: 72
Weight: 180
Name: Joe Alex
Age: 52
Height: 62
Weight: 180
Name: Frank Blank
Age: 40
Height: 72
Weight: 200
函数指针
函数在C中实际上只是指向程序中某一个代码存在位置的指针。 就像你创建过的结构体指针、 字符串和数组那样, 你也可以创建指向函数的指针。
函数指针的主要用途是向其他函数传递“回调”, 或者模拟类和对象。 在这练习中我们会创建一些回调, 并且下一节我们会制作一个简单的对象系统。
- 函数指针的格式类似这样:
int (*POINTER_NAME)(int a, int b) - 记住如何编写它的一个方法是:
- 编写一个普通的函数声明: int callme(int a, int b)
- 将函数用指针语法包装: int (*callme)(int a, int b)
- 将名称改成指针名称: int (*compare_cb)(int a, int b)
这个方法的关键是, 当你完成这些之后, 指针的变量名称为 compare_cb , 而你
可以将它用作函数。 这类似于指向数组的指针可以表示所指向的数组。 指向函数的
指针也可以用作表示所指向的函数, 只不过是不同的名字。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include<string.h>
/** Our old friend die from ex17. */
void die(const char *message)
{
if (errno)
{
perror(message);
}
else
{
printf("ERROR: %s\n", message);
}
exit(1);
}
// a typedef creates a fake type,in this
// case for a function pointer
typedef int (*compare_cb)(int a, int b);
/**
* A classic bubble sort function that uses the
* compare_cb to do the sorting.
*/
int *bubble_sort(int *numbers, int count, compare_cb cmp)
{
int temp = 0;
int i = 0;
int j = 0;
int *target = malloc(count * sizeof(int));
if (!target)
die("Memory error.");
memcpy(target, numbers, count * sizeof(int));
for (i = 0; i < count; i++)
{
for (j = 0; j < count - 1; j++)
{
if (cmp(target[j], target[j + 1]) > 0)
{
temp = target[j + 1];
target[j + 1] = target[j];
target[j] = temp;
}
}
}
return target;
}
int sorted_order(int a, int b)
{
return a - b;
}
int reverse_order(int a, int b)
{
return b - a;
}
int strange_order(int a, int b)
{
if (a == 0 || b == 0)
{
return 0;
}
else
{
return a % b;
}
}
/***Used to test that we are sorting things correctly
*by doing the sort and printing it out.
*/
void test_sorting(int *numbers, int count, compare_cb cmp)
{
int i = 0;
int *sorted = bubble_sort(numbers, count, cmp);
if (!sorted)
die("Failed to sort as requested.");
for (i = 0; i < count; i++)
{
printf("%d ", sorted[i]);
}
printf("\n");
free(sorted);
}
int main(int argc, char *argv[])
{
if (argc < 2)
die("USAGE: ex18 4 3 1 5 6");
int count = argc - 1;
int i = 0;
char **inputs = argv + 1;
int *numbers = malloc(count * sizeof(int));
if (!numbers)
die("Memory error.");
for (i = 0; i < count; i++)
{
numbers[i] = atoi(inputs[i]);
}
test_sorting(numbers, count, sorted_order);
test_sorting(numbers, count, reverse_order);
test_sorting(numbers, count, strange_order);
free(numbers);
return 0;
}
C:\Users\17899\openairinterface5g>hello 4 1 7 3 2 0 8
0 1 2 3 4 7 8
8 7 4 3 2 1 0
3 4 2 7 1 0 8