Structure to function (to print)

This is a discussion on Structure to function (to print) within the C Programming forums, part of the General Programming Boards category; Hi all, I've been struggling to learn structures, and I think I've got the structure part down now. I'm trying ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Post Structure to function (to print)

    Hi all,

    I've been struggling to learn structures, and I think I've got the structure part down now. I'm trying to create a print function to print what I've put into the structures, and I'm getting all sorts of errors from my compiler. Could someone tell me what I'm doing wrong? Thanks for taking the time to read this.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct movie{
    	int year;
    	int rating;
    	};
    
    void print_function (&movie *t);
    
    int main (void)
    {
    
    struct movie starwars;
    
      starwars.year = 1986;
      starwars.rating = 5;
    
    print_function(&movie *t);
    
    return 0;
    }
    
    void print_movie(&movie *t)
    {
    	printf("Year: %d\n", t->year);
    	printf("Year: %d\n", t->rating);
    
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    You have some syntactical problems. Let's look at a prototype for your function.

    void print_movie ( struct movie * m );

    The significance of movie * in the function signature is so that the compiler knows the type of your function's parameters. You don't need to include the type when you actually call your functions, just write the variable name.

    When you call your function, you may need to include & (the address-of operator) in front of the variable name. For example, starwars is not a pointer, but you don't have to make one: you can pass its address to the function and the compiler can take care of the rest.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by hello234 View Post
    Code:
    void print_function (&movie *t);
    You can't pass a pointer to a type only to an instance of that type so rewrite the prototype and the definiton of print_function().

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Citizen-- thanks for getting back to me. I was able to understand a lot of what you said.

    This is my new code, and it works up until where I define the function at the end. Right now, it does not understand the 'm' in the 'm->' portion of the printf statements. I think this is because I did not pass 'm' into the function. However, when I try to do that, there are errors with the function. Thank you

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct movie{
    	int year;
    	int rating;
    	};
    
    void print_function (struct movie * m);
    
    int main (void)
    {
    
    struct movie starwars;
    
    starwars.year = 1986;
    starwars.rating = 5;
    
    print_function(&starwars);
    
    return 0;
    }
    
    void print_movie(starwars)
    {
    
    	printf("Year: %d\n", m->year);
    	printf("Year: %d\n", m->rating);
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Change your prototype and the definition by making the parameter to print_movie() a pointer as in
    Code:
    void print_movie(struct movie *m)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by hello234 View Post
    Citizen-- thanks for getting back to me. I was able to understand a lot of what you said.

    This is my new code, and it works up until where I define the function at the end. Right now, it does not understand the 'm' in the 'm->' portion of the printf statements. I think this is because I did not pass 'm' into the function. However, when I try to do that, there are errors with the function. Thank you

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct movie{
    	int year;
    	int rating;
    	};
    
    void print_function (struct movie * m);
    
    int main (void)
    {
    
    struct movie starwars;
    
    starwars.year = 1986;
    starwars.rating = 5;
    
    print_function(&starwars);
    
    return 0;
    }
    
    void print_movie(starwars)
    {
    
    	printf("Year: %d\n", m->year);
    	printf("Year: %d\n", m->rating);
    
    }
    Well, actually, it doesn't understand "void print_movie(starwars)". It would complain about it, except for the fact that you never call it. You do call a function "void print_function (struct movie * m)", and you need to define that function.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    @hello: Excellent progress! Another important thing to remember about functions is the difference between where they are declared and defined. If you decide to write a prototype, you're actually declaring the function for that source file. Then the body of the function can be written elsewhere, such as at the bottom of the source file, like you have in your post.

    The body of a function is its definition, and you must include the correct signature. The web page below demonstrates this, and teaches these issues.

    http://www.cprogramming.com/tutorial/c/lesson4.html

    What's important for now is that you correct the signature in the definition of print_movie. m should be a pointer to struct movie.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    very bone-headed of me with changing the function names. thank you all for the help. here is the program that works in case future newbies search this forum

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct movie{
    	int year;
    	int rating;
    	};
    
    void print_function (struct movie *m);
    
    int main (void)
    {
    
    struct movie starwars;
    
    starwars.year = 1986;
    starwars.rating = 5;
    
    print_function(&starwars);
    
    return 0;
    }
    
    void print_function(struct movie *m)
    {
    
    	printf("Year: %d\n", m->year);
    	printf("Year: %d\n", m->rating);
    
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    I also notice that you had:
    void print_function (struct movie * m);
    ...at first and then changed to...
    void print_function (struct movie *m);

    Just so you are aware, the * does not have to be next to the name; it can be anywhere YOU want it to be.
    It can be
    void print_function (struct movie * m);
    Or
    void print_function (struct movie* m);
    Or even
    void print_function (struct movie*m);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    can you explain what the asterisk is actually doing here?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    It simply tells the compiler that it is a pointer. Everything to the left of the * is the type of the pointer.
    "int" is a variable of type int.
    "int*" is a pointer to int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    The asterisk is simply to note a pointer type. And contrary to what Elysia may tell you, everything to the left of * is not a pointer.

    int * p, q;
    In this example, q is an int, not a pointer to int.
    The C compiler ignores white space, so unless you have a stylistic objection, it doesn't matter how you declare pointers.
    Last edited by whiteflags; 11-02-2008 at 03:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:28 AM

Tags for this Thread


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