Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > simple C question

Reply
Thread Tools

simple C question

 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-05-2012
On Sun, 4 Nov 2012 17:09:49 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Prathamesh Kulkarni wrote:
>> int main(void)
>> {
>> int a[100], i;
>>
>> for (i = 0; i < 100; i++)
>> a[i] = i;
>> return 0;
>> }

>
>lol <chuckle> I see that's right. No tutorial I saw said this. It was
>i=a[i]; or just iterating over an array without doing anything. lvaules.
>Thanks much.


Please post the URL of the tutorial that said to use i = a[i]. We
need to insure that new C programmers are warned to stay away from it.

--
Remove del for email
 
Reply With Quote
 
 
 
 
tom st denis
Guest
Posts: n/a
 
      11-05-2012
On Nov 4, 6:19*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Ian Collins wrote:
> > On 11/05/12 12:05, Bill Cunningham wrote:
> >> Ian Collins wrote:
> >>> On 11/05/12 11:50, Bill Cunningham wrote:
> >>>> Ian Collins wrote:
> >>>>> On 11/05/12 11:39, Bill Cunningham wrote:

>
> >>>>>> #include<stdio.h>
> >>>>>> #include<string.h>

>
> >>>>> Why?

>
> >>>>>> int main(void)
> >>>>>> {
> >>>>>> * * * *int a[100] = { '\0' };

>
> >>>> * * * To zero out the whole array.

>
> >>>>> Why '\0'?

>
> >>>> * * * I guess with ints it means the same as zero.

>
> >>> Then why not use 0?

>
> >> * * *To get used to using the null character for when working with
> >> chars.

>
> > So do you practice dog training on your cat?

>
> >>>>>> * * * *int i = 1;

>
> >>>>> Why 1?

>
> >>>> * * * I want to index to 1.

>
> >>> To 1?

>
> >> * * *instead of 0 to 99 1 to 100.

>
> > Given your array declaration, what is the last valid entry in your
> > array?
> >>>>>> * * * *for (; i< * *sizeof a; i++)

>
> >>>>> What is sizeof a?

>
> >>>> An array called a of 100 elements. I guess I could've used.

>
> >>>> sizeof (int)*100;

>
> >>> Any what is the value of that expression?

>
> >> Huh. Well I guess 100 instances of however the sizeof ints are on my
> >> machine. I'm running a 64 bit OS.

>
> > So the value is?

>
> * * Not sure. Probably 16 to 32 bytes.
>
> > Is that a valid index to your array?

>
> I don't know for sure that one. Maybe this is not a case for sizeof.
>
> Bill


And yet more value-add conversation provided by Bill.

Thanks.
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-05-2012
Barry Schwarz wrote:

> Please post the URL of the tutorial that said to use i = a[i]. We
> need to insure that new C programmers are warned to stay away from it.


I think we might be good on that Barry I meany a[i]. Not i=a[i]. LIke
you would iterate over an array.

int i, j;
for (i=0;i<100;i++)
printf("%d\n",a[i]);
return 0;

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-05-2012
Prathamesh Kulkarni wrote:
> On Sunday, 4 November 2012 18:09:30 UTC-5, Bill Cunningham wrote:
>> Prathamesh Kulkarni wrote:
>>
>>
>>
>>> why do you want to initalize the array with zeroes when you are

>>
>>> filling it in the loop ?

>>
>>
>>
>> As habit I guess. It's better than manual initialization with 100
>>
>> elements.

>
> No, I mean when you are going to fill
> the entire array in the loop, there's
> no point with array initialization
>
>>
>>
>>
>>> you are segfaulting because of the loop. you want to loop till i <

>>
>>> 100 and the for loop terminates when i become sizeof(int) * 100

>>
>>
>>
>> SO I want
>>
>>
>>
>> for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?
>>
>>
>>
>> Bill

>
> no, the '\0' does not mess up anything,
> although 0 would be a better practice.


You think 0 is better practice. I use the Null Character to get into the
habit so if I'm using chars I don't use 0 but \0

> It's the loop, it should be written as:
> for (i = 0; i < 100; i++)
> a[i] = i;
> in the condition,
> i < sizeof a, you would also be using indexes
> that are out-of-bounds, hence the seg-fault



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-05-2012
On 11/05/12 15:24, Bill Cunningham wrote:
> Prathamesh Kulkarni wrote:
>>
>> no, the '\0' does not mess up anything,
>> although 0 would be a better practice.

>
> You think 0 is better practice. I use the Null Character to get into the
> habit so if I'm using chars I don't use 0 but \0


You didn't answer my question about dog training.

--
Ian Collins
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-05-2012
tom st denis wrote:

> And yet more value-add conversation provided by Bill.
>
> Thanks.


The only time you ever show up in a thread is to complain, be a wise ass
and you never contribute anything. In other threads you have talked about
how great you were at programming when you were 8 to make fun of a new CS
student. I am not feeding the trolls anymore

*plonk*


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-05-2012
Ian Collins wrote:

> You didn't answer my question about dog training.


Yes. I train my cat dog tricks. He likes paper balls and will chase them
and bring them back. I didn't think you'd like the answer but he plays
doglike and I'm being serious.

Bill


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-05-2012
On Sun, 4 Nov 2012 21:22:13 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote:
>
>> Please post the URL of the tutorial that said to use i = a[i]. We
>> need to insure that new C programmers are warned to stay away from it.

>
> I think we might be good on that Barry I meany a[i]. Not i=a[i]. LIke
>you would iterate over an array.
>
>int i, j;
>for (i=0;i<100;i++)
> printf("%d\n",a[i]);
>return 0;


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

--
Remove del for email
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-05-2012
On Sun, 4 Nov 2012 21:24:06 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Prathamesh Kulkarni wrote:
>> On Sunday, 4 November 2012 18:09:30 UTC-5, Bill Cunningham wrote:
>>> Prathamesh Kulkarni wrote:
>>>
>>>> why do you want to initalize the array with zeroes when you are
>>>> filling it in the loop ?
>>>
>>> As habit I guess. It's better than manual initialization with 100
>>> elements.

>>
>> No, I mean when you are going to fill
>> the entire array in the loop, there's
>> no point with array initialization
>>
>>>> you are segfaulting because of the loop. you want to loop till i <
>>>> 100 and the for loop terminates when i become sizeof(int) * 100
>>>
>>> SO I want
>>>
>>> for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?
>>>
>>> Bill

>>
>> no, the '\0' does not mess up anything,
>> although 0 would be a better practice.

>
> You think 0 is better practice. I use the Null Character to get into the
>habit so if I'm using chars I don't use 0 but \0


Trying to code from habit is about the worst thing you can do. If you
are dealing with an array of int, you should use integer values. If
you are dealing with an array of char, you should use character
values. The fact that either will work in many cases is irrelevant.
You should strive to make the code easy to read and eliminate possible
misleading constructs.

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.

--
Remove del for email
 
Reply With Quote
 
Adam Wysocki
Guest
Posts: n/a
 
      11-05-2012
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++)

Or:

#define countof(array) (sizeof(array) / sizeof(array[0]))

for (i = 1; i < countof(a); i++)

--
Gof
http://www.chmurka.net/
 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments