473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

function pointers

hi all...

can anyone help me out regarding passing function pointer as argument to a function?
May 4 '07 #1
5 1269
gpraghuram
1,275 Expert 1GB
Hi,
You shuld pass the prtotype of the function pointer as argument
Expand|Select|Wrap|Line Numbers
  1.  
  2. int add(int x)
  3. {
  4. }
  5. int calladd(int (*fun)(int))
  6. {
  7. }
  8. main()
  9. {
  10. calladd(add);
  11. }
  12.  
Thanks
Raghuram
May 4 '07 #2
Thank you Raghuram...

Its working!! I have a new problem. It seems its possible to access a float variable using a char pointer. If so, how can I get the 4 bytes of float variable value using char pointer with each byte separately and 2 hex digits in each byte...
Hope am clear..

Regards,
Rohini


Hi,
You shuld pass the prtotype of the function pointer as argument
Expand|Select|Wrap|Line Numbers
  1.  
  2. int add(int x)
  3. {
  4. }
  5. int calladd(int (*fun)(int))
  6. {
  7. }
  8. main()
  9. {
  10. calladd(add);
  11. }
  12.  
Thanks
Raghuram
May 5 '07 #3
AdrianH
1,251 Expert 1GB
Thank you Raghuram...

Its working!! I have a new problem. It seems its possible to access a float variable using a char pointer. If so, how can I get the 4 bytes of float variable value using char pointer with each byte separately and 2 hex digits in each byte...
Hope am clear..
I guess you are clear enough.

My question is, why do you want to access a float variable using a char pointer? One doesn't just wake up one day and say, "Hey! I think it would be neat-o to read a float using a char pointer." You should have a good reason for it. Bypassing the type safety system is not usually a good idea if you can do it another way. Also, doing what you are asking may not be what you are wanting anyway.

Let me know,


Adrian
May 5 '07 #4
Hi Adrian

Sorry for the delay. Actually I have a problem in which I have to display all four bytes of a float variable as four 2 digit hex numbers using a pointer.So, I guessed it can be done using char pointer. Is it actualy possible with char pointer? If not, how can it be done?

Regards,
Rohini


I guess you are clear enough.

My question is, why do you want to access a float variable using a char pointer? One doesn't just wake up one day and say, "Hey! I think it would be neat-o to read a float using a char pointer." You should have a good reason for it. Bypassing the type safety system is not usually a good idea if you can do it another way. Also, doing what you are asking may not be what you are wanting anyway.

Let me know,


Adrian
May 19 '07 #5
AdrianH
1,251 Expert 1GB
Hi Adrian

Sorry for the delay. Actually I have a problem in which I have to display all four bytes of a float variable as four 2 digit hex numbers using a pointer.So, I guessed it can be done using char pointer. Is it actualy possible with char pointer? If not, how can it be done?

Regards,
Rohini
So you really do want the binary access to the float. Yes, you use a char pointer. You have to cast the float pointer to a char pointer and then (usually) assign that char pointer to an char pointer variable. Then you can use that pointer variable as you would an array.


Adrian
May 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
12
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.