Thread: Need help please to pass struct array to fucntion

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Need help please to pass struct array to fucntion

    Code:
    #include <stdio.h>
    	typedef struct
    	{
    		int x;
    		int y;
    	}POINT;
    
    
    	void display(POINT);
    
    
    	int main()
    	{
    		int i;
    		POINT p1[10];
    		for (i = 0; i < 10;i++)
    		{
    			p1[i].x =i*2;      // 
    			p1[i].y = i * 4;   //
    		}
    		display(p1);
    		
    	}
    	void display(POINT p[])
    	{
    		for (int i = 0; i < 10;i++)
    		printf("The coordinates of the point are: %d %d \n", p[i].x, p[i].y);
    	}
    display(p1); seems not working to pass to function display I tried p1[] not working any advise please thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > void display(POINT);
    > void display(POINT p[])

    Did you ignore errors and warnings when compiling? Because your declaration doesn't match the definition.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Quote Originally Posted by Prelude View Post
    > void display(POINT);
    > void display(POINT p[])

    Did you ignore errors and warnings when compiling? Because your declaration doesn't match the definition.
    Code:
    #include <stdio.h>
    typedef struct
    {
    	int x;
    	int y;
    }POINT;
    
    
    void display(POINT p[])
    {
    	for (int i = 0; i < 10; i++)
    		printf("The coordinates of the point are: %d %d \n", p[i].x, p[i].y);
    }
    
    
    
    
    int main()
    {
    	int i;
    	POINT p1[10];
    	for (i = 0; i < 10; i++)
    	{
    		p1[i].x = i * 2;      // 
    		p1[i].y = i * 4;   //
    	}
    	display(p1);
    
    
    }
    Yes my mistake, This format is working perfectly, How can I declare it in like first style writing? thank you

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by nuklon View Post
    How can I declare it in like first style writing?
    If you mean have the declaration above main and the declaration below, just copy the function header, paste it above main, and add a semi-colon.

    Code:
    #include <stdio.h>
    
    typedef struct
    {
        int x;
        int y;
    } POINT;
    
    void display(POINT p[]);
    
    int main()
    {
        int i;
        POINT p1[10];
    
        for (i = 0; i < 10; i++)
        {
            p1[i].x = i * 2;
            p1[i].y = i * 4;
        }
    
        display(p1);
    }
    
    void display(POINT p[])
    {
        for (int i = 0; i < 10; i++)
            printf("The coordinates of the point are: %d %d \n", p[i].x, p[i].y);
    }
    You should used named constants instead of the magic number 10.

    You should also pass the number of elements to your function along with the array, instead of hard-coding this value inside of the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-28-2012, 05:16 PM
  2. How to pass in an array/struct as argument
    By Tom_Arch in forum C Programming
    Replies: 11
    Last Post: 04-23-2009, 04:16 PM
  3. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  4. Dynamic Array of Object Fucntion call.
    By Spitball04 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2004, 10:40 AM
  5. Passing Class Array through Fucntion
    By Cris987 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2004, 07:17 PM

Tags for this Thread