LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2005, 12:41 PM   #1
devinWhalen
Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Red Hat, openBSD,Mandrake,freeBSD,SunOS
Posts: 168

Rep: Reputation: 30
C Programming: Help with Pointers to Functions


Hello,

I am just learning C so this might be an obvious question so bare with me....

I am writing a small program, much like grep. I search the files sent in and look for a search word. Like grep I am trying to add functionaility for an ignore case search. I want to use a pointer to a function. The functions I will use will take two char pointers and return int.
Here is a snippet of my code:

Code:
int (*comp)(char *, char *);

    if(!ignore_case)
        comp = notexact;
     else
             comp = exact;
int success = search_line(next_line, find, &search_word, comp);
I haven't made notexact yet but exact is defined as:

Code:
int exact(char *string1, char *string2)
{
 return(strcmp(string1, string2));
}
Instead of returning strcmp, I figured it would be faster to send in a pointer to strcmp like so:

Code:
int (*comp)(char *, char *);
if(!ignore_case)
        comp = notexact;
     else
             comp = strcmp;   
//comp = exact;
However, when I compile this I get the following error:
warning: assignment from incompatible pointer type

Why won't this work? Isn't strcmp defined exactly as my function exact?

Thanks for any help or pointers.....no pun intended

Later
 
Old 03-04-2005, 12:53 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Here is an example that works for me:

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

int main (void)
{
        int (*comp)(const char*, const char*);
        comp = strcmp;

        printf("%d\n",comp("same","same"));
        printf("%d\n",comp("same","notsame"));
        printf("%d\n",comp("notsame","same"));
        return 0;
}
Your problem I think is your function pointer doesn't strictly resemble the strcmp function declaration ("int strcmp(const char *s1, const char *s2);");
 
Old 03-04-2005, 01:00 PM   #3
devinWhalen
Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Red Hat, openBSD,Mandrake,freeBSD,SunOS
Posts: 168

Original Poster
Rep: Reputation: 30
Hey,

Thanks for the help. Now it makes perfect sense! I didn't know that strcmp took const chars.

Later
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming pointers & recursive exvor Programming 8 08-16-2005 09:24 AM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM
c programming pointers (a factorial calculation) Randall Programming 5 02-23-2003 12:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration