Correct Typedef

P

Priya Mishra

What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier


typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName);

int main(int argc, char **argv )
{


int error;
structure *str;


error = func(&str, "abc.txt");


return error;
}

Thanks
Priya
 
S

Suman

Priya said:
What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier
Does that mean you are using VC++? Use a C compiler, will you, please?
#include said:
typedef struct
{
char file;
int a;
int b;
int c;



} structure;
A struct without a tag, and typedef'ing is okay on C compilers.
int func(structure *Ob,
char *fileName);

int main(int argc, char **argv )
Don't need the argc & argv if you are not going to use them, so
int main(void) works.
{


int error;
structure *str;
IMHO, you are trying to retrieve some information
about some file (assuming the function declaration is sane.)
Surely you need some memory allocated here?
Or, atleast a valid pointer.
error = func(&str, "abc.txt");
err: func takes pointer to struct, str, is a pointer to pointer to
struct.
 
S

Suman

Priya said:
What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier
FYI:
On copy-pasting the code in MSVC++ 6
I did not get such an error, what I got was some other error,
in your passing a wrong argument to func. So maybe you can explain
a bit more about whether this is the only piece of code you tried
compiling or not...
[snip]
 
S

Suman

Priya said:
What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier


typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName);

int main(int argc, char **argv )
{


int error;
structure *str;


error = func(&str, "abc.txt");


return error;
}

Thanks
Priya

If I am not too wrong, there was another thread with the exact same
content,
poster's name etc. Usenet is not instant coffee, give it some time
before recreating
a thread. It is possible that your thread gets lost, but that is a
rarety. I have already
replied to the other, and it is indeed irritating to find same content
elsethread.
 
M

Mehta Shailendrakumar

change
error = func(&str, "abc.txt");
to
error = func(str, "abc.txt");

it should work
 
N

Neeraj Gupta

Hi,

I could figure out two problems with the code:
1. You were trying to make a fn call without the function definition.
2. The fn call should be made with only "str" and NOT "&str"

This snippet should work:
#include <malloc.h>

typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName)
{
return 0;
}

int main(int argc, char **argv )
{


int error;
structure *str;
str = (structure *)malloc(sizeof(structure));

error = func(str, "abc.txt");


return error;

}
 
V

vsnadagouda

Hello Priya,

str is a pointer to "structure" type. If you want a pointer to this
poniter, then the receiving end should be a pointer to pointer to
"structure" type.(structure **Ob)

So your program looking like:

typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure **Ob,
char *fileName);

int main(int argc, char **argv )
{


int error;
structure *str;


error = func(&str, "abc.txt");


return error;



}

But, be aware of the uninitialized pointers, like 'str'.

Cheers
-Vallabha
 
S

Seven Kast USA

READ ANY ONE C BOOK .. .I T WILL EXPLAIN U ......DONT COME POST
MESSAGE IN USER NET
 
K

Ken

Hi,

All errors were due to the definition of structure. Try this:

typedef struct _structure
{
char file;
int a;
int b;
int c;
} structure;
 
N

noblesantosh

I just compiles this code on gcc and it works absolutly fine without
any errors, just few warning messeges. which compiler are you using?
 
N

noblesantosh

Priya, I don't know which compiler you are using to compile this code,
here I'm using gcc and it is not giving any errors only one warning
that is

17: warning: passing arg 1 of `func' from incompatible pointer type

The problem is in function call " error = func(&str, "abc.txt"); "
correct it to " error = func(str, "abc.txt"); " removing &.
Now it is working fine.

Santosh.
 
Y

Yohji

Priya said:
What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier


typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName);

int main(int argc, char **argv )
{


int error;
structure *str;


error = func(&str, "abc.txt");


return error;
}

Thanks
Priya

Er,wrong here:

error = func(&str, "abc.txt");

'str' is of structure * type,so '&str' is structure ** .But the
prototype of func is:

int func(structure *,
char *);
 
D

David Resnick

Priya said:
What is wrong in the below code,
I get the 2 error when i compile the prog... what is mean ??


error C2275: structure: illegal use of this type as an expression
error C2065: str: undeclared identifier


typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName);

int main(int argc, char **argv )
{


int error;
structure *str;


error = func(&str, "abc.txt");


return error;
}

Thanks
Priya

I'm wondering if your compiler uses structure as a keyword
or something odd. The only problem I see is that you have
a prototype for func taking a structure* and you are passing
it a structure**. Compiles fine for me (gcc) once that is
repaired and func is defined.

-David
 
N

Netocrat

VC++ is a C compiler if invoked correctly.


You might as well drop this because nothing in the code needs it.

You are one of the more knowledgeable experts of this group and your
resources are wasted on responding to trolls, which it pains me to say is
what you have been doing for the last five posts.

For those who still haven't clued on, in this thread to date the only
genuine posters have been myself, Lawrence Kirby, David Resnick (you would
have been better advised taking Kenny McCormack's advice) and Suman.

The troll posted and has since been merrily responding to itself under the
names: Priya Mishra, Yohji, (e-mail address removed), Ken, Seven Kast USA,
(e-mail address removed), Neeraj Gupta and Mehta Shailendrakumar.
 
V

vinodit205

This code gives no compile time error with gcc compiler.
It seems that the compiler u are using has reserved the keyword
'structure'. Change the name 'structure' and try it out.
 
L

Lawrence Kirby

I just compiles this code on gcc and it works absolutly fine without
any errors, just few warning messeges. which compiler are you using?

Warning messages are diagnostics just as much as error messages. The
comnpiler warned you that there is something wrong with the code, whether
it decides to continue compiling or not isn't particularly significant.

Lawrence
 
L

Lawrence Kirby

Priya, I don't know which compiler you are using to compile this code,
here I'm using gcc and it is not giving any errors only one warning
that is

17: warning: passing arg 1 of `func' from incompatible pointer type

The problem is in function call " error = func(&str, "abc.txt"); "
correct it to " error = func(str, "abc.txt"); " removing &.
Now it is working fine.

If you do that you'll be passing the value of an uninitialised pointer to
func() which is a particularly nasty bug. Better would be to change the
definition of str to a structure rather than a pointer to a structure.

Lawrence
 
L

Lawrence Kirby

Hi,

I could figure out two problems with the code:
1. You were trying to make a fn call without the function definition.
2. The fn call should be made with only "str" and NOT "&str"

This snippet should work:
#include <malloc.h>

C doesn't define a header called <malloc.h>. The correct header to include
for malloc() is said:
typedef struct
{
char file;
int a;
int b;
int c;



} structure;


int func(structure *Ob,
char *fileName)
{
return 0;
}

int main(int argc, char **argv )
{


int error;
structure *str;
str = (structure *)malloc(sizeof(structure));

Consider

structure *str = malloc(sizeof *str);
if (str == NULL) {
/* Handle allocation failure */
}

Lawrence
 
N

Netocrat

You are one of the more knowledgeable experts of this group and your
resources are wasted on responding to trolls, which it pains me to say is
what you have been doing for the last five posts.

For those who still haven't clued on, in this thread to date the only
genuine posters have been myself, Lawrence Kirby, David Resnick (you would
have been better advised taking Kenny McCormack's advice) and Suman.

Correction... Suman belongs in the troll list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top