structure defined inside parms!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: structure defined inside parms!

  1. #1
    Join Date
    Apr 2013
    Posts
    45

    structure defined inside parms!

    Hi!Could you tell me why I get this message: warning: structure defined inside parms,when I run the program???
    Code:
    #include <stdio.h>
    
    struct test
    {
    	char x;
    	int y;
    	float z;
    	char lex[25];
    };
    
    void function(p)
         struct
         {
    	     char c;
    	     int i;
    	     float d;
    	     char c1;
    	     char c2;
    	     char c3;
    	     char str1[12];
    	     char str2[10];
         }p;
         {
    	     printf("%c\n",p.c);
    	     printf("%d\n",p.i);
    	     printf("%f\n",p.d);
    	     printf("%c\n",p.c1);
    	     printf("%c\n",p.c2);
    	     printf("%c\n",p.c3);
    	     printf("%s\n",p.str1);
    	     printf("%s\n",p.str2);
         }
         
         int main()
         {
    	     struct test f;
    	     f.x='A';
    	     f.y=120;
    	     f.z=3.14159;
    	     strcpy(f.lex,"Hello");
    	     function(f);
    	     return 0;
         }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,451

    Re: structure defined inside parms!

    You are trying to use the old k&r style function parameters - which are no longer used! However, you are passing to function a parameter of type struct test, so function must have a parameter of this same type. Try this

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct test
    {
    	char x;
    	int y;
    	float z;
    	char lex[25];
    };
    
    void function(struct test p)
    {
    	printf("%c\n",p.x);
    	printf("%d\n",p.y);
    	printf("%f\n",p.z);
    	printf("%s\n",p.lex);
    }
         
    int main()
    {
    struct test f;
    
    	f.x = 'A';
    	f.y = 120;
    	f.z = 3.14159f;
    	strcpy(f.lex, "Hello");
    	function(f);
    	return 0;
    }
    Are you leaning c or c++? How are you leaning c/c++? As no modern textbook would teach you to use the k&r style function parameters.

    As this is a c/c++ question, it would be more appropriate to post it to the c++ (non-visual c++) forum.
    Last edited by 2kaud; June 10th, 2013 at 06:22 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    45

    Re: structure defined inside parms!

    I am learning c and my teacher taught me,that,besides that way you sent me,I can also use the the k&r style...So is this method wrong???

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,451

    Re: structure defined inside parms!

    Quote Originally Posted by mathmari View Post
    I am learning c and my teacher taught me,that,besides that way you sent me,I can also use the the k&r style...So is this method wrong???
    Not exactly wrong, but not used and not advised.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2013
    Posts
    45

    Re: structure defined inside parms!

    And if I am asked to use the k&r style,could you tell me how I can get rid of the error???

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,451

    Re: structure defined inside parms!

    It's just a warning - not an error - the program will still compile. What compiler are you using as on my MSVS it compiles with no warning/error?

    With your structure p you are trying to split a char[25] into 3 chars, a char[12] and a char[10]. Usually this would be done using a union within the struct so the struct is only defined once rather than having to be re-defined the way you are.

    Code:
    #include <stdio.h>
    
    struct test
    {
    	char x;
    	int y;
    	float z;
    	union {
    		char lex[25];
    		struct {
    			char c1;
    			char c2;
    			char c3;
    			char str1[12];
    			char str2[10];
    		} strstruct;
    	} string;
    };
    
    void function(p)
         struct test p;
    {
    	printf("%c\n",p.x);
    	printf("%d\n",p.y);
    	printf("%f\n",p.z);
    	printf("%c\n",p.string.strstruct.c1);
    	printf("%c\n",p.string.strstruct.c2);
    	printf("%c\n",p.string.strstruct.c3);
    	printf("%s\n",p.string.strstruct.str1);
    	printf("%s\n",p.string.strstruct.str2);
    }
         
    int main()
    {
    struct test f;
    
    	f.x = 'A';
    	f.y = 120;
    	f.z = 3.14159f;
    	strcpy(f.string.lex, "Hello");
    	function(f);
    	return 0;
    }
    This compiles cleanly with my MSVS using k&r style function parameter.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2013
    Posts
    45

    Re: structure defined inside parms!

    Oh my sorry...!!!I saw the warning and thought the program couldn't be compiled!My compiler,cygwin,compiles it too without error...Thank you very much!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center