B
Ben Bacarisse
Malcolm McLean said:OTOH if(Nstates = 52) is correct Fortran with the obvious meaning.
Since when?
Malcolm McLean said:OTOH if(Nstates = 52) is correct Fortran with the obvious meaning.
(e-mail address removed) writes:char **a;<snip>
int numberOfPointers = 5;
a = (char **) malloc ( numberOfPointers * ( sizeof(char * ) ) );
if ( a!= NULL )
printf( "the array, a, points to declare space\n\n" );
char *b[] = { "one", "two", "three", "four", "five" };
a = b;
It's possible that you don't know what this assignment does. It only
replaces one pointer with another. The pointer value that used to be in
'a' is lost so you can no longer access the memory that was allocated.
in particular, you can't ever free it now.
<snip>
Ben, so a=b; , does not assign a to point to whatever b points to?
it replaces?
I thought it was having a point to the same memory location.
How would you do that? How would you have a point to b? what is the
syntax?
char **a;
int numberOfPointers = 5;
a = (char **) malloc ( numberOfPointers * ( sizeof(char * ) ) );
if ( a!= NULL )
printf( "the array, a, points to declare space\n\n" );
char *b[] = { "one", "two", "three", "four", "five" };
a = b;
Ben, so a=b; , does not assign a to point to whatever b points to?
it replaces?
I thought it was having a point to the same memory location.
How would you do that? How would you have a point to b? what is the syntax?
(e-mail address removed) writes:char **a;int numberOfPointers = 5;
a = (char **) malloc ( numberOfPointers * ( sizeof(char * ) ) );
if ( a!= NULL )
printf( "the array, a, points to declare space\n\n" );
char *b[] = { "one", "two", "three", "four", "five" };
a = b;Ben, so a=b; , does not assign a to point to whatever b points to?It's possible that you don't know what this assignment does. It only
replaces one pointer with another. The pointer value that used to be in
'a' is lost so you can no longer access the memory that was allocated.
in particular, you can't ever free it now.
<snip>
Yes, it does exactly that: after the assignment a and b point to the
same place. If that's what you wanted, you got it right, but it looks
like a very odd thing to do. Why allocate space will malloc, only to
loose the pointer forever a couple of lines further down?
It replaces one pointer (the pointer to the malloc'd data) that is init replaces?
'a' with another (a pointer to the first element of the array 'b').
I suspect the problem here is English not C.
Exactly as you have written it. Had I know that's what you wanted to
do, I'd have made a different remark: "why allocate that storage and
then overwrite the poimter to it without every using it?".
Keith Thompson said:I'm sure part of it is that I learned Pascal before I learned C.
But when I see "if (ptr)", I have to mentally translate it to
"if (ptr != NULL)".
I know why Yoda conditions are used, and it's a valid reason.
But would you even consider writing
if (0 == strcmp(s1, s2))
if it weren't for the "==" vs. "=" issue?
[...] the only argument ever presented for Yoda Conditions is
protection against the possibility of mistyping = instead of ==. Such
protection is only needed when one of the operands is an lvalue; such
protection is only possible if the other is not an lvalue. It is neither
needed nor possible in this case.
The relevant property is lvalueness.[...]If you're writing code so obfuscated that it leaves you uncertain which[...]
expressions are lvalues, you've got bigger problems than can be dealt
with by using Yoda conditions.Nothing that occurs at runtime can change whether or not a given[...]
expression is an lvalue.
have to like it, but they do have live with it - just as people who
write /proper/ English have to accept that the developers of the C
language can't spell "maths" properly.
Let me see if I understand your definition of Yoda condition correctly:
A Yoda condition (X==Y) is a condition that is protected against
accidental assignment, while the reverse condition (Y==X) is unprotected.
That means:
- X is an expression that is not a modifiable lvalue;
so that (X=Y) violates 6.5.16, constraint 2.
- Y is a modifiable lvalue;
so that (Y=X) is an accidental but valid assignment.
Examples:
int var;
int const con;
char *s1, *s2;
(42 == var); /* Yoda condition */
(con == var); /* Yoda condition */
(42 == var+1); /* not Yoda condition */
(42 == con); /* not Yoda condition */
(0 == strcmp(s1, s2)); /* not Yoda condition */
Right?
Keith Thompson said:James Kuyper said:On 08/24/2013 03:05 PM, Sharwan Joram wrote: [...]if ( NULL == parameters[parametercount]){
This is what's known as a "Yoda conndition"
<http://en.wikipedia.org/wiki/Yoda_Conditions>. I know that a lot of
programmers like them, and for somewhat valid reasons, but personally I
find them jarring and unnecessary.
Ike Naar said:James Kuyper said:On 08/24/2013 03:05 PM, Sharwan Joram wrote: [...]
if ( NULL == parameters[parametercount]){
This is what's known as a "Yoda conndition"
<http://en.wikipedia.org/wiki/Yoda_Conditions>. I know that a lot of
programmers like them, and for somewhat valid reasons, but personally I
find them jarring and unnecessary. Personally, I'd write that as:
if (parameters[parametercount] == NULL) {
Does it matter? The == operator is symmetric, (X==Y) == (Y==X).
If (X==Y) is jarring and unnecessary, then, for symmetry reasons
(Y==X) is unnecessary and jarring.
Keith Thompson said:If your only criterion for choosing between two different ways
of writing something is that they have the same language-level
semantics, that should mean you have no preference between arr
and i[arr], or between
if (foo) {
/* ... */
}
and
if (!foo); else {
/* ... */
}
Tim Rentsch said:I find this interesting and also rather astonishing.
(Not unbelievable, just astonishing.)
To offer a point of comparison, I also learned Pascal
before I learned C, but immediately took to the C-isms
'if(ptr)' and 'if(!ptr)' without any difficulty. When
I see something like 'if (ptr != NULL)' it almost always
looks awkward or somewhat contrived. Not meaning to
imply anyone else should have this reaction, just that it
is the reaction I have myself.
Ike Naar said:Do you find (42 + x) more or less awkward than (x + 42), and why?
If you don't care, then what's the fundamental difference between
the + operator and the == operator?
If you have the self control to write perverted yoda code, then
surely you've got the self control to not type '=' when you mean
'==',
James Kuyper said:On 08/27/2013 09:25 AM, Phil Carmody wrote:
...
I see no basis for the "perverted" label; it's nothing more than a style
issue based upon linguistically-based prejudices.
It takes far less self control to routinely use Yoda conditionals, than
it takes to never type '=' when you mean '=='. That's primarily because
of the difference between "routinely" and "never". I'm not sure that
there's any one remotely human who has sufficient self control to never
commit such a mistake; I'm willing to concede that you might not be
covered by that statement.
You notice that dangling comma at the end of the bit you quoted?
That comma implies there's more to that sentence.
Phil Carmody said:Tally one more for that point of view here. However, I don't
physically wince when I see (ptr != NULL), unlike some other
jarring and unnecessary constructs.
If the English had not wanted to lose control of the English language,
they shouldn't have conquered and colonized so many countries and forced
them to use that language. The English now constitute a minority among
native speakers of English. There's many more native speakers of US
English alone than there are of all of the British varieties of English.
Get over it.
Ike Naar said:There is no reason to write it that way for the "==" vs. "=" issue.
if (strcmp(s1, s2) == 0)
is already safe, because
if (strcmp(s1, s2) = 0) /* oops, mistyped "==" */
is not valid C.
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.