simple C question

  • Thread starter Bill Cunningham
  • Start date
B

Barry Schwarz

Prathamesh said:
int main(void)
{
int a[100], i;

for (i = 0; i < 100; i++)
a = i;
return 0;
}


lol <chuckle> I see that's right. No tutorial I saw said this. It was
i=a; 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. We
need to insure that new C programmers are warned to stay away from it.
 
T

tom st denis

Ian said:
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?
Given your array declaration, what is the last valid entry in your
array?
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.
 
B

Bill Cunningham

Barry said:
Please post the URL of the tutorial that said to use i = a. 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. Not i=a. LIke
you would iterate over an array.

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

Bill
 
B

Bill Cunningham

Prathamesh said:
No, I mean when you are going to fill
the entire array in the loop, there's
no point with array initialization


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;
in the condition,
i < sizeof a, you would also be using indexes
that are out-of-bounds, hence the seg-fault
 
I

Ian Collins

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.
 
B

Bill Cunningham

tom said:
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*
 
B

Bill Cunningham

Ian said:
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
 
B

Barry Schwarz

Barry said:
Please post the URL of the tutorial that said to use i = a. 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. Not i=a. LIke
you would iterate over an array.

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


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

Barry Schwarz

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.
 
A

Adam Wysocki

Bill Cunningham said:
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++)
 
B

Bill Cunningham

Adam said:
Bill Cunningham said:
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
 
P

Prathamesh Kulkarni

Adam said:
SO I want

for (i=1;i<100;i++) then.
You want:
for (i = 1; i < sizeof(a) / sizeof(a[0]); i++)

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.
 
B

Ben Bacarisse

Bill Cunningham said:
Adam said:
Bill Cunningham said:
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'.
 
B

Ben Bacarisse

Ben Bacarisse said:
Bill Cunningham said:
Adam said:
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]'.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

Ben said:
Ben Bacarisse said:
Bill Cunningham said:
Adam Wysocki 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
 

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,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top