[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

function pointer

Posted on 2005-04-11
10
Medium Priority
?
337 Views
Last Modified: 2010-04-15
I want to use a function pointer and array of function pointers in C using this compiler: http://www.c-compiler.com/

but for some reason it will not compile. Does anyone know why?

Here is the program:

-----------------------------------------------------

#include <stdio.h>
#include <string.h>

void RedFunction();
void BlueFunction();
void GreenFunction();

void (*OneFunctionPointer)();
void (*ArrayOfPointers[2])();

int main(void)
{
      // Set
      OneFunctionPointer=&RedFunction;
      ArrayOfPointers[0]=&BlueFunction;
      ArrayOfPointers[1]=&GreenFunction;
      
      // Call
      OneFunctionPointer();
      ArrayOfPointers[0]();
      ArrayOfPointers[1]();
      
      // Does nothing but leave console screen up
      int input;
      scanf("%d", &input);
}

void RedFunction()
{
      printf("Red function called\n");
}

void BlueFunction()
{
      printf("Blue function called\n");
}

void GreenFunction()
{
      printf("Green function called\n");
}

Note: I have to use that compiler (for school)
0
Comment
Question by:Mag2000
  • 5
  • 5
10 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13755437


What error are you getting?

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13755478

The only things that appears to be a potential problem, is that the compiler may require that "int input" be part of the declarations block.  Move that line above the "// set" comment.


Good luck,
Kent
0
 

Author Comment

by:Mag2000
ID: 13755612
If you download the compiler and try to compile the program you'll see for yourself. Thats why I gave the link to the compiler.

Here is the error: c:\documents and settings\desktop\test.c: line 21: Parse Error, expecting `','' or `SEP'
'OneFunctionPointer()'

By the way I normally use MS Visual Studio. This compiler is a piece of **** compared to VS :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Mag2000
ID: 13755620
Oh and I put the "int input" above "// set" but that didn't work. For some reason this compiler doesn't like function pointers or is very picky with them.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13755822


Try changing to pointer declarations to this:

void (*OneFunctionPointer)(void);
void (*ArrayOfPointers[2])(void);


Kent
0
 

Author Comment

by:Mag2000
ID: 13755962
When I did that I got this error:

c:\documents and settings\desktop\test.c: line 8: Parse Error
'void (*OneFunctionPointer)(void)'
aborting compile

Thanks for helping by the way
0
 

Author Comment

by:Mag2000
ID: 13755971
When I comment out this part it then compiles:

// Call
OneFunctionPointer();
ArrayOfPointers[0]();
ArrayOfPointers[1]();
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13756434

Correct.

The compiler doesn't realize that OneFunctionPointer() is actually a function call by pointer.  My purpose in having you add 'void' to the definition was to make the definition look like a function prototype.

We probably need to add a function prototype prior to defining OneFunctionPointer.  I suspect that this a K&R compiler, not ANSI.

Let me play a bit.  I'll get back.....

Kent
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 300 total points
ID: 13756505


Modify these three lines.  The '&' is redundant, which makes it erroneous.


     OneFunctionPointer = RedFunction;
     ArrayOfPointers[0] = BlueFunction;
     ArrayOfPointers[1] = GreenFunction;

Sometimes it's so obvious that it hurts.....  My apologies for not seeing this sooner.
Kent
0
 

Author Comment

by:Mag2000
ID: 13756801
That didn't work either but I did figure out what it was.

I changed this:

// Call
OneFunctionPointer();
ArrayOfPointers[0]();
ArrayOfPointers[1]();

to this:

// Call
(*OneFunctionPointer)();
(*ArrayOfPointers[0])();
(*ArrayOfPointers[1])();

and it worked! Crazy isn't that. I'm still going to award you for helping.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question