for and arrays

B

Bill Cunningham

Ben Bacarisse said:
He said it worked so presumably if he'd looked he'd see what he expects
to see. For example, in gdb it shows:

(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.

...And you are your best debugger. I guess I am in that camp. However,
if Richard is talking about file sectors and segments and the internals of
files like symbol tables and headers I might just be interested in learning
a little more about gdb.

Bill
 
R

Richard

Bill Cunningham said:
Ben Bacarisse said:
He said it worked so presumably if he'd looked he'd see what he expects
to see. For example, in gdb it shows:

(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.

...And you are your best debugger. I guess I am in that camp. However,
if Richard is talking about file sectors and segments and the internals of
files like symbol tables and headers I might just be interested in learning
a little more about gdb.

Bill

LOL. Yeah Bill. That's what I'm talking about. Hint : I am not. I am
talking about bog standard strings or character arrays.

Go away. Read the tutorial and LEARN how to do things yourself for a
while. And stop moving on to things you are clearly clueless about until
you understand the basics of examining data and, at the very least, the
C string "type".
 
B

Ben Bacarisse

Richard said:
I can not make up my mind. I have given him lots of good advice. Until
he tries to follow some then I think a good laugh is in order...

That is up to up.

Why are you telling me what it looks like?

Because you seem to think it might help. I wanted to show that you are
wrong in this case.
I know what it looks like. He
needs to know what it looks like. The thing you have to remember with
Bill is that he heasn't a clue what "works" means since half the time he
doesnt appear to know what it is he wants to work.

The debugger will show the same as the execution run which he says
worked. Only a cleverer C debugger than I have yet used will show him
that anything is wrong (if he continues to be unlucky in his data's
layout).
It is up to HIM to use the tools. Not for others to tell him what the
tools will tell him.

It will not help to suggest a tool that can't help in this case.
(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.

To see the data is a major part of understanding that the code has
done. If he is serious that he doesn't know how to put spaces in then he
says some serious basic issues. And a debugger can show, down and dirty,
just what he is dealing with.

Which is of course correct (because he was unlucky) so the debugger
can do little but show him that all is as he thought it should be.
 
D

Default User

Ben said:
I don't understand this. If you think Bill is clever fiction, why are
you replying with serious suggestions (see below)? If you think he is
genuinely struggling to learn, then why laugh at his attempts. It
seems cruel.

Riley is a troll. We've been over this.




Brian
 
R

Richard

Ben Bacarisse said:
That is up to up.



Because you seem to think it might help. I wanted to show that you are
wrong in this case.

No. I am not wrong in this case. It is always more than the issue at
hand.

The issue is for Bill to understand the data. Nothing more. Nothing
less. We keep hearing "I dont know spaces", "I dont know string" etc
etc. Its tiresome. He needs to do some spadework.
 
R

Richard

Default User said:
Riley is a troll. We've been over this.

You have your people mixed up. I would like to point out that your
contributions appear to be nothing more than net nannying and
whinging. I am deadly serious with my advice to Bill. Learn to use the
tools to help you understand the basics. It is quite clear he will not
understand stuff from the "standard" for example. All your blathering
and pouting will get him no where. And at least Ben helps and gets stuck
in so a discussion with him on the best approach is not a waste of
time. What do you do? Now, if you have anything positive to contribute,
please do, otherwise go and put your big nose somewhere where it might
be appreciated. Thank you.
 
M

Mark L Pappin

Bill Cunningham said:
I don't know how to do this with spaces or for.

OK, it appears that you've not "got it" as I had hoped you might, so
here are the underlying ideas you need to move from "print a thing
that's hard-coded in the source" to "print a sequence of things that
can be computed".

- an 'int' variable can hold values from 0 up to INT_MAX (and down to
INT_MIN, but that's not important right now), where INT_MAX is at
least 32767
- 'printf()' can print the value of an 'int' variable, as well as a
literal string
- a 'for' loop can initialize a variable, loop around "doing stuff"
until that variable hits an upper limit, and increment the variable
after each time it has "done stuff"

(There are other concepts that could be used to implement this, like
'unsigned int', 'if', etc. They are also Not Important Right Now.)


Since you appear to need it, here's a template for you. Replace the
comments with source that does what's suggested.

#include <stdio.h>
int main(void) {

/* define an int variable called i */

for (/* set i to the value 0 */ ;
/* test to see that i is still less than 7 */ ;
/* increment i*/) {

/* print the value of i as a decimal integer */
/* print a space */

}

/* print a newline, since you only need one of them */

return 0;
}


The "a-ha!" moment I'm hoping you'll have here is the realisation that
the program does not need to have _everything_ laid out explicitly for
it, but instead its behaviour can be described at a higher level. For
instance, if question 3 was changed to ask for all the integers from 0
to 100 as output, then your first answer would need to have the string
extended by around 300 characters, but the above program would merely
need the test changed from "is i less than 7?" to "is i less than
101?". In the real world, specifications _do_ change like this, so
having a tool to make handling the change easy is an advantage.

So I will post another way of doing it and I don't know how to
include spaces. But the program works.

It only "works" because you were unlucky. Ben has given you an
example of a place where it doesn't "work", but you obviously don't
understand why.
#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};

The line above creates an array of 7 characters only. What sits after
those characters in memory could be anything.
printf("%s\n",a);

The "%s" format specifier must be given a string.
A string is a sequence of characters ending with a '\0'.
The array whose starting address you have passed is not a string.
The standard does not define what happens now.

In your case there just happened to be a '\0' sitting in the chunk of
memory immediately after 'a[]'; in Ben's there wasn't.


For now, don't think about using array notation until I suggest that
it might be handy for solving a particular problem.


mlp
 
B

Ben Bacarisse

Riley is a troll.

For the record, I don't use the term myself. It is too easy to throw
around and too hard to decide in all the cases where it matters.
We've been over this.

If you mean by this that you've given me your opinion before, then I
take your word for it. If you mean that most people here are of that
opinion, then I agree, and that is in part why I posted. If anyone
else had chosen to point out how a debugger is unlikely to help in
this case, I would not have felt the need.

I have found "Plain Richard" to be someone that I do not want to argue
with. He does not try to understand other people's positions and
will, in the end, resort to claiming that he just does not believe his
opponents. That does not mean, to me, that everything he writes
should go unchallenged. Sometimes I want to offer an opposing view
but you will see that I am quite happy to let him have the last word.
He has explained his position and I have clarified mine.

Of course you (and others) are free to tell me whose posts you think I
should ignore. I take that advice seriously (but not as gospel). For
some reason (which I really don't understand) I don't feel I want to
offer such advice myself.
 
B

Bill Cunningham

Mark L Pappin said:
Bill Cunningham said:
I don't know how to do this with spaces or for.

OK, it appears that you've not "got it" as I had hoped you might, so
here are the underlying ideas you need to move from "print a thing
that's hard-coded in the source" to "print a sequence of things that
can be computed".

- an 'int' variable can hold values from 0 up to INT_MAX (and down to
INT_MIN, but that's not important right now), where INT_MAX is at
least 32767
- 'printf()' can print the value of an 'int' variable, as well as a
literal string
- a 'for' loop can initialize a variable, loop around "doing stuff"
until that variable hits an upper limit, and increment the variable
after each time it has "done stuff"

(There are other concepts that could be used to implement this, like
'unsigned int', 'if', etc. They are also Not Important Right Now.)


Since you appear to need it, here's a template for you. Replace the
comments with source that does what's suggested.

#include <stdio.h>
int main(void) {

/* define an int variable called i */

for (/* set i to the value 0 */ ;
/* test to see that i is still less than 7 */ ;
/* increment i*/) {

/* print the value of i as a decimal integer */
/* print a space */

}

/* print a newline, since you only need one of them */

return 0;
}


The "a-ha!" moment I'm hoping you'll have here is the realisation that
the program does not need to have _everything_ laid out explicitly for
it, but instead its behaviour can be described at a higher level. For
instance, if question 3 was changed to ask for all the integers from 0
to 100 as output, then your first answer would need to have the string
extended by around 300 characters, but the above program would merely
need the test changed from "is i less than 7?" to "is i less than
101?". In the real world, specifications _do_ change like this, so
having a tool to make handling the change easy is an advantage.

So I will post another way of doing it and I don't know how to
include spaces. But the program works.

It only "works" because you were unlucky. Ben has given you an
example of a place where it doesn't "work", but you obviously don't
understand why.
#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};

The line above creates an array of 7 characters only. What sits after
those characters in memory could be anything.
printf("%s\n",a);

The "%s" format specifier must be given a string.
A string is a sequence of characters ending with a '\0'.
The array whose starting address you have passed is not a string.
The standard does not define what happens now.

In your case there just happened to be a '\0' sitting in the chunk of
memory immediately after 'a[]'; in Ben's there wasn't.


For now, don't think about using array notation until I suggest that
it might be handy for solving a particular problem.
#include <stdio.h>

int main() {
int i;
for(i=0;i<7;i++)
printf("%i \n",i);
}

/* This is the code I came up with */

0
1
2
3
4
5
6

Hum. Now I didn't expect that. I expected the numbers to be printed
horizontally.

Bill
 
R

Richard

Ben Bacarisse said:
For the record, I don't use the term myself. It is too easy to throw
around and too hard to decide in all the cases where it matters.


If you mean by this that you've given me your opinion before, then I
take your word for it. If you mean that most people here are of that

"over this" - he has given his instructions.....
opinion, then I agree, and that is in part why I posted. If anyone
else had chosen to point out how a debugger is unlikely to help in
this case, I would not have felt the need.

The point is that it would. You might not see it. But you are looking
from a different, possibly equally valid, angle. I have spent a lot of
time with trainees and I have seen people almost as stuck as Bill. Best
way is to get them into the memory and SEE things as the program
steps. To see the string. To see the chars. To see the end of string
marker.

And if you think this advice is trolling then I am surprised.
I have found "Plain Richard" to be someone that I do not want to argue
with. He does not try to understand other people's positions and
will, in the end, resort to claiming that he just does not believe his
opponents.

In some cases I take that stance. For example when someone assures me
they can debug 50,000 lines of foreign c code quicker from a printout
than from using an industry strength debugger. Those kind of things that
only get spouted in clc. it is nonsense. Can someone in the world? I
have no doubt someone can. Would we recommend that to someone like Bill
because some Indian Guru can? No we would not. Would we guess that 99%
can because Heathfield once spotted a bug on page one 10 years ago? No
we would not. We would advise sensible procedures to approach the
problem.
That does not mean, to me, that everything he writes
should go unchallenged. Sometimes I want to offer an opposing view
but you will see that I am quite happy to let him have the last word.
He has explained his position and I have clarified mine.

My position here is that its the old seed versus crops thing with
Bill. Get him to teach himself the basic types. Feeling them with a
debugger is a great way. He seems to be as clueless as 6 months ago and
need to get his hands dirty. Pasting in solutions for him is not helping
him - if anything it is hindering him since he then mistakenly thinks he
knows why something is working. He doesn't. He needs to follow the
tutorial and learn how to examine his own programs for now. More
complicated things can follow.
Of course you (and others) are free to tell me whose posts you think I
should ignore. I take that advice seriously (but not as gospel). For
some reason (which I really don't understand) I don't feel I want to
offer such advice myself.

Bwian does it a lot. He has been called on it a lot. I have no idea why
he feels the need to tell others who or what to listen to. Frankly I
think he sees himself as Heathfield's right hand man or Rottweiler. I'm
not sure which.
 
K

Keith Thompson

[snip]

Perhaps you and Mark Pappin can agree on a tag for the subject header
of any articles for Mark's attempts to provide you with a tutorial.
I suggest "[BC]".
 
B

Bill Cunningham

0
1
2
3
4
5
6

Hum. Now I didn't expect that. I expected the numbers to be printed
horizontally.
what am i saying ? Of course they are going to go vertically because of \n

Bill
 
B

Barry Schwarz

Actually I bookmarked the tutorial and haven't got a chance to really
read over it. I have been writing several C utilities and they have been
working so when I pop them into gdb and try to run them they end without an
error code.

Given your monumental lack of understanding of the most fundamental C
concepts as exhibited by the code you have posted and the excuses you
hide behind, how can you possibly even think of writing a utility, let
alone claim that it works?


Remove del for email
 
B

Barry Schwarz

Ben Bacarisse said:
He said it worked so presumably if he'd looked he'd see what he expects
to see. For example, in gdb it shows:

(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.

...And you are your best debugger. I guess I am in that camp. However,

Not only are you not in that camp, you may not even be on the same
planet.
if Richard is talking about file sectors and segments and the internals of
files like symbol tables and headers I might just be interested in learning
a little more about gdb.

Are you sure this shouldn't be a while loop to match your last
haphazard guess? If you are going to throw terms around at random,
don't you want to stick to C terms?


Remove del for email
 
M

Mark L Pappin

Keith Thompson said:
[snip]

Perhaps you and Mark Pappin can agree on a tag for the subject header
of any articles for Mark's attempts to provide you with a tutorial.
I suggest "[BC]".

That sounds like a reasonable idea, Keith.

Bill, to eliminate the potential problem of your news server expiring
the most recent posting in this thread before you've replied, and to
eliminate the actual problem of the Subject line not reflecting the
content of the posts, please add "[BC]" to the subject line of any new
thread you start that relates to our ongoing exhibition tutorial.
You'll notice that I've updated the Subject here.

Richard Heathfield said:
Bill Cunningham said:

Bill, can you see the difference in structure between the template I
suggested and what you've got here?

I suggested

for (A ; B ; C) {
D
}
E

and you have written

for (A ; B ; C)
F

I suggested that you print the value of i and the space within the
body of the loop, and then print the newline after the loop finishes.
You printed each of those 3 things within the body of the loop. You
also used the "no curly brackets" form of the loop and no indentation
which may in fact be contributing to your confusion over what happens
when.

I would recommend that until your skill level goes way up you
should use curly brackets around the body of every loop, conditional,
etc. that you write - this will help to reinforce the idea of
structure for you. Thus, always write

for (A ; B ; C) {
D
}
E

and never

for (A ; B ; C)
D
E

even if 'D' is just a single statement.

Just in case you're confused about what the 'for' construct does: the
above will first do 'A', then it will test if 'B' is zero - if it is,
then the loop finishes, but if not then it will do 'D', and then 'C',
and back to the test. After the loop finishes (that is, 'B' is found
to be equal to zero), then 'E' will be run. (If this is unclear, tell
me - otherwise, I will assume that you understand it in full.)


Likewise, stick with

if (A) {
B
}
else {
C
}

(once we start writing code using 'if') instead of

if (A)
B
else
C

For the purposes of this tutorial I recommend that you indent your
code as I have done here, and at least 2 spaces per level. After
you're more competent at writing actual code is the time for you to
worry about the pros and cons of different indentation styles - right
now we need to reinforce the structure of the code inside your head,
and keep it understandable for those of us who have to read what you
write.


I see that you've later realized why your output appears in a vertical
column instead of a horizontal row. Please have another go at posting
a complete program that prints a single line containing the digits 0
through 6 inclusive, separated by spaces, followed by a newline
character. Once you've managed to regurgitate that (and that is all
this initial bit will be: learning some common patterns by rote until
you have them internalized), I'll give you another couple of problems
to work on, that use what we've done so far and add a little bit at a
time to it.

mlp
 
D

Default User

Ben said:
For the record, I don't use the term myself. It is too easy to throw
around and too hard to decide in all the cases where it matters.

You're free to feel that way. Actions speak louder than words. Riley,
along with Kenny and Twink, have the goal to disrupt the forum.




Brian
 
R

Richard

Default User said:
You're free to feel that way. Actions speak louder than words. Riley,
along with Kenny and Twink, have the goal to disrupt the forum.




Brian

Which part of my sage advice to get a beginner like Bill familiar with C
data structure do you disagree with Bwian?

Tell me, do you ever offer any opinion of your own which is not directly
linked to petty net nannying? Every group has one - and you're the c.l.c
one. Well done.
 
C

Chris Torek

I apologize in advance for yet another off-topic posting, but it
may explain a lot of things (if my assumptions are correct).

The "a-ha!" moment I'm hoping you'll [i.e., Bill will] have here ...

He may well have it, but then I suspect he will need to have it
again the next day.

At one point, Mr Cunnigham mentioned that he was on Klonopin (a
brand name for clonazepam). I suspect (given the evidence accumulated
over several years) that he still is, and has been on it for a very
long time. This particular drug has a nasty side effect: it often
causes a sort of anterograde amnesia, making it impossible to learn
new things. (Depending on the degree of effect, some new things
might be learned over time, but not much, and not quickly.)
 
R

Richard

Chris Torek said:
I apologize in advance for yet another off-topic posting, but it
may explain a lot of things (if my assumptions are correct).

The "a-ha!" moment I'm hoping you'll [i.e., Bill will] have here ...

He may well have it, but then I suspect he will need to have it
again the next day.

At one point, Mr Cunnigham mentioned that he was on Klonopin (a
brand name for clonazepam). I suspect (given the evidence accumulated
over several years) that he still is, and has been on it for a very
long time. This particular drug has a nasty side effect: it often
causes a sort of anterograde amnesia, making it impossible to learn
new things. (Depending on the degree of effect, some new things
might be learned over time, but not much, and not quickly.)

In that case he is fighting a losing battle learning C as he is. He
would be far better off following a measured tutorial and reviewing the
lessons daily. Random questions to a usenet group will do him no good
whatsoever.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top