By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

for and arrays

P: n/a
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Bill
Jul 22 '08 #1
Share this Question
Share on Google+
182 Replies


P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];
Did you try?
This should take every element of the array a and set it to 1,2,3,4,5.
Great.
No, a loop that "sets" things will usually have an assignment in its
body.
Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.
I'd stick with 1 dimensional ones. If you are in this for the
challenge of learning C, then it is much better to master the easy
bits before moving on. If you have some objective in mind (i.e. you
want to write some specific program) then you should pick a language
with fewer pitfalls and peculiarities than C. In particular,
interpreted languages let you learn by trying things out which, I
suspect, is a style that would suit you.

--
Ben.
Jul 22 '08 #2

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Bill Cunningham" <no****@nspam.comwrites:
>I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

Did you try?
Well I tried and it compiled with no errors. Or atleast compiled anyway.
Yes this is an excercise to learn. All I do is an excercise to learn. What
if one had an array of 500 ints and had the simple task of setting them
1-500? How's this for another take.

int a[10];
int b;
for (b=0;b<10;b++) {

a[ b=b+1];
}
Bill
Jul 22 '08 #3

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>"Bill Cunningham" <no****@nspam.comwrites:
>>I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

Did you try?

Well I tried and it compiled with no errors. Or atleast compiled
anyway.
You must have found out by now that a program that compiles is like an
essay the spell-checks. It does not mean you've answered the question.
Yes this is an excercise to learn.
Compiling it is not enough, then. The wonderful thing about
programming is that your program can tell you if you've got it right.
All I do is an excercise to learn. What
if one had an array of 500 ints and had the simple task of setting them
1-500? How's this for another take.

int a[10];
int b;
for (b=0;b<10;b++) {

a[ b=b+1];
}
Let's assume that you intended to write a[500] rather than a[10]. You
need to move one character characters to turn the above into
a program that does what you want, but can you see which character
needs to move at to where?

[I still think that you should learn an interpreted language.]

--
Ben.
Jul 22 '08 #4

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
I understand this code.
No, I'm afraid you don't.
int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.
You seem to be thinking that the line "int a[b];" assigns a value to
an element of a. In fact, it doesn't do anything at all like that.
Knowing what it actually does won't help you, so please ignore the
following. (It declares a VLA which hides the declaration of a.)

Just this once, I'll give you a freebie. If you want to set the
elements of a to 1, 2, 3, 4, 5, here's one way to do it:

int a[5];
int i;
for (i = 0; i < 5; i ++) {
a[i] = i + 1;
}
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.
You don't even understand what a 2 dimensional array is. Your
question assumes that it's a 5-element array followed by a 3-element
array. It isn't. It's a 5-element array, each of whose elements is a
3-element array of ints, for a total of 15 (5*3) ints.

Here's a conceptual picture of your one-dimensional array, where each
'#' represents an element that can hold an int value:

+-+-+-+-+-+
|#|#|#|#|#|
+-+-+-+-+-+

And here's a corresponding picture for your "int a[5][3];":

+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
But you shouldn't even be thinking about 2-dimensional arrays until
you have a firm grasp on 1-dimensional arrays.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 22 '08 #5

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
You don't even understand what a 2 dimensional array is. Your
question assumes that it's a 5-element array followed by a 3-element
array. It isn't. It's a 5-element array, each of whose elements is a
3-element array of ints, for a total of 15 (5*3) ints.
I understand if I am correct that each of the 5 elements have 3
elements. Yes. What I didn't understand was

a[i]=i+1;

Bill
Jul 23 '08 #6

P: n/a
On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <ks***@mib.org>
wrote:
>"Bill Cunningham" <no****@nspam.comwrites:
>I understand this code.

No, I'm afraid you don't.
>int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.

You seem to be thinking that the line "int a[b];" assigns a value to
an element of a. In fact, it doesn't do anything at all like that.
Knowing what it actually does won't help you, so please ignore the
following. (It declares a VLA which hides the declaration of a.)
Since there are no braces around the statement, it is a constraint
violation in C89 since declarations must precede statements. Even in
C99 it should be a duplicate definition of a.
Remove del for email
Jul 23 '08 #7

