Velocity Reviews > simple C question

# simple C question

Bill Cunningham
Guest
Posts: n/a

 11-05-2012
Adam Wysocki wrote:
> Bill Cunningham <(E-Mail Removed)> wrote:
>
>> SO I want
>>
>> for (i=1;i<100;i++) then.

>
> You want:
>
> for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)
>
> Or:
>
> for (i = 1; i < sizeof(a) / sizeof(*a); i++)

[snip]

Ok what's the difference in the two codes above? I am assuming that sizeof
(a) could be sizeof a right? This code confuses me a little. Divide this by
sizeof (a[0]) or a /dereference/ of a? eg. sizeof (*a) ?

Bill

Prathamesh Kulkarni
Guest
Posts: n/a

 11-05-2012
On Monday, 5 November 2012 14:35:50 UTC-5, Bill Cunningham wrote:
> Adam Wysocki wrote:
>
> > Bill Cunningham <(E-Mail Removed)> wrote:

>
> >

>
> >> SO I want

>
> >>

>
> >> for (i=1;i<100;i++) then.

>
> >

>
> > You want:

>
> >

>
> > for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)

>
> >

>
> > Or:

>
> >

>
> > for (i = 1; i < sizeof(a) / sizeof(*a); i++)

>
> [snip]
>
>
>
> Ok what's the difference in the two codes above? I am assuming that sizeof
>
> (a) could be sizeof a right? This code confuses me a little. Divide this by
>
> sizeof (a[0]) or a /dereference/ of a? eg. sizeof (*a) ?
>
>
>
> Bill

yes sizeof(a) and sizeof a are same.
sizeof(a) = sizeof(int) * entries in a
therefore,
entries in a = sizeof(a) / sizeof(int)
type of a[0] is int,
therefore we can replace sizeof(int) by sizeof(a[0])
entries in a = sizeof(a) / sizeof(a[0]);

the latter form is preferred because it is
type independent.

a[0] or *a mean the same thing.
for pointer p, and index i all the following
are equivalent:
p[index], index[p], *(p+index), *(index+p)

array decays into pointer in expression
except if the array is operand of sizeof operator or
& (address) operator.

Ben Bacarisse
Guest
Posts: n/a

 11-05-2012
"Bill Cunningham" <(E-Mail Removed)> writes:

> Adam Wysocki wrote:
>> Bill Cunningham <(E-Mail Removed)> wrote:
>>
>>> SO I want
>>>
>>> for (i=1;i<100;i++) then.

>>
>> You want:
>>
>> for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)
>>
>> Or:
>>
>> for (i = 1; i < sizeof(a) / sizeof(*a); i++)

> [snip]
>
> Ok what's the difference in the two codes above?

None, other than they look different.

> I am assuming that sizeof (a) could be sizeof a right?

Yes. All the ()s in these sizeof expressions are unnecessary. You can
write 'sizeof a[0]' or 'sizeof *a'.

> This code confuses me a little. Divide this by
> sizeof (a[0]) or a /dereference/ of a? eg. sizeof (*a) ?

Hard to say what's confusing you about this. 'a[0]' is a dereference of
'a' just as '*a' is. 'a[0]' is defined to mean '*(a + 0)' and, provided
'a' really is a valid point, 'a+0' is the same as 'a', so 'a[0]' has the
same meaning as '*a'.

--
Ben.

Ben Bacarisse
Guest
Posts: n/a

 11-05-2012
Ben Bacarisse <(E-Mail Removed)> writes:

> "Bill Cunningham" <(E-Mail Removed)> writes:
>
>> Adam Wysocki wrote:
>>> Bill Cunningham <(E-Mail Removed)> wrote:
>>>
>>>> SO I want
>>>>
>>>> for (i=1;i<100;i++) then.
>>>
>>> You want:
>>>
>>> for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)
>>>
>>> Or:
>>>
>>> for (i = 1; i < sizeof(a) / sizeof(*a); i++)

>> [snip]
>>
>> Ok what's the difference in the two codes above?

>
> None, other than they look different.
>
>> I am assuming that sizeof (a) could be sizeof a right?

