C programmers help please...........help with pointers

This is a discussion on C programmers help please...........help with pointers within the C Programming forums, part of the General Programming Boards category; int StrPrint(char *str) main() char str[24]="pointing to a function."; int (*ptr)(char *str); ptr=StrPrint; if (!(*ptr)(str)) printf("Done!\n"); return 0; } int ...

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

    C programmers help please...........help with pointers

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

    ptr=StrPrint;
    if (!(*ptr)(str))
    printf("Done!\n");

    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
    Posts
    14,826
    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;
    int*y;

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

    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
    like:

    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?

    Quzah.

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Aren't you a little new to be messing around with function pointers?

    Code:
    #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
        ptr=StrPrint;
    
        // 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21