Array of structs function pointer

Joined
Jul 16, 2023
Messages
8
Reaction score
3
The first program below calls a function using a pointer. I am now trying to get the same mechanism to work using an array of structs. The last line (commented out) will not work. What am I doing wrong?

This works...
C:
#include <stdio.h>
#include <string.h>

void func(int a)  {   // normal function
  printf("Value of a is %d\n", a);
}

int main() {
  void (*func_ptr)(int) = func;         // assign ptr to func address
  func_ptr(10); // call func
  return 0;
}

This does not work...
C:
#include <stdio.h>
#include <string.h>

// data structure
struct book {
  int     book_id;                     // book id
  int     (*prtFunc) ( struct book  ); // addr of print function
  char    title[50];                   // book title
  char    author[50];                  // book author
};

struct book arBookList[10];

void printBook ( struct book arBookList[], int id ) {
  printf ( "book_id       : %d\n", arBookList[id].book_id );    // access to struc
  printf ( "func addr     : %p\n", arBookList[id].prtFunc );
  printf ( "title         : %s\n", arBookList[id].title   );
  printf ( "author        : %s\n", arBookList[id].author  );     // access to a struc in struc
  printf ( "\n" );
}

int main (  ) {

  arBookList[0].book_id =  0 ;
  arBookList[0].prtFunc = printBook;
  strcpy ( arBookList[0].title, "This Little Piggy" );
  strcpy ( arBookList[0].author, "Bad Wolf" );
  printBook (arBookList, 0 );                             // show data using function call

  arBookList[1].book_id =  1 ;
  arBookList[1].prtFunc = printBook;
  strcpy ( arBookList[1].title, "Mary Had a Lamb" );
  strcpy ( arBookList[1].author, "Snow Fleece" );
  printBook (arBookList, 1 );                             // show data using function call

  // call function using pointer
  void (*func_ptr)(struct book, int) = printBook;         // assign ptr to func address
 
  // func_ptr (arBookList, 1 );                              // <--- does not work, why ???

  return 0;
}
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
C:
struct book {
  int     book_id;                     // book id
  void    (*prtFunc) (struct book[], int); // address of print function ???
  char    title[50];                   // book title
  char    author[50];                  // book author
};

?
 
Joined
Jul 16, 2023
Messages
8
Reaction score
3
C:
struct book {
  int     book_id;                     // book id
  void    (*prtFunc) (struct book[], int); // address of print function ???
  char    title[50];                   // book title
  char    author[50];                  // book author
};

?

C allows you to define your own data types called "struct" which allow a mixture of data types, google "C struct" - for example https://www.w3schools.com/c/c_structs.php

The line above you clip says:
// data structure
which should be self explanatory.
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
Yes, You are right. And in programming exist also so called "classes".
Are You interested in my opinion on Your activity in pointers to function area?
 
Joined
Jul 16, 2023
Messages
8
Reaction score
3
With help on stack overflow, I found I was formatting my pointers wrong. I needed (still don't quite understand it) *pointer type to my *pointer... but only when working with structs. This is odd to me because passing an array is a pointer yet you do not need to use a "pointer type".

Anyway, the corrected code is here.

C:
struct book {                          // struct data
  int     book_id;                     // book id
  void     (*prtFunc) (struct book  *, int); // eventually, addr of print function
  char    title[50];                   // book title
  char    author[50];                  // book author
};

struct book arBookList[10];

void printBook ( struct book *arBookList, int id ) {           // test struct access
  printf ( "book_id       : %d\n", arBookList[id].book_id );  
  printf ( "func addr     : %p\n", arBookList[id].prtFunc );    // eventually this will call function
  printf ( "title         : %s\n", arBookList[id].title   );    // string data test
  printf ( "author        : %s\n", arBookList[id].author  );    // string
  printf ( "\n" );
}

int main (  ) {

  arBookList[0].book_id =  0 ;
  arBookList[0].prtFunc = printBook;
  strcpy ( arBookList[0].title, "This Little Piggy" );
  strcpy ( arBookList[0].author, "Bad Wolf" );
  printBook (arBookList, 0 );                             // show data using function call

  arBookList[1].book_id =  1 ;
  arBookList[1].prtFunc = printBook;
  strcpy ( arBookList[1].title, "Mary Had a Lamb" );
  strcpy ( arBookList[1].author, "Snow Fleece" );
  printBook (arBookList, 1 );                             // show data using function call

  // call function using pointer
  void (*func_ptr)(struct book *, int) = printBook;         // assign ptr to func address
 
  func_ptr (arBookList, 1 );                              // <--- does not work, why ???
  // commenting out the above line compiles ok
  // but it looks ok to me except for different call parameters

  return 0;
}
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
Now tell me please what is the reason of use it? I mean, You do good thing to learning C, the best choice of your life, but there classes waiting for You. And winapi. After that HTML ,CSS, Javascript, PHP. And know You stopped with something that is not necessery.
 
Joined
Jul 16, 2023
Messages
8
Reaction score
3
This is a proof of concept for having a database where you can call a unique function for a particular set of data. What I will actually use it for is saving a graphic button and be able to point to a specific function for that button. C does not have classes, this is a substitution for having a class (data + code to act on the data).
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
That's correct.
If You want to create database check this http://infinityhost.ct8.pl/ - C Area - UI+database.
Take a look what is necessery to create something usefull.
You can try to conver this to C-self-mad-class solution.
There can be many, many windows connected (some visible, some not) and then try to add SQL-self-made-solution.
 
Joined
Jul 16, 2023
Messages
8
Reaction score
3
Some pages of your website format is messed up for Firefox, making the site pretty much unusable. You will want to do some testing.
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
ok thx what exactly? (buttons on one page - that I want to rewrite all in time, but something else?) and download button probably ,but files are avaible in main page main directory
 
Last edited:
Joined
Jul 22, 2023
Messages
1
Reaction score
1
Your function's signature is void (struct book *, int id) but you set your function pointer's type to void (struct book, int id), which is incorrect. If you change it to void (*func_ptr)(struct book *, int) then it will work. (keep in mind that arrays decay to pointers, so writing [] is equivalent to just writing *)

also, your pointer prtFunc has its return type set to int, but your print function returns void, so you may want to fix that also (by changing the return types of either one of those two).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top