>
> Yes. All the ()s in these sizeof expressions are unnecessary. You can
> write 'sizeof a[0]' or 'sizeof *a'.
>
>> This code confuses me a little. Divide this by
>> sizeof (a[0]) or a /dereference/ of a? eg. sizeof (*a) ?

>
> Hard to say what's confusing you about this. 'a[0]' is a dereference of
> 'a' just as '*a' is. 'a[0]' is defined to mean '*(a + 0)' and, provided
> 'a' really is a valid point, 'a+0' is the same as 'a', so 'a[0]' has the
> same meaning as '*a'.

The above is true for normal array accesses (and it may help you
understand why both sizeof expressions are the same) but neither of the
expression you quote involves a dereference. That's because the operand
of sizeof is not evaluated when VLAs are not involved.

Whilst it's useful to know that 'a[0]' means the same as '*a', all that
matters here is the type of the expression. You could even write
'sizeof a[-1000]'.

--
Ben.

Bill Cunningham
Guest
Posts: n/a

 11-05-2012
Barry Schwarz wrote:

> That is not even close to what you originally posted. Why don't you
> get your facts straight before generating drivel.

My apology.

B

Bill Cunningham
Guest
Posts: n/a

 11-05-2012
Barry Schwarz wrote:

[...]

> As your original post in this thread demonstrates, you have serious
> conceptual misunderstandings. Ignoring the difference between char
> and int will only serve to perpetuate many of them.

Yes. As I said I usually don't use a for loop to initialize arrays. I
manually do it. Simple C syntax trips me up. Non use after awhile I forget
things too. From now on I will replace the null character with 0 when not
working with chars since with ints 0 and \0 are the same anyway. Something
else is shortcuts from experts. Their vary technical code confuses me and
I've never dealt with pointers to pointers in coding my own code. But I
should find some tutorials online before coming to clc.

Bill

Bill Cunningham
Guest
Posts: n/a

 11-05-2012
Ben Bacarisse wrote:
> Ben Bacarisse <(E-Mail Removed)> writes:
>
>> "Bill Cunningham" <(E-Mail Removed)> writes:
>>
>>> Adam Wysocki wrote:
>>>> Bill Cunningham <(E-Mail Removed)> wrote:
>>>>
>>>>> SO I want
>>>>>
>>>>> for (i=1;i<100;i++) then.
>>>>
>>>> You want:
>>>>
>>>> for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)
>>>>
>>>> Or:
>>>>
>>>> for (i = 1; i < sizeof(a) / sizeof(*a); i++)
>>> [snip]
>>>
>>> Ok what's the difference in the two codes above?

>>
>> None, other than they look different.
>>
>>> I am assuming that sizeof (a) could be sizeof a right?

>>
>> Yes. All the ()s in these sizeof expressions are unnecessary. You
>> can write 'sizeof a[0]' or 'sizeof *a'.
>>
>>> This code confuses me a little. Divide this by
>>> sizeof (a[0]) or a /dereference/ of a? eg. sizeof (*a) ?

>>
>> Hard to say what's confusing you about this. 'a[0]' is a
>> dereference of 'a' just as '*a' is.

ok

'a[0]' is defined to mean '*(a
>> + 0)' and, provided 'a' really is a valid point, 'a+0' is the same
>> as 'a', so 'a[0]' has the same meaning as '*a'.

>
> The above is true for normal array accesses (and it may help you
> understand why both sizeof expressions are the same) but neither of
> the expression you quote involves a dereference. That's because the
> operand of sizeof is not evaluated when VLAs are not involved.
>
> Whilst it's useful to know that 'a[0]' means the same as '*a', all
> that matters here is the type of the expression. You could even write
> 'sizeof a[-1000]'.

OK.

I'm going to remember what you said here. What I understand of it and I
think I'm grasping it. I've heard array decay into pointers but I've never
know what that meant. I remember from kandr2 iterating in this manner:

*(a+1) *(a+2)

or the like. After using the code for awhile more will click. As the book
says "C wears well the more you use it" or something like that.

Bill

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post MeekiMoo Computer Support 0 07-28-2009 08:10 AM joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM Daniel Frey XML 4 01-12-2005 04:25 PM Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM

Advertisments