468,290 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

doubt structure assignment

Hi ,
When i read linux tcp/ip stack codes i come cross lot of structure
assignment like this.
I guess '=' is the assignment operator and is there any particular
reason for using ':' as the assignment operator.A small snippet as
shown below.

#include <stdio.h>

int main()
{
struct tst {
int a;
char b;
};
struct tst t = {
a: 5,
b: 'm',
};
printf("The value of a:%d",t.a);
}

thanks,
Krishna

Nov 21 '06 #1
6 1419
krishna wrote:
Hi ,
When i read linux tcp/ip stack codes i come cross lot of structure
assignment like this.
I guess '=' is the assignment operator and is there any particular
reason for using ':' as the assignment operator.A small snippet as
shown below.

#include <stdio.h>

int main()
{
struct tst {
int a;
char b;
};
struct tst t = {
a: 5,
b: 'm',
};
printf("The value of a:%d",t.a);
}
I believe these are an archaic form of assignment. They are
non-standard, maybe a gcc extension?

Or it could be JavaScript!

--
Ian Collins.
Nov 21 '06 #2
"krishna" <kr************@gmail.comwrites:
When i read linux tcp/ip stack codes i come cross lot of structure
assignment like this.
I guess '=' is the assignment operator and is there any particular
reason for using ':' as the assignment operator.A small snippet as
shown below.

#include <stdio.h>

int main()
{
struct tst {
int a;
char b;
};
struct tst t = {
a: 5,
b: 'm',
};
printf("The value of a:%d",t.a);
}
It's an obsolete gcc-specific form of designated initializer; the gcc
documentation says it's been obsolete since gcc 2.5.

The C99 equivalent is this:

#include <stdio.h>

int main(void)
{
struct tst {
int a;
char b;
};
struct tst t = {
.a = 5,
.b = 'm'
};
printf("The value of a:%d\n", t.a);
return 0;
}

(I also changed "int main()" to "int main(void)", added a "return 0",
added a newline to the printf, and increased the indentation.)

C90 doesn't support designated initializers, but you can still do
this:

#include <stdio.h>

int main(void)
{
struct tst {
int a;
char b;
};
struct tst t = { 5, 'm' };
printf("The value of a:%d\n", t.a);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '06 #3

Keith Thompson wrote:
"krishna" <kr************@gmail.comwrites:
When i read linux tcp/ip stack codes i come cross lot of structure
assignment like this.
I guess '=' is the assignment operator and is there any particular
reason for using ':' as the assignment operator.A small snippet as
shown below.

#include <stdio.h>

int main()
{
struct tst {
int a;
char b;
};
struct tst t = {
a: 5,
b: 'm',
};
printf("The value of a:%d",t.a);
}

It's an obsolete gcc-specific form of designated initializer; the gcc
documentation says it's been obsolete since gcc 2.5.

The C99 equivalent is this:

#include <stdio.h>

int main(void)
{
struct tst {
int a;
char b;
};
struct tst t = {
.a = 5,
.b = 'm'
};
printf("The value of a:%d\n", t.a);
return 0;
}

(I also changed "int main()" to "int main(void)", added a "return 0",
added a newline to the printf, and increased the indentation.)

C90 doesn't support designated initializers, but you can still do
this:

#include <stdio.h>

int main(void)
{
struct tst {
int a;
char b;
};
struct tst t = { 5, 'm' };
printf("The value of a:%d\n", t.a);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

thanks keith for the clarification.

Nov 21 '06 #4
"krishna" <kr************@gmail.comwrites:
Keith Thompson wrote:
[67 lines deleted]
>
thanks keith for the clarification.
You're welcome.

For future reference, it's important to provide context when you post
a followup, but it's seldom necessary to quote the entire article.
Just quote whatever is necessary for your followup to make sense.
Don't quote signatures unless they're actually relevant to your
followup.

The idea is to make each followup make sense when read by itself
(since readers may not have seen the previous article), but without
making readers wade through large amounts of text to get to the
relevant material.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '06 #5
Hai Keith,
Check this code.
int main (){
struct tst {
int a;
char b;
};
struct tst t={b: 'm',a: 5};
printf("The value of a:%d %c",t.a,t.b);
return 0;
}

I think these designated intializers will be mainly useful in
intializing values in different order. r there any other uses of these?
I checked ur other version also i.e
struct t={.b='m',.a=5};
this also works fine. Try removing the = sign in {.b='m',.a=5}. Even
then it works. I am unable to understand how?
Result is same as other code snippets.

Reagrds,
Vamshi.

On Nov 22, 12:29 am, Keith Thompson <k...@mib.orgwrote:
"krishna" <krishnavija...@gmail.comwrites:
Keith Thompson wrote:
[67 lines deleted]
thanks keith for the clarification.You're welcome.

For future reference, it's important to provide context when you post
a followup, but it's seldom necessary to quote the entire article.
Just quote whatever is necessary for your followup to make sense.
Don't quote signatures unless they're actually relevant to your
followup.

The idea is to make each followup make sense when read by itself
(since readers may not have seen the previous article), but without
making readers wade through large amounts of text to get to the
relevant material.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 22 '06 #6
vamshi wrote:

***** DON'T TOP POST *****
Please see the links given below:

(Taken from one of CBFalconer's sigs)
Some informative links:
<news:news.announce.newusers>
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
On Nov 22, 12:29 am, Keith Thompson <k...@mib.orgwrote:
"krishna" <krishnavija...@gmail.comwrites:
Keith Thompson wrote:
[67 lines deleted]
thanks keith for the clarification.You're welcome.
For future reference, it's important to provide context when you post
a followup, but it's seldom necessary to quote the entire article.
<snip>
Hai Keith,
Unless Keith Thompson happens to be your best buddy, try to be more
formal. This is not your private group or friends list. This is a
technical Usenet group. Unless neccessary, don't address participants
personally, since this is a public forum, and anything you post is
subject to response from anyone else.

Check this code.
Unless you learn more civility, you'll find yourself being ignored or
flamed. Since everyone here participates voluntarily, you should ask of
help in a tad more polite manner than you've done in this post.

<snip code>
I think these designated intializers will be mainly useful in
intializing values in different order. r there any other uses of these?
I checked ur other version also i.e
Also avoid SMS-style abbreviations like 'u', 'r' etc.
I am unable to understand how?
I suggest concentrating on standard C before getting bogged down in
obsolete, implementation specific details.
--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Finally don't quote the sig unless you're commenting upon it.

Nov 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

138 posts views Thread by ambika | last post: by
15 posts views Thread by Sourcerer | last post: by
5 posts views Thread by subramanian | last post: by
8 posts views Thread by somenath | last post: by
26 posts views Thread by Vashna | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.