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

Some Questions Asked in Interview

P: n/a
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.

Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?
Q2 . What is the use of array of pointers?
Q3 . What is the use of pointer to function ?
Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
?
Q5 . What is IOCTL Explain .
Q6 . How to create an interrupt service routine in C?
Q7 . What are the internals of a schedular ?
Q8 . The static variables are declared in heap or stack ?
Nov 14 '05 #1
Share this Question
Share on Google+
50 Replies

P: n/a

"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.
These are not all C questions, and can also have quite lengthy answers.
Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?
Q2 . What is the use of array of pointers?
Q3 . What is the use of pointer to function ?
Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
?
Q5 . What is IOCTL Explain .
Q6 . How to create an interrupt service routine in C?
Q7 . What are the internals of a schedular ?
Q8 . The static variables are declared in heap or stack ?


I won't give any answer becuase I feel the C questions are too vague.
Details on IOCTL can be found on the web, as can information about XON,
XOFF.
Nov 14 '05 #2

P: n/a


Jatinder wrote:
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.

Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array? many Q2 . What is the use of array of pointers? many Q3 . What is the use of pointer to function ? Useful to for anonymous reverse invocation. Most useful to invoke
application's function from library. Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
? It is not c question. Q5 . What is IOCTL Explain . man ioctl. You will get nice explantion Q6 . How to create an interrupt service routine in C? We can write only software interrupts and they are called signal
handlers. These are like normal c functions those take an integer (sig
num) as argument Q7 . What are the internals of a schedular ? not a c question. It requires vague explanation Q8 . The static variables are declared in heap or stack ?

heap

Nov 14 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dhananjaya Reddy Eadala wrote:
Jatinder wrote:

[snip]
Here are some questions on C/C++, OS internals? [snip] Q5 . What is IOCTL Explain .


man ioctl. You will get nice explantion


Actually, this is not a C question. IOCTLs are outside the scope of the
standard C language. /And/ the unix 'man' command isn't necessarily
available to the OP.

[snip]
Q8 . The static variables are declared in heap or stack ?


heap


Perhaps not. The standard C language neither recognizes the term 'heap',
nor the term 'stack', and thus the question doesn't actually relate to
C. As for specific implementations, both 'heap' and 'stack' are eligible
to store static variables, although IMHO 'stack' would take more work to
implement.


- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFApOLIagVFX4UWr64RAmmEAJ9RadHvs/U6vO+FZ5UGeptgAaedjACeP7Dg
uVMv7O4+6DMhR0ucYdz8Ecg=
=xRlU
-----END PGP SIGNATURE-----
Nov 14 '05 #4

P: n/a

"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.

Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?
Q2 . What is the use of array of pointers?
Q3 . What is the use of pointer to function ?
Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
?
Q5 . What is IOCTL Explain .
Q6 . How to create an interrupt service routine in C?
Q7 . What are the internals of a schedular ?
Q8 . The static variables are declared in heap or stack ?


I find it alarming that this trend of giving ridiculous quizzes and tests to
interviewee is increasing. When I was young and looking for a job,
companies that did this were few. I immediately walked out of any interview
were I was asked to take such a test. Regurgitation of facts does not prove
knowledge or wisdom, and is certainly no indication of skill. For someone
with an encyclopedic memory of the C or C++ standards, I would hire for a
one-time fee of $18--the amount I would need to just purchase a copy of the
standard (or whatever it currently costs).

This topic has come up before, and I think I said pretty much the something.
Someone replied with, "So how do you know they can do the job?" Well, there
are no guarantees, but you could try *talking* to them. I never minded
being asked questions in interviews. Isn't that a major part of the
process? Ask about college courses taken and projects done. If you are
interviewing an experienced professional, ask about previous work done. How
about what problems were encountered and how they were overcome. DON'T try
and sitting me in a room with 50 other nameless applicants and presume to
give me a test. That's a company interested in bodies and tests scores, not
cultivating good people. I'm also turned off by companies that call you up
and ask for college transcripts. Unless they have taken the time to
interview me and show an interest in hiring me, I always refused.

As a result, I love the company I work for. They treat me very well, and
there is always very smart, experienced people to learn from.

DrX

Nov 14 '05 #5

P: n/a
js*******@sancharnet.in (Jatinder) writes:
Q1 . What is the use of pointer to an array?
Such things are rarely useful. It is far more common to point to
the first element of an array.
Q2 . What is the use of array of pointers?
This question is so general that it is absurd. You can use
arrays of pointers for any number of things. For example, you
can store pointers to elements to be sorted within the array, and
then sorting will just require moving pointers instead of the
(presumably larger) elements themselves.
Q3 . What is the use of pointer to function ?
Polymorphism.
Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
?
Q5 . What is IOCTL Explain .
Q6 . How to create an interrupt service routine in C?
Q7 . What are the internals of a schedular ?
Q4 through Q7 are off-topic for comp.lang.c
Q8 . The static variables are declared in heap or stack ?


No.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Nov 14 '05 #6

P: n/a

"Jatinder" <js*******@sancharnet.in> wrote in message
Q1 . What is the use of pointer to an array? To pass the array to a function. Q2 . What is the use of array of pointers? To pass a list of arrays to a fucntion. Q3 . What is the use of pointer to function ? To pass a function to a function. Q4 . How to print through serial port? What is Flow Control(Xon,Xoff) Not a clue. Ports vary from platform to platform. ?
Q5 . What is IOCTL Explain . An acronym. Not a clue what it means. Q6 . How to create an interrupt service routine in C? You can't do this in standard C. Refer to your compiler documentation. Q7 . What are the internals of a schedular ? Huh? A clock and a series of function pointers it calls at appropriate
times, I would guess. Q8 . The static variables are declared in heap or stack ?

Neither. They are usually in their own area of memory which is allocated at
program initialisation.
Nov 14 '05 #7

P: n/a
> I find it alarming that this trend of giving ridiculous quizzes and tests to
interviewee is increasing.


Well, as for me, I don't particularly find it alarming that technical
questions be asked during job interviews - even if it looks like a
formal "test".
After all, education itself is not enough to judge someone's
skills. And related past work may be just lies or edulcorated stories -
unless the human resources staff checks them thoroughly, which is not
always possible nor practical.

What I find alarming in the above questions is that at least half of
them don't make any sense or are so general that they can't be answered
directly. I find alarming that it seems these questions have been
written by someone inept in the technical and software engineering
fields, and may disqualify skilled people and qualify people who can't
write actual software.
Nov 14 '05 #8

P: n/a

"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
Here are some questions on C/C++, OS internals? Q1 . What is the use of pointer to an array?
Little. Usually you don't point to arrays.

Q2 . What is the use of array of pointers?
Some. You may need to point to more than one thing at a time.

Q3 . What is the use of pointer to function ?
Immense. You can't call a function without pointing to it.

Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)?
Stick a pen through it. If you make a mess you need to control the ink flow.

