473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1607
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the...
4
by: D Witherspoon | last post by:
I have a Structure I have created and am using it as a Public Property of a class. Here is the property. ------------------------------------------------------ Dim _MyID As SInteger Public...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
15
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
2
by: Suzie | last post by:
I'm a newbie with Vb.NET so please excuse me if this is a stupid quetion. I have declared a structure: Structure ClientRecord Public Client_ID As Integer Public Client_Initials As String...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
5
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
26
by: Vashna | last post by:
Hi Group, I have a doubt about register variables. I know that if we have a variable used very frequently in a function, then provided we never apply the & function to it, we can define it as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.