Like Tree1Likes
  • 1 Post By laserlight

Structures and Pointer

This is a discussion on Structures and Pointer within the C Programming forums, part of the General Programming Boards category; For a HW assignment I have to create a structure called Article Code: typedef struct { int number; int quantity; ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Structures and Pointer

    For a HW assignment I have to create a structure called Article
    Code:
    typedef struct
    {
    	int number;
    	int quantity;
    	char description[STR_LEN+1];
    } Article;
    Contents are assigned at initialization level

    Print Article w/ Print() function (function gets address of the struct as a parameter)

    I am trying to create a header file which defines the struct and print() function.

    Code:
    #ifndef PRINT_ART
    #define STR_LEN 20
    #define PRINT
    
    
    typedef struct
    {
    	int number;
    	int quantity;
    	char description[STR_LEN+1];
    } Article;
    
    
    
    
    
    
    void Print(struct Article *p)
    {
    	(printf("(%d,%d,%s)/n", p.number, p.quantity, p.description);
    }
    
    
    #endif
    But I am confused on how I assign the pointer p to Article.

    I assume once I get this correct my cpp file would be main(), assign values to the struct members and call the print function.

    Can anyone help my get this straight.
    Thanks
    scott

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Your typedef introuces the type Article that is the same as an unnamed struct: importantly, you do not have a type struct Article. To have both you'd need this
    Code:
    typedef struct Article
    {
        int number;
        int quantity;
        char description[STR_LEN+1];
    } Article;
    Later on in your code you say function Print() takes a pointer to a struct Article which effectively does not exist!

    I suggest you do not use typedef in your code (except maybe for function types). Typing an extra "struct" every now and then is not that much work.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by sjmp View Post
    I am trying to create a header file which defines the struct and print() function.
    You shouldn't define the function in the header but just declare it. You should define it in a source file with the same name (with extension .c)

    Code:
    void Print(struct Article *p)
    {
        (printf("(%d,%d,%s)/n", p.number, p.quantity, p.description);
    }
    Read FAQ > All about structures - Cprogramming.com especially the section "Accessing structure variables".

    Quote Originally Posted by sjmp View Post
    But I am confused on how I assign the pointer p to Article.
    If you have the struct stored in a variable, then the &-operator gives you a pointer to it and you would call Print() like
    Code:
    Print(&struct_variable);
    Quote Originally Posted by sjmp View Post
    ...my cpp file...
    The C++ forum is next door.

    Bye, Andreas

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by sjmp View Post
    ... my cpp file ...
    Huh? You're writing C++?
    My previous answer applies to C (I have no idea how struct and typedef behave in C++).
    Maybe you want to ask in the C++ Programming board.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    So i went back and did some reading but I am still confused on the use of typedef vs. tag, when do I need an Alias for the struct, and do not have a clue how to use a pointer to the struct.I cannot get the below to work. Any help would be appreciated.

    Code:
    #ifndef PRINT_ART
    #define PRINT
    #define STR_LEN 20
    
    struct Article{
        int number;
        int quantity;
        char description[STR_LEN+1];
    };
    
    void PRINT(struct Article art_1);
    {
        printf("Number:%d\n", art_1->number);
        printf("Quantity:%d\n", art_1->quantity);
        printf("Desciption:%s\n", art_1->description);
    }
    #endif
    
    
    Code:
     
    Code:
    #include <stdio.h>
    #include "header.h"
    
    int main()
    {
        struct Article art_1 ={56577,65,"Intro to Stuct"};
        struct Article art_2 ={56568,88,"Intro to Stuck"};
    
        PRINT (art_1);
    }
    




  6. #6
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by sjmp View Post
    ... I cannot get the below to work ...
    Why? What does it do that you weren't expecting? What does it not do that you were expecting? If the problem is when compiling: what are the errors/warnings the compiler tells you?

    ~~~~~~~~

    A few points:

    Move code out of the header and into the c file proper (the function PRINT). Leave a prototype in the header file.

    Effective header_guards use the same macro name in both the 1st and 2nd line (you have PRINT_ART and PRINT).

    Do not name your function with ALLUPPERCASE. Reserve ALLUPPERCASE to macros.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Let me try to take a step back and accomplish this in steps.
    Here is the HW -
    1. Create Article which the contents are assigned at initialization level
    2. Printing the Article is done w Print() function. Function get the address of structure as a parameter.'
    2a. suppose p is the pointer to the struct. fields can by printer by (*p).fieldname or p->fieldname.

    I got rid of the header file for now. Here is my code
    Code:
    #include <stdio.h>
    #define STR_LEN 20
    
    
    int main()
    {
    	struct Article{
    		int number;
    		int quantity;
    		char description[STR_LEN+1];
    		}; 
    
    
    	struct Article art_1 = {56577,65,"Intro to Stuct"};
    	
    	printf("Number: %d\n", art_1.number);
    	printf("Quantity: %d\n", art_1.quantity);
    	printf("Desciption: %s\n", art_1.description);
    }
    This works. So I did the structure. Did I assign the contents at initialization level? I think so since it was assigned in the same line I introduce art_1.

    I have to write a Print() function and get the address of the struct as a parameter. I have no idea what this means. And assumed it was talking about using a pointer.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Can someone please help me figure out what is wrong here. Not clear on how to define the struct name after declaring the struct itself. And void print does not like me using the keyword struct.

    here is the header file
    Code:
    #ifndef Print
    #define Print
    #define STR_LEN 20
    
    
    struct Article{
    		int number;
    		int quantity;
    		char description[STR_LEN+1];
    	};
    
    
    struct Article *art_1; // I have tried this many ways w/ the instance name and w/o neither work
    
    
    void Print(struct Article *art_1);
    {
    	printf("Number: %d\n", art_1->number);
    	printf("Quantity: %d\n", art_1->quantity);
    	printf("Desciption: %s\n", art_1->description);
    }
    
    
    #endif
    And the code is now
    Code:
    #include <stdio.h>
    #include "header.h"
    
    
    
    
    int main()
    {
    	struct Article art_1 = {56577,65,"Intro to Struct"};
    
    
    	Print (&art_1);
    	
    }

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You have a semicolon at the end of line 16: void Print(struct Article *art_1); <---

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    I added it based on an error in the compiler: This is what I get if I add it back. Without it I get unexpected type and missing function header
    1>header.h(11): error C2226: syntax error : unexpected type 'Article'
    1>header.h(12): error C2143: syntax error : missing ';' before '{'
    1>header.h(12): error C2447: '{' : missing function header (old-style formal list?)

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I notice that your header inclusion guard is:
    Code:
    #ifndef Print
    #define Print
    Yet you also want to have a function named Print. I suggest that you fully capitalise macro names, and then incorporate the file name into the inclusion guard name, e.g.,
    Code:
    #ifndef SJMP_PRINT_H_
    #define SJMP_PRINT_H_
    Next, why do you declare a pointer in your header file? Refer to:
    Code:
    struct Article *art_1;
    This does not just declare the pointer, it defines the pointer. Thus, if your header is included in more than one source file, this pointer would be defined more than once, which is an error. Do you even need this pointer to be global to begin with?

    Now, concerning your Print function: what you probably want to do is to declare the function in the header:
    Code:
    void Print(struct Article *art_1);
    Then define it in a source file, e.g., the same file that contains the definition of the main function, or another source file corresponding to this header file. The definition would look like:
    Code:
    void Print(struct Article *art_1)
    {
        printf("Number: %d\n", art_1->number);
        printf("Quantity: %d\n", art_1->quantity);
        printf("Desciption: %s\n", art_1->description);
    }
    jwroblewski44 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Thanks laserlight that was very helpful. I renamed the inclusion guard and removed
    Code:
    structArticle *art_1;
    completely. Now it works fine. I do not understand why I am using a pointer Article.art_1 can you please explain that?
    Thanks again, SJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some help needed with structures and pointer
    By Engineria in forum C Programming
    Replies: 11
    Last Post: 08-14-2012, 04:43 AM
  2. Pointer problems with structures
    By grifan526 in forum C Programming
    Replies: 3
    Last Post: 04-12-2010, 03:28 PM
  3. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 01:14 PM
  4. Question about pointer to structures
    By KidMan in forum C Programming
    Replies: 4
    Last Post: 08-23-2006, 07:11 PM
  5. accesing pointer structures
    By xenodvs1 in forum C Programming
    Replies: 3
    Last Post: 04-02-2003, 07:02 PM

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