Q5 . What is IOCTL Explain .
Sorry, I've seen some IOCTLs but never that one.

Q6 . How to create an interrupt service routine in C?
An infinite loop often does the trick.

Q7 . What are the internals of a schedular ?
The concept of an event is quite general. They aren't likely to have
common internals.

Q8 . The static variables are declared in heap or stack ?


False. They are typically declared near the beginning of the
program unless they are local to some function.

Nov 14 '05 #9

P: n/a
"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.

Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?
TO step through the array, or pass it by reference to another function.

Q2 . What is the use of array of pointers?
A list of strings of various sizes, or it could be an array of function
pointers. If one has 20 field validation functions (check_ssn, check_phone,
check_zip, etc.) you could put them in an array of pointers and attach the
pointer to a field on a form.

Q3 . What is the use of pointer to function ?
See above, you can put them in an array and call them like this: validate
(field, vals[check_zip]);

Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
That's what parallel ports are for. Who cares. I use USB.

Q5 . What is IOCTL Explain .
A dinosaur. DOS based device-driver accessor: ioctl (handle, cmd, &dx, &cx);
Time to upgrade.

Q6 . How to create an interrupt service routine in C?
signal ();
With this you can capture Ctrl-C, divide by zero, etc.

Q7 . What are the internals of a schedular ?
A state machine?

Q8 . The static variables are declared in heap or stack ?


Undefined, but the answer desired is probably heap.
Nov 14 '05 #10

P: n/a
Mabden wrote:

"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
I 'm a professional looking for the job.In interview these questions
were asked with some others which I answered.But some of them left
unanswered.Plz help.

Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?


TO step through the array,
or pass it by reference to another function.


A pointer to an array is also handy for mallocating
a multidimensional array, using only one pointer.

--
pete
Nov 14 '05 #11

P: n/a
Mabden wrote:
Q1 . What is the use of pointer to an array?

TO step through the array, or pass it by reference to another function.


You can't step through an array using a pointer to it.
If you increment a pointer to an array by 1 you will point
one past the end of the object (the array in this case) this
is defined, but you cannot dereference this pointer as that
leads to undefined behaviour.

A pointer to an array is rarely useful, I have never had the
need for one.

--
Thomas.

Nov 14 '05 #12

P: n/a
"Thomas stegen" <ts*****@cis.strath.ac.uk> wrote in message
news:2g************@uni-berlin.de...
Mabden wrote:
Q1 . What is the use of pointer to an array?

TO step through the array, or pass it by reference to another function.


You can't step through an array using a pointer to it.
If you increment a pointer to an array by 1 you will point
one past the end of the object (the array in this case) this
is defined, but you cannot dereference this pointer as that
leads to undefined behaviour.

A pointer to an array is rarely useful, I have never had the
need for one.


You are assuming I am incompetent. I would either keep a count on members or
add a terminating value to the array. Running off the end, or "off by one"
is a common error that one must plan to handle. Please don't tell me I can't
do something because you see a trivial restriction. When you say "you can't"
I think of going to the moon. You would have said the same thing back then,
"you can't"

I can.

--
Mabden
Nov 14 '05 #13

P: n/a
Mabden <ma****@sbcglobal.net> wrote:
"Thomas stegen" <ts*****@cis.strath.ac.uk> wrote in message
news:2g************@uni-berlin.de...
Mabden wrote:
>>Q1 . What is the use of pointer to an array?
>
>
> TO step through the array, or pass it by reference to another function.
>
You can't step through an array using a pointer to it.
If you increment a pointer to an array by 1 you will point
one past the end of the object (the array in this case) this
is defined, but you cannot dereference this pointer as that
leads to undefined behaviour.

A pointer to an array is rarely useful, I have never had the
need for one.

You are assuming I am incompetent. I would either keep a count on members or
add a terminating value to the array. Running off the end, or "off by one"
is a common error that one must plan to handle. Please don't tell me I can't
do something because you see a trivial restriction. When you say "you can't"
I think of going to the moon. You would have said the same thing back then,
"you can't" I can.


I guess you're overlooking the difference between "pointer to an array"
and "pointer to the first element of an array". The OP probably meant the
second but when you take it to mean what it really says a pointer to an
array would be something like

int array[ 10 ];
int ( * pointer_to_array_of_ten_ints )[ 10 ] = &array;

And that construct probably would tend to be used not that often;-)

Regrds, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #14

P: n/a
Lew Pitcher <Le*********@td.com> writes:

|> >> Q8 . The static variables are declared in heap or stack ?

|> > heap

|> Perhaps not. The standard C language neither recognizes the term
|> 'heap', nor the term 'stack', and thus the question doesn't actually
|> relate to C. As for specific implementations, both 'heap' and
|> 'stack' are eligible to store static variables, although IMHO
|> 'stack' would take more work to implement.

On the systems I'm familiar with, the answer would be neither.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #15

P: n/a
"Xenos" <do**********@spamhate.com> writes:

|> "Jatinder" <js*******@sancharnet.in> wrote in message
|> news:22*************************@posting.google.co m...
|> > I 'm a professional looking for the job.In interview these
|> > questions were asked with some others which I answered.But some of
|> > them left unanswered.Plz help.

|> > Here are some questions on C/C++, OS internals?
|> > Q1 . What is the use of pointer to an array?
|> > Q2 . What is the use of array of pointers?
|> > Q3 . What is the use of pointer to function ?
|> > Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
|> > ?
|> > Q5 . What is IOCTL Explain .
|> > Q6 . How to create an interrupt service routine in C?
|> > Q7 . What are the internals of a schedular ?
|> > Q8 . The static variables are declared in heap or stack ?

|> I find it alarming that this trend of giving ridiculous quizzes and
|> tests to interviewee is increasing.

It depends. The above questions aren't multiple choice, and can lead to
interesting and thoughtful answers. At least, some could.

In my experience, such "quizzes" are most useful if given orally and
interactively by someone familiar with the topic. Less formally, I
suspect that most people called on to vet a number of so-called
specialists will make up a list of questions to start with. After the
first few, of course, you play it by ear, adjusting to the apparent
competence of the person being interviewed.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #16

P: n/a
Thomas stegen <ts*****@cis.strath.ac.uk> writes:

|> Mabden wrote:

|> >>Q1 . What is the use of pointer to an array?
|> > TO step through the array, or pass it by reference to another
|> > function.

|> You can't step through an array using a pointer to it. If you
|> increment a pointer to an array by 1 you will point one past the end
|> of the object (the array in this case) this is defined, but you
|> cannot dereference this pointer as that leads to undefined
|> behaviour.

|> A pointer to an array is rarely useful, I have never had the need
|> for one.

Nonsense. If a is a two dimensional array, the expression a[i] returns
a pointer to an array.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #17

