Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

code in C( is it correct)

0 views
Skip to first unread message

Pallav singh

unread,
Jun 19, 2009, 5:07:32 AM6/19/09
to
Hi all ,

struct S
{
int a;
char c;
char * ptr;

};

struct T
{
int a;
char c;

};

int main()
{
struct S s1={1,'a',"India"};
struct S s2={2,'b',"Country"};

struct T t1={1,'a'};
struct T t2={2,'b'};

if( s1= s2) // Is it Correct? if yes/ no why
printf(" data copied successfully for struct S \n ");

if( t1= t2) // Is it Correct? if yes/ no why
printf(" data copied successfully for struct T \n");

return 0;
}

Keith Thompson

unread,
Jun 19, 2009, 5:35:14 AM6/19/09
to

Your question isn't about the C standard document, so it's more
appropriate for comp.lang.c. I've cross-posted and redirected
followups there.

There are two problems other than what you're asking about.
"int main()" should be "int main(void)" (though all, or nearly
all, compilers should accept both forms). And you're missing the
"#include <stdio.h>" that's required for printf.

Both if statements are invalid. The assignments are ok (though they
look like they may have been intended as comparisons, which sould be
invalid for structs), but since an assignment yields the assigned
value, which is a struct, you can't use them in if conditions.
(Your compiler should have told you this.)

Even for scalars, the result returned by an assignment is not an
indication of whether it succeeded or failed. There's no mechanism
for an assignment to indicate failure. It just works.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

0 new messages