Thread: Problem passing pointer to function

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    20

    Problem passing pointer to function

    I am trying to access information within a structure. I am trying to do this by passing a pointer to the structure into a function. The function is declared like so:

    Code:
    void areaItems(Ptr *sPtr); //typedef Ptr is a pointer to item structure
    I am then calling on the function like so:

    Code:
    areaItems(&startPtr); //is this the right way to pass in the pointer to the first structure in my linked list???
    Hopefully I haven't screwed anything up yet.

    Here is the code for void areaItems:

    Code:
    void areaItems(Ptr *sPtr)
    {
       printf("%s\n", sPtr->name);
    }
    ...This is clearly not working....but why?

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Also, my sincere apologies for all the questions as of late (and for those of you who've had to repeat yourself throughout my mistakes). This forum has been the best help I've gotten thus far

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please tell me you didn't make 'Ptr' a pointer using a typedef? I think I'm going to put you on ignore now.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Yes, someone else mentioned this earlier. I actually got this straight out of my textbook. I'd be happy not to do it again, but I've been on the same assignment since I've joined the forum. I'm having trouble understanding all of the elements of the assignment, and would be glad to not do the same thing for future assignments once I grasp what the hell is going on in this assignment in my book. (The book is C: How to Program by Deitel)

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Because of the typedef pointers. My guess is your function prototype is really areaItems(struct node** sPtr); since Ptr is typedef as a pointer. Hence why you shouldn't do it. You just need to pass the pointer, e.g. like this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct myExample{
    	int x;
    };
    
    void foo(struct myExample*);
    
    int main(void){
    
    	struct myExample* foobar = malloc(sizeof(*foobar));
    
    	foo(foobar);
    
    	printf("%d", foobar->x);
    
    	free(foobar);
    	getchar();
    	return(0);
    }
    void foo(struct myExample* fooPTR){
    
    	fooPTR->x = 5;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If startPtr is a pointer to something, then passing your function the address of startPtr is not going to work. What you're then passing the function is a pointer to a pointer.

    Try just
    Code:
    areaItems(startPtr);
    And I agree with quzah. You've been strongly advised many times NOT to typedef pointers. Why are you still doing it?
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    TheBigH, see post #4. I'm not trying to ........ anyone off, haha, I really did get this out of my textbook, and I already said I wasn't going to do this again, not to mention apologizing for it (post 2) I've literally been working on the same assignment since I've joined the forum.

    Would this declaration work the same?

    Code:
    areaItems(Item *startPtr); //where Item is the typedef of the item structures?

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Yes and then just pass your Item pointer to the function directly, as I showed in the above example. (post #5)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When in doubt, test it out:
    Code:
    #include<stdio.h>
    struct foo 
    {
        int bar;
    };
    void bar( struct foo *baz )
    {
        if( baz != NULL )
            printf( "baz->bar = %d\n", baz->bar );
    }
    int main( void )
    {
        struct foo d = { 10 }, *l = &d;
        bar( &d );
        bar( l );
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Thank you Andrew and quzah (posts 8 and 9), trying now

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Code:
    void areaItems (int area, Item *sPtr); //I declare it
    
    //...later on
    
    areaItems(current, startPtr); //I call it
    
    void areaItems(int area, Item *sPtr)
    {
       printf("%s\n", sPtr->name);
    }
    Everything seems to be working correctly, but the print statement seems to print complete garbage. Are there any other errors that would cause this in the above code, or is my problem likely elsewhere?

    thanks

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Assuming startPtr is a Item*, then my guess is you didn't fix your problem from your other post. That would be you left the name member defined as a char* and then didn't allocate any memory for it. I warned you about this in your other thread.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have been given multiple clear working examples now. Learn from them, and post your code in a small complete example of what you are trying to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So you would need to have something like this: (NOTE: inside struct is a char array not a char*, also that strcpy needs to be used to place a value in there).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct myExample{
    	char name[30];
    	int x;
    };
    
    void foo(struct myExample*);
    
    int main(void){
    
    	struct myExample* foobar = malloc(sizeof(*foobar));
    
    	foobar->x = 5;
    	strcpy(foobar->name, "John");
    
    	foo(foobar);
    
    	
    
    	free(foobar);
    	getchar();
    	return(0);
    }
    void foo(struct myExample* fooPTR){
    
    	printf ("%d\n%s", fooPTR->x , fooPTR->name);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    You were right, that's what was wrong. I am going to have to gut a lot of my code because I am making these same mistakes in multiple places. =/

    At the end of your code, what is the getchar();? Is that just holding the program waiting for a keystroke...or something?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a function pointer
    By Akkernight in forum C++ Programming
    Replies: 17
    Last Post: 03-22-2009, 04:41 AM
  2. c ++ passing a pointer into a function
    By Ron in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2008, 08:31 PM
  3. problem with pointer passing to function
    By umeshjaviya in forum C Programming
    Replies: 3
    Last Post: 05-02-2008, 05:44 AM
  4. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM

Tags for this Thread