P: n/a
James Kanze <ka***@gabi-soft.fr> scribbled the following:
Thomas stegen <ts*****@cis.strath.ac.uk> writes:
|> Mabden wrote:
|> >>Q1 . What is the use of pointer to an array?
|> > TO step through the array, or pass it by reference to another
|> > function. |> You can't step through an array using a pointer to it. If you
|> increment a pointer to an array by 1 you will point one past the end
|> of the object (the array in this case) this is defined, but you
|> cannot dereference this pointer as that leads to undefined
|> behaviour. |> A pointer to an array is rarely useful, I have never had the need
|> for one. Nonsense. If a is a two dimensional array, the expression a[i] returns
a pointer to an array.


No it doesn't. It returns an array, which "decays" into a pointer when
used as a value. a itself "decays" into a pointer to an array when used
as a value. "Decays" does not mean "is", as Chris Torek will explain at
length given half the chance.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #18

P: n/a
James Kanze <ka***@gabi-soft.fr> wrote:
Thomas stegen <ts*****@cis.strath.ac.uk> writes: |> Mabden wrote: |> >>Q1 . What is the use of pointer to an array?
|> > TO step through the array, or pass it by reference to another
|> > function. |> You can't step through an array using a pointer to it. If you
|> increment a pointer to an array by 1 you will point one past the end
|> of the object (the array in this case) this is defined, but you
|> cannot dereference this pointer as that leads to undefined
|> behaviour. |> A pointer to an array is rarely useful, I have never had the need
|> for one. Nonsense. If a is a two dimensional array, the expression a[i] returns
a pointer to an array.


Well, lets look at that. As you probably know very well there aren't
any true multidimensional arrays in C, they are basically syntactic
sugar, made up from flat, one-dimensional arrays. And the rules for
such arrays, like for example

int a[ N ][ M ];

are to evaluate

a[ i ][ j ] := *( a + i * M + j )

and

a[ i ] := a + i * M

where 'a' itself is a pointer to the first element of that "two-dimen-
sional" array (not a "pointer to a two-dimensional array"). So the type
of 'a[i]' isn't "pointer to array" but (in this case) "pointer to int".

It seems to be a common misconception that with

int b[ 20 ];

'b' would be of type "pointer to array". But 'b' is of type "pointer to
int", pointing to the first element of the array, so you loosely can
call it an "array pointer". But only '&b' is of type "pointer to array
(of 20 ints)". You will see that easily when you create type of "pointer
to array (of 20 ints)" e.g. using a typedef to make things easier to
read (and not to offend Emmanuel sense of beauty;-):

typedef int ( *AP )[ 20 ];

AP a;

If you now try

int b[ M ];

a = b;

you will get a "assignment from incompatible pointer type" error, and
you must change that to

a = &b;

to get it working.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #19

P: n/a
Je***********@physik.fu-berlin.de writes:

|> James Kanze <ka***@gabi-soft.fr> wrote:
|> > Thomas stegen <ts*****@cis.strath.ac.uk> writes:

|> > |> Mabden wrote:

|> > |> >>Q1 . What is the use of pointer to an array?
|> > |> > TO step through the array, or pass it by reference to
|> > |> > another function.

|> > |> You can't step through an array using a pointer to it. If you
|> > |> increment a pointer to an array by 1 you will point one past
|> > |> the end of the object (the array in this case) this is
|> > |> defined, but you cannot dereference this pointer as that leads
|> > |> to undefined behaviour.

|> > |> A pointer to an array is rarely useful, I have never had the
|> > |> need for one.

|> > Nonsense. If a is a two dimensional array, the expression a[i]
|> > returns a pointer to an array.

|> Well, lets look at that. As you probably know very well there aren't
|> any true multidimensional arrays in C, they are basically syntactic
|> sugar, made up from flat, one-dimensional arrays.

Right.

I think you'll agree that if a is declared as a 2 diminsional array, the
expression a + i is a pointer to an array. I think I got carried away
with a[i] -- a[i] is an array, which decays to a pointer to the first
element in certain cases.

|> And the rules for such arrays, like for example

|> int a[ N ][ M ];
|>
|> are to evaluate

|> a[ i ][ j ] := *( a + i * M + j )

Not quite. It evaluates to *(a + i) + j. In this case, a decays to a
pointer to an array (the first element of a, and int[M]); i is added to
this pointer, which gives a pointer to an array, the i'th array in a.
This pointer then decays to a pointer to the first element, to which j
is added.

The expression you wrote will typically invoke undefined behavior
(except for special cases, e.g. where i and j equal 0).

|> and

|> a[ i ] := a + i * M

No. The expression a[i] is explicitly defined by the standard as *(a+i).

You never, never multiply by M. Behind the scenes, of course, the
compiler will multiply by sizeof(a[i]), i.e. the size of an int[M]. But
that is simply the implementation technique, and is no different than
when you have an array of struct's or whatever.

|> where 'a' itself is a pointer to the first element of that
|> "two-dimensional" array (not a "pointer to a two-dimensional
|> array").

Not quite. Read your first sentence: there are no two-dimensional
arrays, just arrays of arrays. So a decays to a pointer to the first
element of a, and the type of that element is an int[M].

|> So the type of 'a[i]' isn't "pointer to array" but (in this case)
|> "pointer to int".

No, the type of a[i] is int[M]. Not pointer to array, since we've
already dereferenced the pointer. But array of int.

Depending on what you do next, that array may decay into a pointer. Or
may not: what does sizeof( a[i] ) return?

|> It seems to be a common misconception that with

|> int b[ 20 ];

|> 'b' would be of type "pointer to array".

Not with anyone who has every used C. The common misconception seems to
be that b is a pointer to int, and that the declarations:
extern int * b ;
and
extern int b[] ;
are equivalent, and can be used interchangeably.

|> But 'b' is of type "pointer to int", pointing to the first element
|> of the array, so you loosely can call it an "array pointer".

No, that is the common misconception. b is of type array[20] of int.

|> But only '&b' is of type "pointer to array (of 20 ints)". You will
|> see that easily when you create type of "pointer to array (of 20
|> ints)" e.g. using a typedef to make things easier to read (and not
|> to offend Emmanuel sense of beauty;-):

|> typedef int ( *AP )[ 20 ];

|> AP a;

|> If you now try

|> int b[ M ];

|> a = b;

|> you will get a "assignment from incompatible pointer type" error, and
|> you must change that to

|> a = &b;

|> to get it working.

But I was talking about 2 dimentional arrays. And
int b[ 10 ][ 20 ] ;
AP a = b ;
should work.

Although my example went a step too far -- I should have said a+i,
rather than a[i] (which is, of course *(a+i)). But the point remains;
you cannot use two dimensional arrays in C without using pointers to
arrays. Even if typically, you don't write them explicitly.

Another obvious case, of course: what is the type of the parameter of:
int f( int a[5][5] ) ;

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #20

