Can C do it ?

B

bpascal123

The fundamental problem that he's referring to is the fact that the
Conju array was declared as having only 7 elements, whereas your code,
in three different places, refers to Conj[7], which would (if it did not
have undefined behavior) refer to the non-existent 8th element of the
array. This might work without a problem if the position where the 8th
element would go doesn't happen to be currently in use by any other part
of the program, but that's just random luck, and it's bad luck, because
it makes your program appear to work correctly despite the fact that it
has a very serious problem.

Hi,

As my 2 last replies a few minutes ago, Conj[7] in a loop has 8
itiration starting from 0 and, i have assigned Conj[7] with a value
that the code takes in consideration. So the code should behave well
here or did i miss something when i was learning these.

I know in vba we can set base 0 to 1. I don't know if that can be done
in C (it would help!). Maybe this can be done if Array[0] is set with
a no value and any loops in this array start at 1...

Thanks,

Pascal
 
K

Keith Thompson

The fundamental problem that he's referring to is the fact that the
Conju array was declared as having only 7 elements, whereas your code,
in three different places, refers to Conj[7], which would (if it did not
have undefined behavior) refer to the non-existent 8th element of the
array. This might work without a problem if the position where the 8th
element would go doesn't happen to be currently in use by any other part
of the program, but that's just random luck, and it's bad luck, because
it makes your program appear to work correctly despite the fact that it
has a very serious problem.

The above was written by James Kuyper. Please stop snipping
attribution lines.
As my 2 last replies a few minutes ago, Conj[7] in a loop has 8
itiration starting from 0 and, i have assigned Conj[7] with a value
that the code takes in consideration. So the code should behave well
here or did i miss something when i was learning these.

Yes, you missed something. You declared Conju as:

Conju2 Conju[7];

Either assigning a value to Conju[7] or reading the value of Conju[7]
invokes undefined behavior, since Conju[7] does not exist. The
elements that do exist are Conju[0], Conju[1], ..., Conju[6].

If the memory location that *would* have held Conju[7] happens
not to be used by anything else, then it's likely you'd get away
with it. Getting away with it is the worst thing that can happen.
If you actually need to refer to Conju[7], you need to define the
array with 8 elements. If you define Conju with 7 elements, you must
not refer to Conju[7].
I know in vba we can set base 0 to 1. I don't know if that can be done
in C (it would help!). Maybe this can be done if Array[0] is set with
a no value and any loops in this array start at 1...

No, C arrays start at 0. There are ways to pretend that they
start at 1; some ways work but waste space, others invoke undefined
behavior. If you're trying to learn C, you should learn to deal
with 0-based arrays; don't fight the language.
 
B

bpascal123

The fundamental problem that he's referring to is the fact that the
Conju array was declared as having only 7 elements, whereas your code,
in three different places, refers to Conj[7], which would (if it did not
have undefined behavior) refer to the non-existent 8th element of the
array. This might work without a problem if the position where the 8th
element would go doesn't happen to be currently in use by any other part
of the program, but that's just random luck, and it's bad luck, because
it makes your program appear to work correctly despite the fact that it
has a very serious problem.

The above was written by James Kuyper.  Please stop snipping
attribution lines.
As my 2 last replies a few minutes ago, Conj[7] in a loop has 8
itiration starting from 0 and, i have assigned Conj[7] with a value
that the code takes in consideration. So the code should behave well
here or did i miss something when i was learning these.

Yes, you missed something.  You declared Conju as:

    Conju2 Conju[7];

Either assigning a value to Conju[7] or reading the value of Conju[7]
invokes undefined behavior, since Conju[7] does not exist.  The
elements that do exist are Conju[0], Conju[1], ..., Conju[6].

If the memory location that *would* have held Conju[7] happens
not to be used by anything else, then it's likely you'd get away
with it.  Getting away with it is the worst thing that can happen.
If you actually need to refer to Conju[7], you need to define the
array with 8 elements.  If you define Conju with 7 elements, you must
not refer to Conju[7].
I know in vba we can set base 0 to 1. I don't know if that can be done
in C (it would help!). Maybe this can be done if Array[0] is set with
a no value and any loops in this array start at 1...

No, C arrays start at 0.  There are ways to pretend that they
start at 1; some ways work but waste space, others invoke undefined
behavior.  If you're trying to learn C, you should learn to deal
with 0-based arrays; don't fight the language.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <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"


I now see my mistake. It'll take time before i get away from such
basic mistakes!
Thanks

<=== Conju2 Conju[7] ;

===> Conju[8] ;

Conju[0].Pro = "Je " ;
Conju[1].Pro = "Tu " ;
Conju[2].Pro = "Il ";
Conju[3].Pro = "Elle ";
Conju[4].Pro = "Nous " ;
Conju[5].Pro = "Vous ";
Conju[6].Pro = "Ils " ;
Conju[7].Pro = "Elles " ;

Conju[0].Term = "e" ;
Conju[1].Term = "es" ;
Conju[2].Term = "e" ;
Conju[3].Term = "e" ;
Conju[4].Term = "ons";
Conju[5].Term = "ez" ;
Conju[6].Term = "ent" ;
Conju[7].Term = "ent " ;
 
B

Barry Schwarz

Actually Keith wrote the following. You should preserve attributions.

You should not quote signatures unless you are commenting on them
specifically.
Hi Keith,

My last post is a reply to Barry's comment. There, i mention the code
which i think has undefined behavior. Barry didn't say much about it
so i could only guess where would this would be.for ( i = 0 ; i < 8 ; i++ )
printf("\n%s%s%s", Conju.Pro, verb2, Conju
.Term) ;


Conju was defined as an array of 7 structures. The legal subscripts
run from 0 to 6. This loop will attempt to access the non-existent
structure Conju[7] which still invokes undefined behavior.
The code is in fourth post earlier in this current discussion page.

Usenet is not known for preserving chronological order.
 
B

Barry Schwarz

Hi Barry, snip
for (i = 0, j = 0; i < sizeof pro / sizeof pro[0], i < sizeof term / sizeof term[0]; i ++)

First, can the two quotients ever be different?
I don't know at all how this code works. I got it from Pete's post/
reply earlier in this discussion. I take it as it is, i guess if i
keep on coding, sooner or later i'll understand

Pete only sent two messages in this thread, on 29 July and 30 July.
Neither contains that code.

Copying code without understanding is a bad habit to get into.

snip
Unfortunately, this object does not exist.

Neither does this one.

What object ? There are 2 distinct code and both works indepentantly
from the other. Variable names slighlty change "verb" is used in one
and "Verb" is used in the other one.

Why did you snip the line of code the comment referred to? Your reply
style makes it very difficult to have a coherent discussion.
 
P

Phil Carmody

pete said:
I know in vba we can set base 0 to 1. I don't know if that can be done
in C (it would help!).

There used to be something called "training wheels"
that people put on bicycles for their children.
I think eventually people realised
that you can't learn to ride a bike that way.
Maybe this can be done if Array[0] is set with
a no value and any loops in this array start at 1...

Whenever I see code like that, I think
"Pascal programmers, forced to write C at gunpoint".

Whenever I see code like that, I think "Aaaaargh - why am
I reading Numerical Recipies in C?"!

Phil
 
R

Ray

Even if i'll be doing very basic stuff for a while, I'd like not to
rely on pointer. As an accountant, learning to deal work with strings
and arrays can be helpful.

See, that's a problem. Strings and arrays *are* pointers in C.
An array is a pointer to a block of contiguous memory containing
like-typed values; a string is an array of characters.

If you intend to use strings and arrays, you must use pointers.

You may, however, use them very gently, in a way that means you
can think of them as strings and arrays and needn't worry too
much about getting them tangled.

In your place, with your application, I would start off like this:

-----------------------------------------------------------
/* this typedef makes mot into a type which can hold
strings up to 15 characters long. */
typedef char[16] mot;


/* Now make noms an array which can hold up to 9 mots, initialized to
the French pronouns */
mot noms[9] = {"Je","Tu","Il","Elle","Ca","Nous","Vous", "Ils","Elles"};

/* Finally make an array for the corresponding suffixes for regular
verb conjugations */
mot terminaisons[9] = {"e","es","e","e","e","ons","ez","ent","ent"};
--------------------------------------------------------------

Now, I won't lie to you; mot is a pointer type. noms and terminaisons
are pointers to memory blocks filled with more pointers. You can think
of them as strings and arrays if you're more comfortable that way, but
strings and arrays are only specialized kinds of pointers.

Bear
 
J

James Kuyper

Ray said:
See, that's a problem. Strings and arrays *are* pointers in C.
An array is a pointer to a block of contiguous memory containing
like-typed values; ...

Confusing arrays with pointers is very common, probably because of the
fact that arrays are often converted into pointers by the rules of C.
With three exception, an lvalue of array type is usually automatically
converted into a pointer to the first element of the array. A
declaration of a function parameter as if it were an array is
automatically converted into a declaration of a pointer to the element
type of the array.

If an array was a pointer, then given the following declaration:

int array[10];

sizeof(array) would be equal to sizeof(int*) rather than 10*sizeof(int),
and &array would have the type int**, rather than int (*)[10].
... a string is an array of characters.

According to 7.1.1p1, "A string is a contiguous sequence of characters
terminated by and including the first null character". That sequence
need not constitute an entire array; it could just be part of one, such
as, for example, the string starting at ("Hello, world!" + 7), which
consists of 7 characters and has a length of 6.
In your place, with your application, I would start off like this:

-----------------------------------------------------------
/* this typedef makes mot into a type which can hold
strings up to 15 characters long. */
typedef char[16] mot; ,,,
Now, I won't lie to you; mot is a pointer type.

No, mot is an array type, though it would be converted into a pointer
type if used to declare a function parameter. sizeof(mot) will be 16,
which, on most implementations, will not be the same as sizeof(char*).

I would recommend against using such a typedef, because of the peculiar
rules that C has for handling arrays. It's generally a bad idea to use a
typedef to hide the fact that that something is either an array or a
pointer, because it can lead to code that looks right but doesn't
actually work (and vice versa).
 
F

Flash Gordon

Ray said:
See, that's a problem. Strings and arrays *are* pointers in C.
An array is a pointer to a block of contiguous memory containing
like-typed values; a string is an array of characters.

No, that is WRONG!
An array is a block of memory, not a pointer to the block. Try taking
the size of an array to see this. It's just that there is an automatic
conversion from the array name to a pointer to the first element of the
array in most situations. However, not keeping the distinction clear
leads to a lot of confusion.
If you intend to use strings and arrays, you must use pointers.

Now that is, basically, true.
You may, however, use them very gently, in a way that means you
can think of them as strings and arrays and needn't worry too
much about getting them tangled.

In your place, with your application, I would start off like this:

I don't like typedefs of array types.
/* Now make noms an array which can hold up to 9 mots, initialized to
the French pronouns */
mot noms[9] = {"Je","Tu","Il","Elle","Ca","Nous","Vous", "Ils","Elles"};

/* Finally make an array for the corresponding suffixes for regular
verb conjugations */
mot terminaisons[9] = {"e","es","e","e","e","ons","ez","ent","ent"};

You may not be lying, but you are definitely WRONG. mot is an array
type, not a pointer type.
noms and terminaisons
are pointers to memory blocks filled with more pointers.

No, they are arrays. Arrays of arrays to be more precise.
You can think
of them as strings and arrays if you're more comfortable that way, but
strings and arrays are only specialized kinds of pointers.

This is also wrong.

I suggest you read chapter 6 of the comp.lang.c FAQ over at
http://c-faq.com/
 
K

Keith Thompson

Ray said:
See, that's a problem. Strings and arrays *are* pointers in C.
An array is a pointer to a block of contiguous memory containing
like-typed values; a string is an array of characters.
[...]

No, no, INT_MAX times no!

Arrays are not pointers. Pointers are not arrays.

An array is not a pointer to a block of contiguous memory, it
is the block itself. Arrays are manipulated using pointers, but
pointers to individual elements, not usually to the whole array.
(You can have a pointer to an array, but it's not the same thing
as a pointer to the array's first element; it's of a different type.)

A string is a data format, not a data type; an array may *contain*
a string.

You need to read and understand section 6 of the comp.lang.c FAQ
<http://www.c-faq.com>.
 
B

bpascal123

Hi,

I understand C vocabulary can rely on pointer for anything... That's
why i understand as well from studying for the last 3-4 months / 20-40
hrs a week, i haven't really started learning C since i was trying to
keep myself away from pointers.

As i said earlier, i'm new to programming. Like any other subjects a
good understanding of the basics will help later on unlike starting
straight with :
while ( *pc++ = *pb++ ) ; which is for me an obscure way to copy
elements from pb to pc (formerly declared as : char buffer[128] ; char
clearbuf[128] ; char *pb = buffer ; char *pc = clearbuf and sp on ...
it took me a while to understand this...arghhhh ! I think this is
still simple in the pointers chapter, pointers to functions and
memalloc will make things a lot more spicy.

About this discussion, you all should have a $10 fine for overspeed. I
really was looking for a "non-pointer" way to achieve this. Here it is
a few html pages later on the tutorial site i'm learning from :


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


int main(void)
{

char SUJ[6][5] = {"je","tu","il","nous","vous","ils"};
char TERM[6][5] = {"e","es","e","ons","ez","ent"};
char VERB[20];
int L;
int I;

printf("Verbe : ");
scanf("%s", VERB);


L=strlen(VERB);

if ((VERB[L-2] != 'e') || (VERB[L-1] != 'r'))
printf("\"%s\" n'est pas un verbe du premier groupe.\n",VERB);

else
{
VERB[L-2]='\0';

for (I=0 ; I<6 ; I++)
printf("%s %s%s\n",SUJ, VERB, TERM);
}

return 0;
}


It works just fine without any *pointers, structures and so on.

Just 2 dimensional arrays made it.

I guess this is the end of this discussion. Whatever the output, I
learned a lot from any discussion i initiate or that i can read from
others. I should soon be posting a lot as i'm about to enter pointers
chapter.
There are still a few things i'd like to learn on how to do without
pointers. By the way, how is it possible to achieve a good level in
programming without getting things done with simplicity? I'm not a
simple person and as i can understand that, i will not be a good
programmer or not in an easy way. However, as C is a lot about
pointers, i might find things easier when i know more about them.

Thx,

Pascal
 
B

Ben Bacarisse

As i said earlier, i'm new to programming. Like any other subjects a
good understanding of the basics will help later on unlike starting
straight with :
while ( *pc++ = *pb++ ) ; which is for me an obscure way to copy
elements from pb to pc (formerly declared as : char buffer[128] ; char
clearbuf[128] ; char *pb = buffer ; char *pc = clearbuf and sp on ...
it took me a while to understand this...arghhhh ! I think this is
still simple in the pointers chapter, pointers to functions and
memalloc will make things a lot more spicy.

About this discussion, you all should have a $10 fine for overspeed.

Ouch! OK. You go at whatever speed you need to. It is hard to hold
back and not suggest improvements (see below!) but you can always
ignore a post.
I
really was looking for a "non-pointer" way to achieve this. Here it is
a few html pages later on the tutorial site i'm learning from :

You wanted a "non-star" way to do it -- you can't use arrays without using
pointers; it is just that your tutorial has kept this from you.

<snip code>

Just for your consideration, here is how I would write the same
program that you wrote. There are lots of little changes. Some are
significant (limiting the length of a string scan is a very good thing
to get into the habit of doing) and others are just stylistic
alternatives. Just seeing an alternative can sometimes be helpful:

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

int main(void)
{
char verb[20];
int len;

printf("Verbe : ");
scanf("%19s", verb);
len = strlen(verb);

if (strcmp(verb + len - 2, "er") != 0)
printf("\"%s\" n'est pas un verbe du premier groupe.\n", verb);
else {
char *suj[] = {"je", "tu", "il", "nous", "vous", "ils"};
char *term[] = {"e", "es", "e", "ons", "ez", "ent"};
int i;
for (i = 0; i < sizeof suj / sizeof suj[0]; i++)
printf("%4s %.*s%s\n", suj, len - 2, verb, term);
}
return 0;
}
 
J

James Kuyper

About this discussion, you all should have a $10 fine for overspeed. I
really was looking for a "non-pointer" way to achieve this. ...

If you think you're not ready yet for pointers, then you should be
attempting much simpler things. Pointers are so fundamental to C that
you shouldn't be trying anything this complicated until after you're
learned a lot more about them.

To build on your analogy, you're trying to drive to the local
supermarket with your car still in first gear, and feel that we're
"overspeeding" you by suggesting that you should at least shift into 2nd
gear. If your car has a manual transmission, that analogy might be
entirely lost on you - sorry!
#include <stdio.h>
#include <string.h>


int main(void)
{

char SUJ[6][5] = {"je","tu","il","nous","vous","ils"};
char TERM[6][5] = {"e","es","e","ons","ez","ent"};
char VERB[20];
int L;
int I;

printf("Verbe : ");
scanf("%s", VERB);


L=strlen(VERB);

if ((VERB[L-2] != 'e') || (VERB[L-1] != 'r'))
printf("\"%s\" n'est pas un verbe du premier groupe.\n",VERB);

else
{
VERB[L-2]='\0';

for (I=0 ; I<6 ; I++)
printf("%s %s%s\n",SUJ, VERB, TERM);
}

return 0;
}


It works just fine without any *pointers, structures and so on.

Just 2 dimensional arrays made it.


printf is automatically converted to a pointer to a function in this
context. VERB is automatically converted to a pointer to char in
virtually every context where you've used it except the declaration.
SUJ and TERM are both pointers to char.

You might want to reconsider the feasibility of avoiding pointers which
using C.
 
B

Barry Schwarz

Hi,

I understand C vocabulary can rely on pointer for anything... That's
why i understand as well from studying for the last 3-4 months / 20-40
hrs a week, i haven't really started learning C since i was trying to
keep myself away from pointers.

As i said earlier, i'm new to programming. Like any other subjects a
good understanding of the basics will help later on unlike starting
straight with :
while ( *pc++ = *pb++ ) ; which is for me an obscure way to copy
elements from pb to pc (formerly declared as : char buffer[128] ; char
clearbuf[128] ; char *pb = buffer ; char *pc = clearbuf and sp on ...
it took me a while to understand this...arghhhh ! I think this is
still simple in the pointers chapter, pointers to functions and
memalloc will make things a lot more spicy.

About this discussion, you all should have a $10 fine for overspeed. I
really was looking for a "non-pointer" way to achieve this. Here it is
a few html pages later on the tutorial site i'm learning from :


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


int main(void)
{

char SUJ[6][5] = {"je","tu","il","nous","vous","ils"};
char TERM[6][5] = {"e","es","e","ons","ez","ent"};
char VERB[20];
int L;
int I;

printf("Verbe : ");

The argument to printf is a pointer.
scanf("%s", VERB);

Both arguments to scanf are pointers.
L=strlen(VERB);

The argument to strlen is a pointer.
if ((VERB[L-2] != 'e') || (VERB[L-1] != 'r'))
printf("\"%s\" n'est pas un verbe du premier groupe.\n",VERB);

Both arguments to printf are pointers.
else
{
VERB[L-2]='\0';

for (I=0 ; I<6 ; I++)
printf("%s %s%s\n",SUJ, VERB, TERM);


All four arguments to printf are pointers.
}

return 0;
}


It works just fine without any *pointers, structures and so on.

Only if you consider 10 pointers to be "without any." But there are
no structures.

What does the "so on" mean? If you want to use only a very trivial
subset of the language, be prepared to write only trivial programs.
Just 2 dimensional arrays made it.

I guess this is the end of this discussion. Whatever the output, I
learned a lot from any discussion i initiate or that i can read from
others. I should soon be posting a lot as i'm about to enter pointers
chapter.
There are still a few things i'd like to learn on how to do without
pointers. By the way, how is it possible to achieve a good level in

C without pointers has all the utility of accounting without
arithmetic.
 
M

Morris Keesan

....
Just seeing an alternative can sometimes be helpful: ....
char verb[20]; ....
scanf("%19s", verb);

This is just asking for trouble, when you modify the declaration of verb.
If instead, one writes

/* Pascal, look away: this is definitely overspeed */

#define VERB_LEN 19
#define Scan_String(len, array) scanf("%" #len "s", array)

...
char verb[VERB_LEN + 1]
...
Scan_String(VERB_LEN, verb)

then the scanf call remains correct if you change the length of verb.
 
F

Flash Gordon

(e-mail address removed) wrote:

About this discussion, you all should have a $10 fine for overspeed. I
really was looking for a "non-pointer" way to achieve this. Here it is
a few html pages later on the tutorial site i'm learning from :

Others have pointed out lots of pointers in the solution, I'll point out
some real errors in it.
#include <stdio.h>
#include <string.h>


int main(void)
{

char SUJ[6][5] = {"je","tu","il","nous","vous","ils"};
char TERM[6][5] = {"e","es","e","ons","ez","ent"};
char VERB[20];
int L;
int I;

printf("Verbe : ");
scanf("%s", VERB);

Someone else pointed out that you can overflow the buffer here. I don't
know about French, but in Britain there are place names a lot more than
20 characters.

However, if you don't overflow the buffer but instead enter a single
character here...
L=strlen(VERB);

L will be set to one...
if ((VERB[L-2] != 'e') || (VERB[L-1] != 'r'))

So the above "underflows" the buffer, I.e. reads before the start of it.
printf("\"%s\" n'est pas un verbe du premier groupe.\n",VERB);

else
{
VERB[L-2]='\0';

The above therefor could write to the location before the start of the
buffer.
for (I=0 ; I<6 ; I++)
printf("%s %s%s\n",SUJ, VERB, TERM);
}

return 0;
}


It works just fine without any *pointers, structures and so on.


It works as long as you restrict your input. I would be a little dubious
about a tutorial which has such a fundamental error as reading/writing
before the start of an array.
Just 2 dimensional arrays made it.

I guess this is the end of this discussion. Whatever the output, I
learned a lot from any discussion i initiate or that i can read from
others. I should soon be posting a lot as i'm about to enter pointers
chapter.
There are still a few things i'd like to learn on how to do without
pointers. By the way, how is it possible to achieve a good level in
programming without getting things done with simplicity? I'm not a
simple person and as i can understand that, i will not be a good
programmer or not in an easy way. However, as C is a lot about
pointers, i might find things easier when i know more about them.


Pointers are simple, they point at things (or not), just like fingers.
Step out of your house with your hand down by your side. Your finger has
been initialised to a null pointer (by putting it down by your side).
Now point it to the first house, and you have set it to point at
something. Move it three houses along, and you have added three to it.
Get someone to demolish the entire street, and it is now a wild pointer
that does not point at a house!

Simple.

The only thing is you have specialised fingers for pointing at different
types of things.

In my opinion most of the trouble people have with pointers is either
thinking they are complex or thinking you need to understand all the low
level detail.
 
N

Nick Keighley

On Mon, 17 Aug 2009 17:09:30 -0700 (PDT), "(e-mail address removed)"

probably a bad idea. I think you've got a little scared of them.
And clc isn't helping you at all!

Come on guys take your pedantry busting meds!

Try to distinguish between *explicit* use of pointers; which is what
bpascal mans when he says "pointers" and *implicit* use of pointers
which is what you lot mean. You can call printf() and use arrays
without any knowledge of pointers.

K&R doesn't mention pointers until chapter 5!! That's page 93!!
By that time they've done a huge chunk of the language.


it *still* looks that way to me and I've been programming in C for a
decade
or so. I know (and use) the idiom but I'd never claimed it was
obvious.

(formerly declared as : char buffer[128] ; char
clearbuf[128] ; char *pb = buffer ; char *pc = clearbuf and sp on ....
it took me a while to understand this...arghhhh ! I think this is
still simple in the pointers chapter, pointers to functions and
memalloc will make things a lot more spicy.

that's malloc() (or calloc() or realloc())

About this discussion, you all should have a $10 fine for overspeed. I
really was looking for a "non-pointer" way to achieve this. Here it is
a few html pages later on the tutorial site i'm learning from :
#include <stdio.h>
#include <string.h>
int main(void)
{
   char SUJ[6][5]  = {"je","tu","il","nous","vous","ils"};
   char TERM[6][5] = {"e","es","e","ons","ez","ent"};
   char VERB[20];
   int L;
   int I;

I don't like uppercase for identifiers. Traditionally all UC is
reserved for
macros

The argument to printf is a pointer.

but not knowing this doesn't do any harm. You'd put toddlers up
against Mr.Bolt

exactly!


Only if you consider 10 pointers to be "without any."  But there are
no structures.

He no more needs to know there are pointers there than he needs to
know which registers are being used. Learn to do abstraction it can
be a useful skill if you want to continue programming.

What does the "so on" mean?  If you want to use only a very trivial
subset of the language, be prepared to write only trivial programs.

4 chapters of k&r doesn't look so trivial to me!

C without pointers has all the utility of accounting without
arithmetic.
bollocks

yes pointers are important but you are also right about learning at
your own speed.

[insert stupid analogy about breaking the car into individual atoms
and carrying it to the shops in a paper bag]


:)


--
Nick Keighley

Avoid hyperbole at all costs,
it's the most destructive argument on the planet.
- Mark McIntyre in comp.lang.c
 
N

Nick Keighley

Pointers are simple, they point at things (or not), just like fingers.
Step out of your house with your hand down by your side. Your finger has
been initialised to a null pointer (by putting it down by your side).
Now point it to the first house, and you have set it to point at
something. Move it three houses along, and you have added three to it.
Get someone to demolish the entire street, and it is now a wild pointer
that does not point at a house!

Simple.

The only thing is you have specialised fingers for pointing at different
types of things.

In my opinion most of the trouble people have with pointers is either
thinking they are complex or thinking you need to understand all the low
level detail.

I don't think the syntax helps. I found the syntax confusing as a
beginner
and I'd used Pascal, BCPL and assembler. I had a clear model of the
hardware.
 
B

Ben Bacarisse

Nick Keighley said:
K&R doesn't mention pointers until chapter 5!! That's page 93!!
By that time they've done a huge chunk of the language.

My copy does so in chapter 1, on page 24, though to be fair this is a
forward reference to the "the details" in chapter 5.

<snip>
 
B

Ben Bacarisse

Morris Keesan said:
On Mon, 17 Aug 2009 21:16:59 -0400, Ben Bacarisse

...
Just seeing an alternative can sometimes be helpful: ...
char verb[20]; ...
scanf("%19s", verb);

This is just asking for trouble, when you modify the declaration of verb.
If instead, one writes
Agreed.

/* Pascal, look away: this is definitely overspeed */

#define VERB_LEN 19
#define Scan_String(len, array) scanf("%" #len "s", array)

...
char verb[VERB_LEN + 1]
...
Scan_String(VERB_LEN, verb)

then the scanf call remains correct if you change the length of
verb.

But consider the context. I wanted to show some tidying up with
minimal language features. Stringification in a macro body is not a
minimal feature!
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top