[英]Function pointer as a member of a C struct
I have a struct as follows, with a pointer to a function called "length" that will return the length of the chars member.我有一个如下结构,带有一个指向名为“length”的函数的指针,该函数将返回 chars 成员的长度。
typedef struct pstring_t {
char * chars;
int (* length)();
} PString;
I have a function to return the length of the characters from a pointer to a PString:我有一个函数可以从指向 PString 的指针返回字符的长度:
int length(PString * self) {
return strlen(self->chars);
}
I have a function initializeString() that returns a pointer to a PString:我有一个函数 initializeString() 返回一个指向 PString 的指针:
PString * initializeString() {
PString *str;
str->length = &length;
return str;
}
It is clear that I am doing something very wrong with my pointers here, because the str->length = &length
line causes an EXC_BAD_ACCESS signal in my debugger, as does `return strlen(self->chars).很明显,我在这里的指针做错了,因为
str->length = &length
行在我的调试器中导致 EXC_BAD_ACCESS 信号,`return strlen(self->chars) 也是如此。 Does anyone have any insights into this problem?有没有人对这个问题有任何见解?
I specifically want to be able have the initializeString() function return a pointer to a PString, and the length function to use a pointer to a PString as input.我特别希望能够让 initializeString() 函数返回一个指向 PString 的指针,以及使用指向 PString 的指针作为输入的 length 函数。 This is just an experiment in implementing a rudimentary object-oriented system in C, but I don't have a lot of experience dealing with pointers head-on.
这只是在 C 中实现基本面向对象系统的一个实验,但我没有很多正面处理指针的经验。 Thanks for any help you can give me.
感谢你给与我的帮助。
Allocate memory to hold chars.分配内存来保存字符。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PString {
char *chars;
int (*length)(PString *self);
} PString;
int length(PString *self) {
return strlen(self->chars);
}
PString *initializeString(int n) {
PString *str = malloc(sizeof(PString));
str->chars = malloc(sizeof(char) * n);
str->length = length;
str->chars[0] = '\0'; //add a null terminator in case the string is used before any other initialization.
return str;
}
int main() {
PString *p = initializeString(30);
strcpy(p->chars, "Hello");
printf("\n%d", p->length(p));
return 0;
}
My guess is that part of your problem is the parameter lists not matching.我的猜测是您的问题的一部分是参数列表不匹配。
int (* length)();
and和
int length(PString * self)
are not the same.不一样。 It should be
int (* length)(PString *);
它应该是
int (* length)(PString *);
. .
...woah, it's Jon! ……哇,是乔恩!
Edit : and, as mentioned below, your struct pointer is never set to point to anything.编辑:并且,如下所述,您的结构指针从未设置为指向任何内容。 The way you're doing it would only work if you were declaring a plain struct, not a pointer.
你这样做的方式只有在你声明一个普通结构而不是一个指针时才有效。
str = (PString *)malloc(sizeof(PString));
Maybe I am missing something here, but did you allocate any memory for that PString before you accessed it?也许我在这里遗漏了一些东西,但是您在访问它之前是否为该 PString 分配了任何内存?
PString * initializeString() {
PString *str;
str = (PString *) malloc(sizeof(PString));
str->length = &length;
return str;
}
The pointer str
is never allocated.指针
str
永远不会被分配。 It should be malloc
'd before use.它应该在使用前
malloc
'd。
You can use also "void*" (void pointer) to send an address to the function.您还可以使用“void*”(空指针)向函数发送地址。
typedef struct pstring_t {
char * chars;
int(*length)(void*);
} PString;
int length(void* self) {
return strlen(((PString*)self)->chars);
}
PString initializeString() {
PString str;
str.length = &length;
return str;
}
int main()
{
PString p = initializeString();
p.chars = "Hello";
printf("Length: %i\n", p.length(&p));
return 0;
}
Output:输出:
Length: 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.