P: n/a
James Kanze wrote:
Thomas stegen <ts*****@cis.strath.ac.uk> writes:

|> Mabden wrote:

|> >>Q1 . What is the use of pointer to an array?
|> > TO step through the array, or pass it by reference to another
|> > function.

|> You can't step through an array using a pointer to it. If you
|> increment a pointer to an array by 1 you will point one past the end
|> of the object (the array in this case) this is defined, but you
|> cannot dereference this pointer as that leads to undefined
|> behaviour.

|> A pointer to an array is rarely useful, I have never had the need
|> for one.

Nonsense. If a is a two dimensional array, the expression a[i] returns
a pointer to an array.

Nonsense. C does not have multi dimensional arrays. Only arrays of
arrays. The expression of an array yields (not returns) the address
of its first element. It's all about decadence. :-)

int i = 0;

char a[5][10];

This defines (creates) exactly 50 bytes of memory. An array [5] of
array [10] of char.

Expressing a yields 'pointer to array [10] of char'. Expressing a[i]
yields 'pointer to char'. It would be &a[i] to get 'pointer to array
[10] of char'.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #21

P: n/a

On Sun, 16 May 2004, James Kanze wrote:

Je***********@physik.fu-berlin.de writes:
|> James Kanze <ka***@gabi-soft.fr> wrote:
|> > Thomas stegen <ts*****@cis.strath.ac.uk> writes:
|> > |> A pointer to an array is rarely useful, I have never had the
|> > |> need for one.

|> > Nonsense. If a is a two dimensional array, the expression a[i]
|> > returns a pointer to an array.

|> Well, let's look at that. [The above assertion is wrong.]

I think you'll agree that if a is declared as a 2 diminsional array, the
expression a + i is a pointer to an array.
Yes.

<big snip> Although my example went a step too far -- I should have said a+i,
rather than a[i] (which is, of course *(a+i)). But the point remains;
you cannot use two dimensional arrays in C without using pointers to
arrays. Even if typically, you don't write them explicitly.
That's just as nonsensical an argument as saying that most C
programs use non-trivial language parsers. You're arguing that
the program "uses" something supplied invisibly by the compiler
(or the language definition). I don't think this is what Thomas
meant.
Another obvious case, of course: what is the type of the parameter of:
int f( int a[5][5] ) ;


Pointer to array[5] of 'int', of course. But I have never written
a program that needed to know or use the type of the parameter in
such a function; normally, I use 'a' as if it were an array of arrays
of 'int'. Which the language syntax encourages me to do, of course.
I, like Thomas, have never to my knowledge used a declaration of the
form

foo (*bar)[baz];

in anything other than joke code. As he says, pointers to arrays
are hardly ever useful.

-Arthur
Nov 14 '05 #22

P: n/a
Je***********@physik.fu-berlin.de wrote:

It seems to be a common misconception that with

int b[ 20 ];

'b' would be of type "pointer to array". But 'b' is of type "pointer to
int", pointing to the first element of the array


If that were true, then sizeof(b) would equal sizeof(int *),
and it would be legal to write: int *const *p = &b; p[0][0] = 1; etc.
Nov 14 '05 #23

P: n/a
James Kanze wrote:
.... snip ...
In my experience, such "quizzes" are most useful if given orally
and interactively by someone familiar with the topic. Less
formally, I suspect that most people called on to vet a number of
so-called specialists will make up a list of questions to start
with. After the first few, of course, you play it by ear,
adjusting to the apparent competence of the person being
interviewed.


Or the inverse, with the interviewee adjusting to the apparent
competence of the interviewer. But beware 'apparent', s/he may be
setting traps.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
Nov 14 '05 #24

P: n/a
Mabden wrote:
"Thomas stegen" <ts*****@cis.strath.ac.uk> wrote in message
news:2g************@uni-berlin.de...
Mabden wrote:

Q1 . What is the use of pointer to an array?
TO step through the array, or pass it by reference to another function.

You can't step through an array using a pointer to it.
If you increment a pointer to an array by 1 you will point
one past the end of the object (the array in this case) this
is defined, but you cannot dereference this pointer as that
leads to undefined behaviour.

A pointer to an array is rarely useful, I have never had the
need for one.

You are assuming I am incompetent.


Your assumption about my assumption is wrong. I am now assuming
that you are ignorant about the difference between a pointer to
an array and a pointer to the first element in the array.
I would either keep a count on members or
add a terminating value to the array. Running off the end, or "off by one"
is a common error that one must plan to handle. Please don't tell me I can't
do something because you see a trivial restriction.
Don't attribute to stupidity or ignorance what can be attributed to
competence.

Or something :p

It so happens I do know what I am talking about.

If I have for example,

int a[5];

An array. If I want a pointer to that array I need to do something
along the lines of

int (*p)[5];

Now I can do,

p = &a;

I cannot do,

p = a;

Since that is not allowed.

Now doing p + 1 will not allow me to access the second element in
the array designated by a.
When you say "you can't"
I think of going to the moon. You would have said the same thing back then,
"you can't"

I can.


Oh, so I am a stubborn conservative? I am against progress? I
find this quite offensive. Back then I would have said "yes we can"
by the way. At least I think I would have, based on the evidence I can
glean from my attitudes to other similar things today.

--
Thomas.

Nov 14 '05 #25

P: n/a
Joe Wright <jo********@comcast.net> writes:

|> James Kanze wrote:

|> > Thomas stegen <ts*****@cis.strath.ac.uk> writes:
|> > |> Mabden wrote:
|> > |> >>Q1 . What is the use of pointer to an array?
|> > |> > TO step through the array, or pass it by reference to another
|> > |> > function.

|> > |> You can't step through an array using a pointer to it. If you
|> > |> increment a pointer to an array by 1 you will point one past
|> > |> the end of the object (the array in this case) this is
|> > |> defined, but you cannot dereference this pointer as that leads
|> > |> to undefined behaviour. A pointer to an array is rarely
|> > |> useful, I have never had the need for one.

|> > Nonsense. If a is a two dimensional array, the expression a[i]
|> > returns a pointer to an array.

|> Nonsense. C does not have multi dimensional arrays. Only arrays of
|> arrays.

OK. Formally, what C uses for multi dimensional arrays is in fact
arrays of arrays. Formally, what C uses for indexing is pointer
arithmetic.

|> The expression of an array yields (not returns) the address of its
|> first element.

No. An expression of array type yields an array. Which will convert
implicitly to a pointer in certain contexts only. And the conversion
obviously doesn't continue, because what you get after the decay is a
pointer, not an array.

And of course, if what you have is an array of arrays, what the decay
gives you is a pointer to an array.

|> It's all about decadence. :-)

|> int i = 0;

|> char a[5][10];

|> This defines (creates) exactly 50 bytes of memory. An array [5] of
|> array [10] of char.

|> Expressing a yields 'pointer to array [10] of char'.

You mean, like in &a, or sizeof( a )?

Of course, in contexts where the decay occurs... Well, that's exactly my
point. You have a pointer to an array.

|> Expressing a[i] yields 'pointer to char'.

No, it yields an array [10] of char, which isn't the same thing.

Try sizeof( a[i] ), for example.

|> It would be &a[i] to get 'pointer to array [10] of char'.

How about a + i?

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #26

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:

[...]
|> As he says, pointers to arrays are hardly ever useful.

And you missed my point. Explicit pointers to arrays are hardly ever
useful; like you, I find the declaration syntax complex enough to render
code using them pretty unreadable to many people. But that wasn't what
he said. Implicit pointers to arrays occur in just about any program
which uses "multi-dimensional arrays".

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #27

P: n/a
CBFalconer <cb********@yahoo.com> writes:

|> James Kanze wrote:

|> ... snip ...

|> > In my experience, such "quizzes" are most useful if given orally
|> > and interactively by someone familiar with the topic. Less
|> > formally, I suspect that most people called on to vet a number of
|> > so-called specialists will make up a list of questions to start
|> > with. After the first few, of course, you play it by ear,
|> > adjusting to the apparent competence of the person being
|> > interviewed.

|> Or the inverse, with the interviewee adjusting to the apparent
|> competence of the interviewer. But beware 'apparent', s/he may be
|> setting traps.

The role of an interview is both parties to learn more about the other.
Setting traps isn't normally an effective way to learn much; if you
can't establish some sort of open dialog during the interview, it is
probable that you won't be able to establish much of one afterwards
either. In which case, you're better off not taking the job (providing
the market and your finances give you a choice).

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #28

P: n/a
In <86************@lns-vlq-37-82-254-162-226.adsl.proxad.net> James Kanze <ka***@gabi-soft.fr> writes:
Thomas stegen <ts*****@cis.strath.ac.uk> writes:

|> Mabden wrote:

|> >>Q1 . What is the use of pointer to an array?
|> > TO step through the array, or pass it by reference to another
|> > function.

|> You can't step through an array using a pointer to it. If you
|> increment a pointer to an array by 1 you will point one past the end
|> of the object (the array in this case) this is defined, but you
|> cannot dereference this pointer as that leads to undefined
|> behaviour.

|> A pointer to an array is rarely useful, I have never had the need
|> for one.

Nonsense. If a is a two dimensional array, the expression a[i] returns
a pointer to an array.


That immediately decays into a pointer to its first element, with the
known exceptions.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #29

P: n/a
In <22*************************@posting.google.com> js*******@sancharnet.in (Jatinder) writes:
Here are some questions on C/C++, OS internals?
You should have posted *only* those related to C here.
Q1 . What is the use of pointer to an array?
In the rare cases when the size of the array is known at compile time, it
simplifies the dynamic allocation of bidimensional arrays. Extend to
multidimensional arrays where only the first dimension can be variable.
Q2 . What is the use of array of pointers?
Far too many to be worth enumerating. argv is a typical example.
Q3 . What is the use of pointer to function ?
Callbacks and encapsulation, among many others.
Q4 . How to print through serial port? What is Flow Control(Xon,Xoff)
?
Q5 . What is IOCTL Explain .
Q6 . How to create an interrupt service routine in C?
Q7 . What are the internals of a schedular ?
These are not C questions. C provides no support for interrupt handlers.
Q8 . The static variables are declared in heap or stack ?


Is this an ad litteram rendition of the original interview question?

Static variables are declared in neither heap nor stack, they are declared
in C source code files. And they are *allocated* whereever the
implementor chooses to, typically neither in heap (the malloc and friends
arena) nor in stack (automatic allocation arena and function call related
data).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #30

P: n/a
Je***********@physik.fu-berlin.de wrote:

(snip)
Well, lets look at that. As you probably know very well there aren't
any true multidimensional arrays in C, they are basically syntactic
sugar, made up from flat, one-dimensional arrays. And the rules for
such arrays, like for example


Well, at least C has the memory layout of a multidimensional
array. Java doesn't even have that. Java will allocate an
array of pointers, (called object reference variables, to
pretend that it doesn't have pointers), each pointing to an array.

What do they have to be to be true?

(snip)

-- glen

Nov 14 '05 #31

P: n/a
On 16 May 2004 14:12:43 -0700, in comp.lang.c , ol*****@inspire.net.nz (Old
Wolf) wrote:
Je***********@physik.fu-berlin.de wrote:

It seems to be a common misconception that with

int b[ 20 ];

'b' would be of type "pointer to array". But 'b' is of type "pointer to
int", pointing to the first element of the array
If that were true, then sizeof(b) would equal sizeof(int *),


Indeed. As far as I remember,, b is of type "array[20] of int".
and it would be legal to write: int *const *p = &b; p[0][0] = 1; etc.


But it is, provided you first pass b to a function... :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #32

P: n/a
On Fri, 14 May 2004 -0700, Ben Pfaff <bl*@cs.stanford.edu> wrote:
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle


Every computer were in the net
every programmes were happy with they not-low language
but
there was a multi-platform virus it was able to enter in any OS and
at date xx.xx.xxxx it checked if it was in every computer in the net
and the answer was *yes*: so started his job: destroy every executable
and source file wrote before 1990.

There was a disaster!
Assembler programmers were in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of
optimization problem, break glass.'

Programmers broken the glas and ask to rewrite all the lost software
because their Pc were OFF
The answer was: "we doing optimization only, and doesn't know were to
start for rewriting them". It was the end of the story.
Nov 14 '05 #33

P: n/a
"RoSsIaCrIiLoIA" <n@esiste.ee> wrote in message
news:gm********************************@4ax.com...
On Fri, 14 May 2004 -0700, Ben Pfaff <bl*@cs.stanford.edu> wrote:
"We put [the best] Assembler programmers in a little glass case in the hallway near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their work in languages most appropriate to the job at hand." --Richard Riehle


Every computer were in the net
every programmes were happy with they not-low language
but
there was a multi-platform virus it was able to enter in any OS and
at date xx.xx.xxxx it checked if it was in every computer in the net
and the answer was *yes*: so started his job: destroy every executable
and source file wrote before 1990.

There was a disaster!
Assembler programmers were in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of
optimization problem, break glass.'

Programmers broken the glas and ask to rewrite all the lost software
because their Pc were OFF
The answer was: "we doing optimization only, and doesn't know were to
start for rewriting them". It was the end of the story.


Or in English:

I remember the day when every computer in the world was connected to the
Internet. All the Operating Systems and the programs they ran were
high-level object-oriented masterpieces. All the programmers were high-level
object-oriented programmers. But, there was a multi-platform virus that no
one knew about. It was able to enter any OS on any platform.

And on date xx.xx.xxxx the virus checked, as it had been doing every day, to
see if every computer on the Internet was infected. The answer was, "Yes!"
So the virus began its task; Destroy every executable program and source
file that were written before the year 1990!

There was world wide disaster and panic! But not to worry - there was still
the little glass case at the end of the hall, by the fire escape, in the
basement. In it were the last of the Assembler Programmers. A little sign on
the glass read, "Break glass in case of optimization problem." And there was
a tiny steel hammer hanging next to the box, on a chain.

The modern high-level object-oriented programmers breathed a sigh of relief
as they wiped the dust from the hammer and broke the glass. They begged the
Assembler Programmers to rewrite all the lost software, because the Internet
was all but destroyed and their computers not longer worked.

The Assembler Programmers shuffled their feet, glanced at each other, and
replied, "Uh, we only do optimizations..."
Nov 14 '05 #34

P: n/a
On Fri, 14 May 2004 12:39:00 -0400, Xenos sez:
I find it alarming that this trend of giving ridiculous quizzes and tests
to interviewee is increasing. When I was young and looking for a job,
companies that did this were few. I immediately walked out of any
interview were I was asked to take such a test. Regurgitation of facts
does not prove knowledge or wisdom, and is certainly no indication of
skill. For someone with an encyclopedic memory of the C or C++ standards,
I would hire for a one-time fee of $18--the amount I would need to just
purchase a copy of the standard (or whatever it currently costs).


My first "real" programming job followed an interview that had little to
do with computers. Once the interviewer found out I shared his love for
the movie "Airplane!", we spent most of the time exchanging favorite
quotes from that movie. Somewhere in there he shoved a printout of some
Clipper code (the language I knew and he needed at the time) across the
desk and asked me to translate a few lines to English, which I did, but to
this day I'm convinced that I got the job because I knew the proper
response to "Surely you can't be serious!"

--
Peter B. Steiger
Cheyenne, WY
If you must reply by email, you can reach me by placing zeroes
where you see stars: wypbs_**3 at bornagain.com.

Nov 14 '05 #35

P: n/a
James Kanze wrote:
Joe Wright <jo********@comcast.net> writes:

|> James Kanze wrote:

|> > Thomas stegen <ts*****@cis.strath.ac.uk> writes:
|> > |> Mabden wrote:
|> > |> >>Q1 . What is the use of pointer to an array?
|> > |> > TO step through the array, or pass it by reference to another
|> > |> > function.

|> > |> You can't step through an array using a pointer to it. If you
|> > |> increment a pointer to an array by 1 you will point one past
|> > |> the end of the object (the array in this case) this is
|> > |> defined, but you cannot dereference this pointer as that leads
|> > |> to undefined behaviour. A pointer to an array is rarely
|> > |> useful, I have never had the need for one.

|> > Nonsense. If a is a two dimensional array, the expression a[i]
|> > returns a pointer to an array.

|> Nonsense. C does not have multi dimensional arrays. Only arrays of
|> arrays.

OK. Formally, what C uses for multi dimensional arrays is in fact
arrays of arrays. Formally, what C uses for indexing is pointer
arithmetic.

|> The expression of an array yields (not returns) the address of its
|> first element.

No. An expression of array type yields an array. Which will convert
implicitly to a pointer in certain contexts only. And the conversion
obviously doesn't continue, because what you get after the decay is a
pointer, not an array.
This seems argumentive but, the argument to sizeof is either an
object or a type. The compiler yields the size of it in bytes. The
argument to sizeof is never evaluated and is not an expression.
And of course, if what you have is an array of arrays, what the decay
gives you is a pointer to an array.

|> It's all about decadence. :-)

|> int i = 0;

|> char a[5][10];

|> This defines (creates) exactly 50 bytes of memory. An array [5] of
|> array [10] of char.

|> Expressing a yields 'pointer to array [10] of char'.

You mean, like in &a, or sizeof( a )?
You're missing something. &a is an address by definition. sizeof &a
is 4 at my house. sizeof a yields 50.
Of course, in contexts where the decay occurs... Well, that's exactly my
point. You have a pointer to an array.

|> Expressing a[i] yields 'pointer to char'.

No, it yields an array [10] of char, which isn't the same thing.

Try sizeof( a[i] ), for example.
You're much too hung up on sizeof. Arguments to sizeof are not
expressions. sizeof a[i] is 10. Interesting but unimportant.

char *cp = a[i];

is correct.

char (*ap)[10] = &a[i];

is also correct.

|> It would be &a[i] to get 'pointer to array [10] of char'.

How about a + i?


Looks like &a[i] to me. What do you think?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #36

P: n/a
Joe Wright wrote:
This seems argumentive but, the argument to sizeof is either an
object or a type. The compiler yields the size of it in bytes. The
argument to sizeof is never evaluated and is not an expression.


The argument to sizeof is never evaluated
but it can be an expression.
An object identifier, is an expression.

--
pete
Nov 14 '05 #37

P: n/a
pete <pf*****@mindspring.com> wrote:
Joe Wright wrote:
This seems argumentive but, the argument to sizeof is either an
object or a type. The compiler yields the size of it in bytes. The
argument to sizeof is never evaluated and is not an expression.
The argument to sizeof is never evaluated


This is true in C89. In C99, it may be necessary to evaluate a sizeof
argument - this is done if and only if it is a variable-length array.
but it can be an expression.


This is true. To be precise, it's a unary-expression.

Richard
Nov 14 '05 #38

P: n/a
Joe Wright <jo********@comcast.net> wrote:

<much snippage>
[...] the argument to sizeof is either an
object or a type. The compiler yields the size of it in bytes. The
argument to sizeof is never evaluated and is not an expression.
<snippage>Arguments to sizeof are not
expressions. sizeof a[i] is 10. Interesting but unimportant.


Nitpick:

Since sizeof is not a function, there are no such beasts like
"arguments to sizeof"; sizeof is an unary operator and therefore
has exactly one operand, which has to be either an expression (sic!)
or the parenthesized name of a type.

You're of course right in stating that expressions used as operands
of sizeof are not examined for their value ("evaluated"), but their
type - this therefore one of the notable exceptions to Chris Torek's
"The Rule" [1] (when being operand of the address operator is the
other exception).

This concept makes it BTW possible to evaluate the sizeof operator
at compile-time rather than run-time (again with one notable
exception: C99 variable-length arrays, for obvious reasons).
[1] "In any value context, an object of type 'array of T' is
converted to a value of type 'pointer to T', pointing to
the first element of that array."

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #39

P: n/a
In <2O********************@comcast.com> Joe Wright <jo********@comcast.net> writes:
This seems argumentive but, the argument to sizeof is either an
object or a type. The compiler yields the size of it in bytes. The
argument to sizeof is never evaluated and is not an expression.

^^^^^^^^^^^^^^^^^^^^^^^^
What's wrong with sizeof(1 + 1)?

Even worse, in C99 evaluation may be needed, when the argument is a VLA:

extern int n, m;
sizeof(int [n + m]);

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #40

P: n/a
In article <news:oc********************************@4ax.com >
Irrwahn Grausewitz <ir*******@freenet.de> writes:
You're of course right in stating that expressions used as operands
of sizeof are not examined for their value ("evaluated"),
[except in some cases in C99...]
but their type - this therefore one of the notable exceptions to
Chris Torek's The Rule" [1] (when being operand of the address
operator is the other exception).


