structure defined inside parms!
Hi!Could you tell me why I get this message: warning: structure defined inside parms,when I run the program??? :blush:
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;
}
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.:confused:
As this is a c/c++ question, it would be more appropriate to post it to the c++ (non-visual c++) forum.
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???
Re: structure defined inside parms!
Quote:
Originally Posted by
mathmari
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.
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??? :(
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.
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!!!!!! :thumb: