Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

int length(PString * self) {     return strlen(self->chars); } 

I have a function initializeString() that returns a pointer to a 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). 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. 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. Thanks for any help you can give me.

like image 309
Jonathan Sterling Avatar asked Aug 29 '09 03:08

Jonathan Sterling


People also ask

Can you have a function pointer in a struct?

Function pointers can be stored in variables, structs, unions, and arrays and passed to and from functions just like any other pointer type. They can also be called: a variable of type function pointer can be used in place of a function name.

Can a function be a member of a struct in C?

You cannot have functions in structs in C; you can try to roughly simulate that by function pointers though.

Can a pointer be a structure member in C?

A pointer could be a member of structure, but you should be careful before creating the pointer as a member of structure in C. Generally we take a pointer as a member when we don't know the length of the data which need to store.

How can structure members access pointer?

To access members of a structure using pointers, we use the -> operator. In this example, the address of person1 is stored in the personPtr pointer using personPtr = &person1; . Now, you can access the members of person1 using the personPtr pointer.


2 Answers

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; } 
like image 103
KV Prajapati Avatar answered Sep 27 '22 20:09

KV Prajapati


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 *);.

...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)); 
like image 23
jtbandes Avatar answered Sep 27 '22 21:09

jtbandes