Actually, this is why the first part is "In a value context". When
x appears after "&" or "sizeof" (i.e., &x or sizeof x), this is
*not* a "value context".

There are additional "not value context"s:

++x; /* x is required to name an object */
x = 9; /* x is required to name an object */

and in these cases, if "x" is an array, the code is invalid.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #41

P: n/a
Joe Wright <jo********@comcast.net> writes:

|> James Kanze wrote:

|> > Joe Wright <jo********@comcast.net> writes:
|> > |> James Kanze wrote:
|> > |> > Thomas stegen <ts*****@cis.strath.ac.uk> writes:
|> > |> > |> Mabden wrote:
|> > |> > |> >>Q1 . What is the use of pointer to an array?
|> > |> > |> > TO step through the array, or pass it by reference to
|> > |> > |> > another function.
|> > |> > |> You can't step through an array using a pointer to it.
|> > |> > |> If you increment a pointer to an array by 1 you will
|> > |> > |> point one past the end of the object (the array in this
|> > |> > |> case) this is defined, but you cannot dereference this
|> > |> > |> pointer as that leads to undefined behaviour. A pointer
|> > |> > |> to an array is rarely useful, I have never had the need
|> > |> > |> for one.
|> > |> > Nonsense. If a is a two dimensional array, the expression
|> > |> > a[i] returns a pointer to an array.
|> > |> Nonsense. C does not have multi dimensional arrays. Only
|> > |> arrays of arrays.

|> > OK. Formally, what C uses for multi dimensional arrays is in fact
|> > arrays of arrays. Formally, what C uses for indexing is pointer
|> > arithmetic.

|> > |> The expression of an array yields (not returns) the address of
|> > |> its first element.

|> > No. An expression of array type yields an array. Which will
|> > convert implicitly to a pointer in certain contexts only. And the
|> > conversion obviously doesn't continue, because what you get after
|> > the decay is a pointer, not an array.

|> This seems argumentive

No, just a simple statement of facts, drawn from the C standard.

|> but, the argument to sizeof is either an object or a type.

Since when? I use things like "sizeof( f() )" frequently. (Mostly in C++
template resolution, but it is perfectly legal in C as well.) Or what
about the frequent macro:
#define lenght( a ) (sizeof( a ) / sizeof( a[ 0 ] ))
to determine the number of elements in an array?

Roughly speaking, an expression has a type, and evaluates a value. When
used as the operand of sizeof, only the type is relevant.

And given something like:
int a[ 5 ][ 10 ] ;
sizeof( a[ 0 ] )
the expresion a[ 0 ] in the second line has a type int[10] (and sizeof
yields sizeof(int)*10).

This is C 101. I'm surprised that anyone who uses C isn't aware of
this.

|> The compiler yields the size of it in bytes. The argument to sizeof
|> is never evaluated and is not an expression.

The argument of sizeof is not evaluated. It is an expression.

|> > And of course, if what you have is an array of arrays, what the
|> > decay gives you is a pointer to an array.

|> > |> It's all about decadence. :-)
|> > |> int i = 0;
|> > |> char a[5][10];
|> > |> This defines (creates) exactly 50 bytes of memory. An array
|> > |> [5] of array [10] of char. Expressing a yields 'pointer to
|> > |> array [10] of char'.

|> > You mean, like in &a, or sizeof( a )?

|> You're missing something. &a is an address by definition.

But an address of what? What is the type of the pointer?

|> sizeof &a is 4 at my house. sizeof a yields 50.

|> > Of course, in contexts where the decay occurs... Well, that's
|> > exactly my point. You have a pointer to an array.

|> > |> Expressing a[i] yields 'pointer to char'.

|> > No, it yields an array [10] of char, which isn't the same thing.
|> > Try sizeof( a[i] ), for example.

|> You're much too hung up on sizeof.

Simply because that is the most common case where decay doesn't occur.
I could just have easily used &a[i] in my example, but people don't
offen take the address of arrays. Taking the address of an array could
be considered a bit of C exotica, where as sizeof... Anyone who doesn't
know about sizeof doesn't know C.

|> Arguments to sizeof are not expressions.

Since when?

|> sizeof a[i] is 10. Interesting but unimportant.

Except that the argument of sizeof in this expression is an expression,
which has array type.

|> char *cp = a[i];

|> is correct.

Because decay (implicit conversion) occurs in this context.

|> char (*ap)[10] = &a[i];

|> is also correct.

Because there is no decay (implicit conversion) when an expression of
array type is used as the argument of the & operator.

|> > |> It would be &a[i] to get 'pointer to array [10] of char'.

|> > How about a + i?

|> Looks like &a[i] to me. What do you think?

In C90, the two are different. There's a special exception in C99 which
changes the meaning of [] if it is the operand of &.

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #42

P: n/a
Chris Torek <no****@torek.net> writes:

|> In article <news:oc********************************@4ax.com >
|> Irrwahn Grausewitz <ir*******@freenet.de> writes:
|> >You're of course right in stating that expressions used as operands
|> >of sizeof are not examined for their value ("evaluated"),

|> [except in some cases in C99...]

|> >but their type - this therefore one of the notable exceptions to
|> >Chris Torek's The Rule" [1] (when being operand of the address
|> >operator is the other exception).

|> Actually, this is why the first part is "In a value context". When x
|> appears after "&" or "sizeof" (i.e., &x or sizeof x), this is *not*
|> a "value context".