P: n/a
On Tue, 22 Jul 2008 22:15:52 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>I understand this code.
You are not even close as others have pointed out.
>
int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
What do you think the phrase "first element of 5" means? What about
"the second element of 3"? How many elements to you think this 2D
array has?
>to 1,2,3 ? I don't know how to work with a 2 dimensional array.
Don't even try till after you understand 1D arrays.
Remove del for email
Jul 23 '08 #8

P: n/a
Barry Schwarz <sc******@dqel.comwrites:
On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <ks***@mib.org>
wrote:
>>"Bill Cunningham" <no****@nspam.comwrites:
>>I understand this code.

No, I'm afraid you don't.
>>int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.

You seem to be thinking that the line "int a[b];" assigns a value to
an element of a. In fact, it doesn't do anything at all like that.
Knowing what it actually does won't help you, so please ignore the
following. (It declares a VLA which hides the declaration of a.)

Since there are no braces around the statement, it is a constraint
violation in C89 since declarations must precede statements. Even in
C99 it should be a duplicate definition of a.
It's also a constraint violation in C89 because it's a VLA. But I
didn't want to go into too much detail about why it's wrong. The way
to transform that into what Bill intended is to delete it and write a
correct line.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 23 '08 #9

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ? Though I have even more to learn.
This C syntax is confusing. Bash and Perl are interesting though.

Bill
Jul 23 '08 #10

P: n/a
Bill Cunningham said:
>
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>[I still think that you should learn an interpreted language.]
I've come so far with C
No, you haven't. You're still failing to grasp Chapter 1 of K&R2, after
what must be something like a decade of trying. That isn't far. That's
near.
now why stop ?
Because you're wasting your time.
Though I have even more to learn.
C is not a large language. I would estimate that you've mastered perhaps a
thousandth of it. In what, ten years? Life's too short. Do something else.
This C syntax is confusing.
Actually, C's syntax is astoundingly simple. Okay, maybe it takes a little
getting used to - but anyone who is ever going to "get it" will "get it"
within a few weeks or, at most, months.
Bash and Perl are interesting though.
If you think C's syntax is confusing, you have some real treats in store.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 23 '08 #11

P: n/a
On 2008-07-22, Ben Bacarisse <be********@bsb.me.ukwrote:
"Bill Cunningham" <no****@nspam.comwrites:
>for (b=0;b<10;b++) {

a[ b=b+1];
}
[ snip ]
[I still think that you should learn an interpreted language.]
In light of that cute a[b=b+1] above, it better be one
that is very liberally interpreted, to the point of being
downright charitable. :)
Jul 23 '08 #12

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:xr******************************@bt.com...
Bill Cunningham said:
>>
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>[I still think that you should learn an interpreted language.]
I've come so far with C

No, you haven't. You're still failing to grasp Chapter 1 of K&R2, after
what must be something like a decade of trying. That isn't far. That's
near.
[snip]

About 5 years is more like it. And for one year I gave up on C. But
everything is in C or C++. Looking at Bash it doesn't look any simpler
really and in just looks C like. I need someone to explain things to me when
I get stuck. If I had a tutor you would be suprised in what I can pick up.
But that's one thing you don't get in clc. The one thing I need.

Bill
Jul 23 '08 #13

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ? Though I have even more to learn.
This C syntax is confusing. Bash and Perl are interesting though.
No, I'm afraid you haven't come very far at all with C.

You might consider studying Python. Perl is largely a mish-mash of C,
AWK, shell scripting, and a few other languages. Python has a more
coherent design. Personally, I'm a big fan of Perl, but I don't think
it's the language for you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 23 '08 #14

P: n/a

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Bill Cunningham" <no****@nspam.comwrites:
>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ? Though I have even more to
learn.
This C syntax is confusing. Bash and Perl are interesting though.

No, I'm afraid you haven't come very far at all with C.

You might consider studying Python. Perl is largely a mish-mash of C,
AWK, shell scripting, and a few other languages. Python has a more
coherent design. Personally, I'm a big fan of Perl, but I don't think
it's the language for you.
There's always assembly :-)

Bill
Jul 23 '08 #15

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ?
Learning is not like rolling a boulder up a hill. The learning does
not start to pour out of you head the moment you stop stuffing more
in. In fact, you may find that *more* of the C you've seen so far
"sticks" if you take a break and look at something else. The only
reason to keep going would be if you are very close to achieving some
goal with C, but that does not seem to the case.

However, the main reason is because I think you will get on better
with language that allows you to get immediate feedback from trying
things out. I don't say this without some experience of how people
learn programming. Some people are born planners -- they think, plan,
sketch things out, revise the plan, and then write an almost perfect
program first time. Other like to try this construct, have a go with
that idea, wonder why such-and-such behaves like so-and-so and, over
time, they can evolve a program. People in the first group would
prefer to read (often cover to cover) a reference type of book for the
language they are about to use. Those in the second like tutorials
with lost of examples and variations on a theme. I think you are
closer to the second group than the first.

You can learn any language using either style, but interpreted
languages suit the second group better, at least at first.
Though I have even more to learn.
This C syntax is confusing. Bash and Perl are interesting though.
Both are a Very Bad Idea (for reasons too messy and off topic to get
into here). I think you would prefer a language where the pieces fit
together in a logical and relatively unconstrained way. The best
example of that is Haskell, but all the best books are rather
expensive (and all the on-line tutorials seem to delight in too much
jargon). My top choice for you would be Scheme because it is clean
and logical and has excellent supporting texts, now entirely free on
the web. See http://mitpress.mit.edu/sicp/ If that is too far removed
from what you know, try Python.

BTW, far more significant would be to find a friend, nearby, who could
help you learn.

--
Ben.
Jul 23 '08 #16

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Bill Cunningham" <no****@nspam.comwrites:
>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ?

Learning is not like rolling a boulder up a hill. The learning does
not start to pour out of you head the moment you stop stuffing more
in. In fact, you may find that *more* of the C you've seen so far
"sticks" if you take a break and look at something else. The only
reason to keep going would be if you are very close to achieving some
goal with C, but that does not seem to the case.
[snip]

Goal? Read and understand file sections,headers, symbols and sectors. That
would probably involve hex dumps, debuggers, BFD knowledge and logic
understanding to take apart a program to bits and reconstruct whatever I
want like a new file format. That is the goal in my head. But one has to
learn a language with this and assembler will be involved.

Bill
Jul 23 '08 #17

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>"Bill Cunningham" <no****@nspam.comwrites:
>>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

[I still think that you should learn an interpreted language.]

I've come so far with C now why stop ?

Learning is not like rolling a boulder up a hill. The learning does
not start to pour out of you head the moment you stop stuffing more
in. In fact, you may find that *more* of the C you've seen so far
"sticks" if you take a break and look at something else. The only
reason to keep going would be if you are very close to achieving some
goal with C, but that does not seem to the case.

[snip]

Goal? Read and understand file sections,headers, symbols and sectors. That
would probably involve hex dumps, debuggers, BFD knowledge and logic
understanding to take apart a program to bits and reconstruct whatever I
want like a new file format. That is the goal in my head.
I did not say you did not have one. I said, keep up with C if you are
close to achieving a particular goal using it. I don't think you
are. I.e. I don't think you will loose anything by trying a new way
to learn programming.

Your reply reads, to me, as if I have annoyed you. I am sorry if that
is that case, but I accept that it is almost impossible to avoid being
annoying in this medium. My suggestions about how you might change
tack were intended to be helpful. When the path to the summit is too
steep, going sideways can get you up there faster.
But one has to
learn a language with this and assembler will be involved.
Very few of these goals (things to learn about) are language specific
and although learning C might help, it seems to me neither necessary
nor sufficient for achieving them.

--
Ben.
Jul 23 '08 #18

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Bill Cunningham" <no****@nspam.comwrites:
>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>"Bill Cunningham" <no****@nspam.comwrites:

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

[I still think that you should learn an interpreted language.]
>
I've come so far with C now why stop ?

Learning is not like rolling a boulder up a hill. The learning does
not start to pour out of you head the moment you stop stuffing more
in. In fact, you may find that *more* of the C you've seen so far
"sticks" if you take a break and look at something else. The only
reason to keep going would be if you are very close to achieving some
goal with C, but that does not seem to the case.

[snip]

Goal? Read and understand file sections,headers, symbols and sectors.
That
would probably involve hex dumps, debuggers, BFD knowledge and logic
understanding to take apart a program to bits and reconstruct whatever I
want like a new file format. That is the goal in my head.

I did not say you did not have one. I said, keep up with C if you are
close to achieving a particular goal using it. I don't think you
are. I.e. I don't think you will loose anything by trying a new way
to learn programming.

Your reply reads, to me, as if I have annoyed you. I am sorry if that
is that case, but I accept that it is almost impossible to avoid being
annoying in this medium. My suggestions about how you might change
tack were intended to be helpful. When the path to the summit is too
steep, going sideways can get you up there faster.
Annoyed? Well I do read this as if you are trying to help. But giving up
C is like giving up on math because you have trouble pulling products from
multiples from your head because you have trouble with multiplication. There
are calculators and you can go onto higher maths if you have a little help
with a calculator.

It's like giving up.

Bill
Jul 24 '08 #19

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

[snipped much]
Very few of these goals (things to learn about) are language specific
and although learning C might help, it seems to me neither necessary
nor sufficient for achieving them.
What about taking something like an API like system calls, Glib, GTK+ or
something that is built in C and learn more and more about standard C.

I think that would either help or blow up in my face and confuse me
more.

Bill
Jul 24 '08 #20

P: n/a

"Mark L Pappin" <ml*@acm.orgwrote in message
news:87************@Don-John.Messina...

[snip]
1. Copy and paste this code (between "/*begin1*/" and "/*end1*/") into
a new source file, compile, and run it. What output does it produce?

/*begin1*/
#include <stdio.h>
int main(void){
/* you are not expected to understand this - it is to
verify that you can and will follow instructions */
printf("%s%c%d\n",
"protest"+3,
"blank space"[5],
27/3-2*2*2);
return 0;
}
/*end1*/
2. Write a program to print a single line containing the greeting
"Hello, world!" followed by a newline character.
The results I got from your first program was a warning concerning no
newline character and the text

test 1

Here is the answer to #2 of your question.

#include <stdio.h>

int main(void) {
printf("hello world\n");
return 0;
}

Do you wish to correspond through private email ? I will post the answer to
your third question later.

Bill
Jul 24 '08 #21

P: n/a

"Bill Cunningham" <no****@nspam.comwrote in message
news:Va5ik.507$_l.500@trnddc04...

[snip]
The results I got from your first program was a warning concerning no
newline character and the text

test 1

Here is the answer to #2 of your question.

#include <stdio.h>

int main(void) {
printf("hello world\n");
return 0;
}
Oops. I forgot the ! at the end of the hello world.
#include <stdio.h>
>
int main(void) {
printf("hello world!\n");
return 0;
}
There.

Bill

Jul 24 '08 #22

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Bill

Bill, instead of making such elementary mistakes time after time after
time why not just take my advice and step through your code with a
debugger for your system?

Surely you are trolling? You can surely not be so confused at this
stage?
Jul 25 '08 #23

P: n/a
On Thu, 24 Jul 2008 00:56:00 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...

[snipped much]
>Very few of these goals (things to learn about) are language specific
and although learning C might help, it seems to me neither necessary
nor sufficient for achieving them.
What about taking something like an API like system calls, Glib, GTK+ or
something that is built in C and learn more and more about standard C.
Don't even think about. You have been on chapter 1 for five years.
>
I think that would either help or blow up in my face and confuse me
more.
Until you have firm foundation in basic C, these add-ons will only
serve to confuse you even more than you are now.
Remove del for email
Jul 25 '08 #24

P: n/a
On Jul 23, 3:15*am, "Bill Cunningham" <nos...@nspam.comwrote:
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a[b];

* * This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Bill
hi,
i guess this is wat you want to do ..

for(i=0;i<5;i++)
for(j=0;j<3;j++)
a[i][j]=j;

ur 2D array has 5 rows 3 columns ( 5 x 2 ) 1st column is set to 0 2nd
to 1 3rd to 2 for each row..
Jul 25 '08 #25

P: n/a
On Jul 24, 6:41 am, Keith Thompson <ks...@mib.orgwrote:
"Bill Cunningham" <nos...@nspam.comwrites:
"Ben Bacarisse" <ben.use...@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ? Though I have even more to learn.
This C syntax is confusing. Bash and Perl are interesting though.

You might consider studying Python. Perl is largely a mish-mash of C,
AWK, shell scripting, and a few other languages. Python has a more
coherent design.
I think these are what I need to learn: C (C++ sometimes), Bourne
Shell, Makefile Scripting.
Personally, I'm a big fan of Perl, but I don't think
it's the language for you.
Last month, I wrote an automatic ssh and telnet script in Perl.
Because the files for rsh login on the destinate host may not be
available, I need to go to Perl and except. After I code an ssh and
telnet client in C, I can do the job without Perl. Perl is a mess.

Eric Steven Raymond said in his book: ``C and Python are semi-compact;
Perl, Java, Emacs Lisp, and shell are not.'' (http://www.faqs.org/docs/
artu/ch04s02.html#orthogonality)
Jul 25 '08 #26

P: n/a
Mark L Pappin <ml*@acm.orgwrites:
"Bill Cunningham" <no****@nspam.comwrites:
>I will post the answer to your third question later.

I look forward to it
Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).

--
Ben.
Jul 26 '08 #27

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
Mark L Pappin <ml*@acm.orgwrites:
>"Bill Cunningham" <no****@nspam.comwrites:
>>I will post the answer to your third question later.

I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).
Mark,

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

Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill
Jul 27 '08 #28

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>Mark L Pappin <ml*@acm.orgwrites:
>>"Bill Cunningham" <no****@nspam.comwrites:
I will post the answer to your third question later.

I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).
Mark,

I don't know how to do this with spaces or for. So I will post another way
of doing it and I don't know how to include spaces. But the program
works.
You don't know how to include spaces?

ROTFLM. It just gets better.
>
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill
Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?
Jul 27 '08 #29

P: n/a

"Richard" <rg****@gmail.comwrote in message
news:g6**********@registered.motzarella.org...
"Bill Cunningham" <no****@nspam.comwrites:
>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>Mark L Pappin <ml*@acm.orgwrites:

"Bill Cunningham" <no****@nspam.comwrites:
I will post the answer to your third question later.

I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).
Mark,

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

You don't know how to include spaces?

ROTFLM. It just gets better.
>>
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?
All I get from that thing is 'no stack'

Bill
Jul 27 '08 #30

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g6**********@registered.motzarella.org...
>"Bill Cunningham" <no****@nspam.comwrites:
>>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
Mark L Pappin <ml*@acm.orgwrites:

"Bill Cunningham" <no****@nspam.comwrites:
>I will post the answer to your third question later.
>
I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).

Mark,

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

You don't know how to include spaces?

ROTFLM. It just gets better.
>>>
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?

All I get from that thing is 'no stack'
And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!
Jul 27 '08 #31

P: n/a
[I am re-posting my reply in case than means everything can be kept in
the one place.]

"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>Mark L Pappin <ml*@acm.orgwrites:
>>"Bill Cunningham" <no****@nspam.comwrites:
I will post the answer to your third question later.

I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).
<snip>
I don't know how to do this with spaces or for. So I will post another way
of doing it and I don't know how to include spaces. But the program
works.
You were unlucky. I have more luck, it seems. On my system I get:

0123456�-��H.��Pt���l��ЃH.��Pt��

Do you know why?
#include <stdio.h>

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

char a[]={'0',' ','1',' ','2',' ','3',' ','4',' ','5',' ','6'};

did not occur to you. Knowing why you did not think of it is probably
more helpful than getting the program right. For example, did you
look for, and not find, a special escape sequence for a space
character (like there is for newline: '\n'). If so what did you do
when you did not find it? Maybe you simply did not know how to write
a space and you don't know any way to find such things out. I have a
feeling that examining these questions will be more helpful to you
than someone telling you how to write a space.
printf("%s\n",a);
}
--
Ben.
Jul 27 '08 #32

P: n/a

"Richard" <rg****@gmail.comwrote in message
news:g6**********@registered.motzarella.org...
And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!
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.

Bill
Jul 27 '08 #33

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
You were unlucky. I have more luck, it seems. On my system I get:

0123456?-??H.??Pt???l???H.??Pt??

Do you know why?
No I have no idea. That looks like garbage on your printout.

Bill
Jul 27 '08 #34

P: n/a
Richard<rg****@gmail.comwrites:

<snip program>
You don't know how to include spaces?

ROTFLM. It just gets better.
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.
>#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?
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.

--
Ben.
Jul 27 '08 #35

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g6**********@registered.motzarella.org...
>And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!

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.

Bill
Stop writing C utilities until you figure out strings. Trust me. use the
debugger to inspect your data structures.
Jul 27 '08 #36

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
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
Jul 27 '08 #37

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>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".

Jul 27 '08 #38

P: n/a
Ben Bacarisse wrote:
Richard<rg****@gmail.comwrites:

<snip program>
You don't know how to include spaces?

ROTFLM. It just gets better.

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
Jul 27 '08 #39

P: n/a
"Default User" <de***********@yahoo.comwrites:
Ben Bacarisse wrote:
>Richard<rg****@gmail.comwrites:

<snip program>
You don't know how to include spaces?

ROTFLM. It just gets better.

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

Jul 27 '08 #40

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
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
Jul 27 '08 #41

P: n/a
"Default User" <de***********@yahoo.comwrites:

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

--
Ben.
Jul 28 '08 #42

P: n/a

"Mark L Pappin" <ml*@acm.orgwrote in message
news:87************@Don-John.Messina...
"Bill Cunningham" <no****@nspam.comwrites:
>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
Jul 28 '08 #43

P: n/a
Ben Bacarisse <be********@bsb.me.ukwrites:
"Default User" <de***********@yahoo.comwrites:

<snip>
>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
"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.
Jul 28 '08 #44

P: n/a
Bill Cunningham said:

<snip>
#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.
Let's look at your printf again:

printf("%i \n",i);

Specifically, let's look at the format string:

"%i \n"

Why did you put the %i there?
Why did you put the space there?
Why did you put the \n there?

What effect do you think the \n will have, each time through the loop?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 28 '08 #45

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
[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]".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '08 #46

P: n/a

"Bill Cunningham" <no****@nspam.comwrote in message
news:3j9jk.201$Ht4.163@trnddc01...
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
Jul 28 '08 #47

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:Y8******************************@bt.com...
What effect do you think the \n will have, each time through the loop?
Sorry Richard. I fogot again.

Bill
Jul 28 '08 #48

P: n/a
On Sun, 27 Jul 2008 21:14:57 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
"Richard" <rg****@gmail.comwrote in message
news:g6**********@registered.motzarella.org...
>And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!

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
Jul 28 '08 #49

P: n/a
On Sun, 27 Jul 2008 21:51:43 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
>
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>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
Jul 28 '08 #50

182 Replies

This discussion thread is closed

Replies have been disabled for this discussion.