some queries

C

c.lang.myself

Last night, while making a linked list program following questions
cropped in my mind..

QUERY-1:
I know any variable declared as extern or in global scope is
initialized with zero by default. Does it also apply to pointers so
that a global pointer would be initilaized with NULL.

QUERY-2:
I know various assignment operator +=, -=,*= ,/= but if p is a
pointer than why p->=next is not valid. and we have to use p=p->next

QUERY-3:

I got following run time error in using scanf function. I am unable to
find with what is wrong in my scanf usage..

scanf:floating point format not linked abnormal program termination


thanks,
 
G

Guest

Last night, while making a linked list program following questions
cropped in my mind..

QUERY-1:
I know any variable declared as extern or in global scope is
initialized with zero by default. Does it also apply to pointers so
that a global pointer would be initilaized with NULL.

yes. Note that a null pointer may not be all-bits-zero but
a file scope pointer variable (that includes what you are talking
about) will be initialised to null. This applies to statics as well.
QUERY-2:
I know various assignment operator +=, -=,*= ,/=   but if p is a
pointer than why p->=next is not valid. and we have to use p=p->next

no real reason. Ask Dennis Richie.
QUERY-3:

I got following run time error in using scanf function. I am unable to
find with what is wrong in my scanf usage..

scanf:floating point format not linked   abnormal program termination

this is really a problem with your implementaion (compiler). It
thinks you aren't using floating point so it doesn't need to link
in the floating point part of the math library. You need to consult
your documentation. Try adding a call to some floating point math
(eg. sin()) or add -lm to your linker options
 
C

Chris Dollin

Richard said:
(e-mail address removed) said:

I don't know for sure, but I'd imagine there is no reason other than that
nobody ever put that syntax into the language.

And that there would have to be special evaluation rules for the
right-hand-side of the ->= operator, since a member name on its
own isn't an expression.
 
K

Keith Thompson

Chris Dollin said:
And that there would have to be special evaluation rules for the
right-hand-side of the ->= operator, since a member name on its
own isn't an expression.

True, but the -> and . operators already have that problem. I don't
think a ->= operator would cause any real difficulties, it probably
just wasn't thought of, or wasn't thought to be worthwhile.

Note that a .= operator wouldn't make any sense, since a struct or
union can't be a member of itself.

But if you want a hypothetical operator that makes even less sense,
imagine ==, where x == y means x = (x = y). :cool:}
 
G

Guest

But if you want a hypothetical operator that makes even less sense,
imagine ==, where x == y means x = (x = y).  :cool:}

and since x++ means x = x + 1
it follows that
x** means x = x * 1
x// means x = x / 1
x&& means x = x & 1
x|| means x = x | 1
x^^ means x = x ^ 1

I'm not sure if ?: can be extended
 
J

James Kuyper

Last night, while making a linked list program following questions
cropped in my mind..

QUERY-1:
I know any variable declared as extern or in global scope is

The correct term is "file scope", not global scope. What 'extern' and
'file scope' have in common is that they both give the variable static
storage duration.
initialized with zero by default. Does it also apply to pointers so
that a global pointer would be initilaized with NULL.

Almost. There a subtle issue with your terminology.
An pointer object with static storage duration, if not explicitly
initialized in it's definition, will be implicitly initialized to a null
pointer. However, "null" and "NULL" have two different meanings. "null"
is an adjective indicating that the pointer doesn't point at anything.

"NULL" is the name of a standard macro which is guaranteed to expand to
an expression which qualifies as null pointer constant. As a result, it
is treated as a null pointer if it occurs in a pointer context, even if
the expression doesn't have pointer type. There's a huge variety of
expressions that qualify as null pointer constants: 0, 0.0, 0U, 0.F,
'\0', (5-5), (void*)0, etc. However, the two most likely definitions for
NULL are 0 and (void*)0.
QUERY-2:
I know various assignment operator +=, -=,*= ,/= but if p is a
pointer than why p->=next is not valid. and we have to use p=p->next

I don't know whether that possibility was even considered, much less why
it was rejected, if it was considered.
QUERY-3:

I got following run time error in using scanf function. I am unable to
find with what is wrong in my scanf usage..

scanf:floating point format not linked abnormal program termination

If we don't see your scanf() usage, we can't be sure what's wrong with
it either. Please show us a complete program that will actually compile,
as presented, and which demonstrates this problem. I've got a feeling
this has something to do with your compiler, so please identify which
compiler you're using, and what options you selected when compiling this
program.
 
V

viza

Hi

and since x++ means x = x + 1
it follows that
x** means x = x * 1
x// means x = x / 1
x&& means x = x & 1
x|| means x = x | 1
x^^ means x = x ^ 1

I would love a boolean toggle operator, like

x= !x;

x!=; perhaps.

perhaps we can ask for these operators for christmas.

viza
 
J

James Kuyper

James Kuyper wrote:
....
the expression doesn't have pointer type. There's a huge variety of
expressions that qualify as null pointer constants: 0, 0.0, 0U, 0.F,
'\0', (5-5), (void*)0, etc. However, the two most likely definitions for
NULL are 0 and (void*)0.

My apologies: 0.0 and 0.F don't belong in that list. I don't think I was
fully awake when I wrote it.
 
P

Phil Carmody

Chris Dollin said:
And that there would have to be special evaluation rules for the
right-hand-side of the ->= operator, since a member name on its
own isn't an expression.

As an aside, the relative workingnesses of the following expressions
has always amused me. It's far from intuitive to the average C dabbler.

typedef struct
{
int i;
int j;
} foo_s;

foo_s f1, f2;
foo_s *pf1=&f1, *pf2=&f2;

void movefoo(int foonum)
{
( foonum>0? f2: f1 ). j = ( foonum>0? f2: f1 ). i;
(*(foonum>0?&f2:&f1)). j = (*(foonum>0?&f2:&f1)). i;
( foonum>0?pf2:pf1 )->j = ( foonum>0?pf2:pf1 )->i;
}


(Untested code, but the constructs and subtle differences between
them should be clear.)

Phil
 
P

Phil Carmody

Keith Thompson said:
Note that a .= operator wouldn't make any sense, since a struct or
union can't be a member of itself.

But if you want a hypothetical operator that makes even less sense,
imagine ==, where x == y means x = (x = y). :cool:}

I'm more boggled by the idea of a unary &= ? Makes a unary -=
seem positively tame. (But no, I have no idea which way round
the syntax for the latter should be: -=foo; or foo-=;)
Ternary ?= : would be pretty perverse too.

(I guess there's an another intermediate step before we need to
worry about discussing the ^^= operator...)

Phil
 
B

Ben Bacarisse

and since x++ means x = x + 1

++x is more similar since x++ returns the old value.
it follows that
x** means x = x * 1
x// means x = x / 1
x&& means x = x & 1
x|| means x = x | 1
x^^ means x = x ^ 1

&&, || and // all have their own syntactic problems, of course. (I
know you are not being entirely serious).

One thing I miss in C, that applies more to a hypothetical ->=
compound than the other assignment operators, is so called
"displacement operators".

To invent a syntax, let's just reverse the order so we now also permit
x =+ 1 and so on[1]. This sets x to x + 1 but returns the /prior/
value. I other words, x =+ 1 is like x++ (just like x += 1 is like
++x).

These are not hugely very useful, but =-> is lovely since it permits
constructs like

while (list) free(list =-> next);

without any need to a temporary variable.

[1] This was, of course, how += used to be written in very old C, but
don't let that get in the way.
 
B

Bartc

Phil Carmody said:
I'm more boggled by the idea of a unary &= ? Makes a unary -=
seem positively tame. (But no, I have no idea which way round
the syntax for the latter should be: -=foo; or foo-=;)

I use a language which allows this, and it would be (in C form):

-=foo; /* foo=-foo */
~=foo; /* foo=~foo */
etc.

(unary & isn't included; it wasn't considered an operator. But a=&a isn't
particularly useful)
Ternary ?= : would be pretty perverse too.

You mean:

a=a?b:c;

shortened to:

a=?b:c; ?

Perhaps a?=b:c, but getting a bit silly now.
 
K

Keith Thompson

James Kuyper said:
James Kuyper wrote:
...

My apologies: 0.0 and 0.F don't belong in that list. I don't think I
was fully awake when I wrote it.

And (void*)0 isn't valid either. Consider sizeof NULL.

((void*)0) is valid (though a painfully literal reading of the
standard indicates that it's not, since the description of
parenthesized expressions doesn't say that a parenthesized null
pointer constant is a null pointer constant).
 
B

Barry Schwarz

Last night, while making a linked list program following questions
cropped in my mind..

QUERY-1:
I know any variable declared as extern or in global scope is
initialized with zero by default. Does it also apply to pointers so
that a global pointer would be initilaized with NULL.

extern doesn't define an object; it only declares it. At the place
where the object is defined, if it doesn't have initialization
specified it is initialized by default to 0, 0.0, or NULL as
appropriate.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top