www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/29/12:42:22

Sender: crough45 AT amc DOT de
Message-Id: <97Nov29.194034gmt+0100.17026@internet01.amc.de>
Date: Sat, 29 Nov 1997 18:39:02 +0100
From: Chris Croughton <crough45 AT amc DOT de>
Mime-Version: 1.0
To: tudor AT cam DOT org
Cc: djgpp AT delorie DOT com
Subject: Re: weird error: pointers to functions in structs

Vic <tudor AT cam DOT org> wrote:

> If I say
> but[number].(*action)(); I get a parse error before "("
> but if I say
> void (*thing)();
> thing=but[number].action;
> (*thing) ();
> it works and calls the right function. Why?

Because saying structure.(*fieldname) is not valid C
syntax.  It doesn't make sense.  If it were to make any sense
at all then that expression would mean something like:

  take the contents of the variable 'action' and use them
  to determine which field in 'structure' to access

C doesn't support that.

The thing which you need to dereference is the expression

  but[number].action

(which you do when you assign it to 'thing' first).  So
you need to call it as

  (*but[number].action)();

In practice, you don't need to because pointers to functions
are automagically dereferenced when called, so you can say

  but[number].action();

quite happily.

BTW, this is off-topic for this newsgroup, questions about C 
syntax should be asked in one of the C newsgroups.  Or ask 
your teacher.  Or read a C primer (Kernighan and Richie is
pretty good, and has sections about pointers, functions and 
structures).

Chris C

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019