The C++ standard speaks of an lvalue to rvalue conversion in this case.
Taken in isolation, I sort of like the formulation. On the other hand,
lvalue-ness is only partially (ambiguously) introduced into the type
system; I don't like the partialness, and I'm not convinced that things
would work well if it were completely integrated into the type system
(although I suspect that most of the problems wouldn't affect C).

|> There are additional "not value context"s:

|> ++x; /* x is required to name an object */
|> x = 9; /* x is required to name an object */

|> and in these cases, if "x" is an array, the code is invalid.

More generally, a "not value context" is one that requires an lvalue?

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #43

P: n/a
>Chris Torek <no****@torek.net> writes (in part):
|> There are additional "not value context"s:
|> ++x; /* x is required to name an object */
|> x = 9; /* x is required to name an object */
|> and in these cases, if "x" is an array, the code is invalid.
In article <news:86************@lns-vlq-21-82-255-58-67.adsl.proxad.net>
James Kanze <ka***@gabi-soft.fr> writes (in part):More generally, a "not value context" is one that requires an lvalue?


Almost. The sizeof operator confuses things, because sizeof can
take a type-name, an lvalue, or an rvalue:

long var;
size_t x;

x = sizeof(long); /* valid */
x = sizeof var; /* valid and same as sizeof(long) */
x = sizeof 3L; /* valid and same as sizeof(long) */

In C99, "sizeof" has even more special oddness when applied to VLAs.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #44

P: n/a

On Thu, 20 May 2004, James Kanze wrote:

Joe Wright <jo********@comcast.net> writes:

|> The compiler yields the size of it in bytes. The argument to sizeof
|> is never evaluated and is not an expression.

The argument of sizeof is not evaluated. It is an expression.
...or a type. The 'char' in 'sizeof (char)' is not an expression.
Careful not to contradict the wrong answer so strongly that your
own answer is wrong. :)

|> > How about a + i?

|> Looks like &a[i] to me. What do you think?

In C90, the two are different. There's a special exception in C99 which
changes the meaning of [] if it is the operand of &.


Could you elaborate on the difference in C90?

-Arthur
Nov 14 '05 #45

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:

|> On Thu, 20 May 2004, James Kanze wrote:

|> > Joe Wright <jo********@comcast.net> writes:

|> > |> The compiler yields the size of it in bytes. The argument to
|> > |> sizeof is never evaluated and is not an expression.

|> > The argument of sizeof is not evaluated. It is an expression.

|> ...or a type. The 'char' in 'sizeof (char)' is not an expression.
|> Careful not to contradict the wrong answer so strongly that your own
|> answer is wrong. :)

Yes. I was, obviously, thinking about the case where it wasn't a type.

|> > |> > How about a + i?

|> > |> Looks like &a[i] to me. What do you think?

|> > In C90, the two are different. There's a special exception in C99
|> > which changes the meaning of [] if it is the operand of &.

|> Could you elaborate on the difference in C90?

#define i 10
int a[ i ] ;
&a[ i ] /* Undefined behavior in C90, equivalent of a + i in C99

--
James Kanze
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
Nov 14 '05 #46

P: n/a
> Static variables are declared in neither heap nor stack, they are declared
in C source code files. And they are *allocated* whereever the
implementor chooses to, typically neither in heap (the malloc and friends
arena) nor in stack (automatic allocation arena and function call related
data).
But is it not required to allocate static variables in heap, so
that their values are intact even after a function is called more than once?
Then generally where are they allocated?.
Dan

Nov 14 '05 #47

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

anonymous wrote:
|>Static variables are declared in neither heap nor stack, they are declared
|>in C source code files. And they are *allocated* whereever the
|>implementor chooses to, typically neither in heap (the malloc and friends
|>arena) nor in stack (automatic allocation arena and function call related
|>data).
|
|
| But is it not required to allocate static variables in heap,

Nope. The C language doesn't even recognize the concept of 'heap', let alone
require static variables to be allocated in it. However, a specific C compiler
/might/ implement the language such that static variables are allocated in the
'heap', if such a thing exists in the implementation platform.

For that matter, it is perfectly concievable (although hardly likely) that a C
compiler will allocate static variables in a 'file', or even in a 'database'.
There are no restrictions on the mechanism that the compiler uses to allocate
the variables, so long as the allocation and access mechanisms do not conflict
with the requirements of the C language.

| so
| that their values are intact even after a function is called more than once?

Static variables are guaranteed (by the C language) to retain their values
across function calls. How the compiler manages that is entirely up to the
compiler.

| Then generally where are they allocated?.

OK, lets step away from the theory for a second. There's only so many times we
can say that the /mechanism/ isn't something that we know about or care about,
so long as the C language /policy/ is adhered to.

In practice, compilers don't use /either/ 'heap' or 'stack' to store static
variables. Both 'heap' and 'stack' are typically /dynamic/ allocation
mechanisms, and since we're talking about static data, these mechanisms are
unwieldly to use in that fashion.

C compilers (at least of the Unix kind) typically store static variables in
specific, pre-allocated areas of the BSS or DATA segments of the program's
memory map. They typically allocate the program's data area (in the resulting
'load module') such that it looks like...

~ | constants & initialized statics | uninitialized statics | heap | stack |
~ +=================================+-----------------------+......'.......+
~ | populated at pgm compile time | populated at program execution time |
~ \ /
~ bottom of memory top of memory
The constants and initialized statics consist of those data elements that
a) are of global scope and have been given initializers in the source code, or
b) are of local scope, defined as "static", and have been given initializers
~ in the source code, or
c) are string constants, or other truely static data

The uninitialized statics consist of those data elements that
a) are of global scope which have not been given initializers in the source
~ code, or
b) are of local scope, defined as "static", but have not been given
~ initializers in the source code,

The 'heap' and 'stack' are just whatever's left over after all the statics are
loaded into memory. It's just one big scratchpad area that gets managed by the
C runtime. Function calls to malloc() typically reserve blocks of memory
starting at the 'heap' end of this scratchpad area and build towards the
'stack' end. The C runtime allocates automatic variables starting at the top
of the 'stack' end, working down toward the 'heap' end.

So, you see, in theory, there is no 'heap'. In practice, statics aren't
allocated in the 'heap', if there is one.

Does this make it any clearer?

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAsLZ+agVFX4UWr64RAvoxAJ4zzpq0X+5dQeoUqnZ64b 4iuom7WACfYj0o
hiR8Knqi9tA+ZFBUeUwh0Ys=
=BjYz
-----END PGP SIGNATURE-----
Nov 14 '05 #48

P: n/a
Mabden <ma****@sbcglobal.net> wrote:
"Jatinder" <js*******@sancharnet.in> wrote in message
news:22*************************@posting.google.co m...
Here are some questions on C/C++, OS internals?
Q1 . What is the use of pointer to an array?


TO step through the array, or pass it by reference to another function.

On the other hand, that's whats indexing is for.
Q5 . What is IOCTL Explain .


A dinosaur. DOS based device-driver accessor: ioctl (handle, cmd, &dx, &cx);
Time to upgrade.


<OT in comp.lang.c>
It is actually what's used POSIX enviorments for IO controlling. So
it's not that outdated.
</OT in comp.lang.c>

--
Viktor Lofgren, Umea, Sweden
Mainly self-eductated UNIX programmer, running Slackware-current.
Nov 14 '05 #49

P: n/a
> So, you see, in theory, there is no 'heap'. In practice, statics aren't
allocated in the 'heap', if there is one.

Does this make it any clearer?

Thank you for the detail explanation - I was always under the impression that
if something is stored it is either in the stack/heap or registers.
Lew Pitcher

Nov 14 '05 #50

50 Replies

This discussion thread is closed

Replies have been disabled for this discussion.