Thread: C programmers help with pointers

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001

    C programmers help with pointers

    int StrPrint(char *str)
    char str[24]="pointing to a function.";
    int (*ptr)(char *str);

    if (!(*ptr)(str))

    return 0;
    int StrPrint (char *str)
    printf("&s\n", str);
    return 0;
    /* I don't really know why there are pointers declared inside of a function,
    don't understand int (*ptr)(char *str); and the if statement. I mean this book just jumped into declaring pointers in a function without really explaining them. I was like you know understanding pointer a little bit, moving up in the address declaring and all that, but that just blew me off. Please any help will be greatlly appreciated */
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.

    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Ok, to declare a pointer, you declare it exactly the same way you
    would declare a standard (non-pointer) variable of the same type, except you add a '*' in front of it.

    /* variable */
    int x;

    /* pointer */
    int *y;

    Now, white space doesn't matter. These are all the same:

    int *y;
    int* y;

    Now you have a pointer that points to... something totally

    This is the major killer of applications. Pointers not being initialized
    before they're used. Thus, it's always a good idea to do something

    int *y=NULL;

    NULL means "nothing" basicly.

    Function pointers are basicly the same. You give them a name, you give them a return type, and you give them the arguments
    they take:

    int (*ptr)(char *str);

    Ok, this function returns an int.
    This function (pointer) is named 'ptr'.
    This function takes a string as an argument.

    Now you can assign the pointer to a function that fits that description:

    /* some dummy function */
    int myfunction( char* s ) { if( s ) return 0; return 1; }

    ptr = myFunction;

    That help?


  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001

    Yeah but........

    I understand somewhat what your're saying, but to understand it more could you please tell me what the function is doing, for example tell me what the program is doing at a certain point of the program, and etc. You basically told me about pointers how they behave now I wan to know how they behave relatd to this program. Sorry to bother you so much.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Aren't you a little new to be messing around with function pointers?

    #include <stdio.h>
    // a function which accepts a char pointer and returns an int
    // in this case, it prints the string and returns 0
    int StrPrint(char *str);
    int main() {
        char str[]="pointing to a function.";
        // a pointer to a function
        // the function being pointed to accepts a char * and returns an int
        // just like StrPrint
        int (*ptr)(char *str);
        // a function name without () is just a pointer to that function
        // so this is just copying a pointer value into a variable
        // just like any other assignment would do
        // this is how you would call the function normally
        if( !StrPrint(str) )
            printf( "Done!\n" );
        // this is an old way - where you have to specify that ptr
        // is actually a pointer
        if( !(*ptr)(str) )
            printf( "Done!\n" );
        // this is the ANSI-C way of referring to the function via a pointer
        // you can see the similarity to calling the actual function by name
        if( !ptr(str) )
            printf( "Done!\n" );
        return 0;
    int StrPrint(char *str) {
        printf( "%s\n", str );
        return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  4. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM