ans and explain

R

raj shekar

a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an array?
 
J

James Kuyper

a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an array?

I presume that there's an arithmetic type missing from the start of that
declaration?

Unless I'm missing something, that appears to have a syntax error. If
the '.' were replaced with a ',', the "[0]=9" part would be an example
of a designated initializer.

When you provide an initializer for an array, the length of the array
need not be specified; it is set based upon the number of elements in
the initializer.

The declaration

int a[] = {2, 5, 3, 6, [0]=9, 4};

is a shortcut that's equivalent to the following:

int a[5] = {2, 5, 3, 6, 4};
a[0] = 9;

As such, it's pretty pointless, dropping the designated initializer, and
replacing the '2' with a '9', would have the same effect. designate
initializers are mainly useful for sparse arrays, where you rely upon
the default zero-initialization to set the other elements:

int array[100] = {[42] = 100};
 
R

raj shekar

a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an array?



I presume that there's an arithmetic type missing from the start of that

declaration?
Unless I'm missing something, that appears to have a syntax error. If

the '.' were replaced with a ',', the "[0]=9" part would be an example

of a designated initializer.



When you provide an initializer for an array, the length of the array

need not be specified; it is set based upon the number of elements in

the initializer.



The declaration



int a[] = {2, 5, 3, 6, [0]=9, 4};



is a shortcut that's equivalent to the following:



int a[5] = {2, 5, 3, 6, 4};

a[0] = 9;



As such, it's pretty pointless, dropping the designated initializer, and

replacing the '2' with a '9', would have the same effect. designate

initializers are mainly useful for sparse arrays, where you rely upon

the default zero-initialization to set the other elements:



int array[100] = {[42] = 100};





if initializer is present lenth of the array may be omitted

and

u know about c99 designated intializer
 
I

Ike Naar

a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an array?

I presume that there's an arithmetic type missing from the start of that
declaration?

Unless I'm missing something, that appears to have a syntax error. If
the '.' were replaced with a ',', the "[0]=9" part would be an example
of a designated initializer.

When you provide an initializer for an array, the length of the array
need not be specified; it is set based upon the number of elements in
the initializer.

The declaration

int a[] = {2, 5, 3, 6, [0]=9, 4};

is a shortcut that's equivalent to the following:

int a[5] = {2, 5, 3, 6, 4};
a[0] = 9;

Is this correct? Doesn't it set a[0] to 2, then a[1] to 5,
then a[2] to 3, then a[3] to 6, then a[0] to 9, then a[1] to 4,
resulting in an array of four elements containing [9,4,3,6]?
 
J

James Kuyper

The declaration

int a[] = {2, 5, 3, 6, [0]=9, 4};

is a shortcut that's equivalent to the following:

int a[5] = {2, 5, 3, 6, 4};
a[0] = 9;

Is this correct? Doesn't it set a[0] to 2, then a[1] to 5,
then a[2] to 3, then a[3] to 6, then a[0] to 9, then a[1] to 4,
resulting in an array of four elements containing [9,4,3,6]?

You're right - that's what it says in 6.7.9p17. I haven't been allowed
to use this feature, so I'd forgotten about that aspect of it.
 
B

Ben Bacarisse

Ike Naar said:
a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an array?

I presume that there's an arithmetic type missing from the start of that
declaration?

Unless I'm missing something, that appears to have a syntax error. If
the '.' were replaced with a ',', the "[0]=9" part would be an example
of a designated initializer.

When you provide an initializer for an array, the length of the array
need not be specified; it is set based upon the number of elements in
the initializer.

The declaration

int a[] = {2, 5, 3, 6, [0]=9, 4};

is a shortcut that's equivalent to the following:

int a[5] = {2, 5, 3, 6, 4};
a[0] = 9;

Is this correct? Doesn't it set a[0] to 2, then a[1] to 5,
then a[2] to 3, then a[3] to 6, then a[0] to 9, then a[1] to 4,
resulting in an array of four elements containing [9,4,3,6]?

Yes, that's right. The standard talks about a "current object", much
like a pointer that moves through the aggregate. Elements with no
designation initialise the current object and move it on the next
element or field. A designated initialiser sets the current object to
the designated element.
 
K

Kenny McCormack

a[]={2,5,3,6.[0] = 9,4} what are a[0],a[1] ? and what is the lenth of an
array?

So you don't know jack shit; but you sure have opinions!

Pretty much like everyone else here.

--
Both the leader of the Mormon Church and the leader of the Catholic
church claim infallibility. Is it any surprise that these two orgs
revile each other? Anybody with any sense knows that 80-yr old codgers
are hardly infallible. Some codgers this age do well to find the crapper
in time and remember to zip-up.
 
B

Bill Cunningham

int array[100] = {[42] = 100};

Yes he wants array element 42 to contain the value 100. That is kind of
what I was thinking. Personally I would've put the value in there via
pointer notation but that's a personal preference. Or would try anyway.

Bill
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top