pointers.

M

mdh

Could I ask a question to help me better understand this issue.
(P 108, K&R II)


If one declares: char *lineptr[someNmber]; ( which is explained as an
array, each of whose elements is a pointer to a char, which I see)

and a function like qsort is as: void qsort(char *lineptr[],
int ,int);

I am not sure I understand why the calling function uses the
terminology:

if ( etc etc etc)

qsort(lineptr, someNumber, someNumber);

as opposed to

qsort(*lineptr, someNumber, someNumber);

As usual, there is something very fundamental that I am probably
missing :)
 
M

Mike Wahler

mdh said:
Could I ask a question to help me better understand this issue.
(P 108, K&R II)


If one declares: char *lineptr[someNmber]; ( which is explained as an
array, each of whose elements is a pointer to a char, which I see)

and a function like qsort is as: void qsort(char *lineptr[],
int ,int);

For the benefit of those who don't have access to K&R II:
The declared function 'qsort' is not the one in the standard
library but one in the book (p. 110).
I am not sure I understand why the calling function uses the
terminology:

if ( etc etc etc)

I must assume the line you're referring to is the one that reads:

if((nlines = readlines(lineptr, MAXLINES)) >= 0)
qsort(lineptr, someNumber, someNumber);

as opposed to

qsort(*lineptr, someNumber, someNumber);

As usual, there is something very fundamental that I am probably
missing :)

The 'if' statement is not directly related to the 'qsort()' function.
The conditional expression of the 'if' calls function 'readlines()'
which returns the number of lines read and compares that number to zero.
Only if more than zero lines were read is the 'qsort()' function called.
Not much point in sorting an empty array, eh? :)

To understand what it's doing, you need to look at the *whole* program
for context.

-Mike
 
M

Mike Wahler

Mike Wahler said:
mdh said:
Could I ask a question to help me better understand this issue.
(P 108, K&R II)


If one declares: char *lineptr[someNmber]; ( which is explained as an
array, each of whose elements is a pointer to a char, which I see)

and a function like qsort is as: void qsort(char *lineptr[],
int ,int);

For the benefit of those who don't have access to K&R II:
The declared function 'qsort' is not the one in the standard
library but one in the book (p. 110).
I am not sure I understand why the calling function uses the
terminology:

if ( etc etc etc)

I must assume the line you're referring to is the one that reads:

if((nlines = readlines(lineptr, MAXLINES)) >= 0)
qsort(lineptr, someNumber, someNumber);

as opposed to

qsort(*lineptr, someNumber, someNumber);

As usual, there is something very fundamental that I am probably
missing :)

The 'if' statement is not directly related to the 'qsort()' function.
The conditional expression of the 'if' calls function 'readlines()'
which returns the number of lines read and compares that number to zero.
Only if more than zero lines were read is the 'qsort()' function called.

Actually, that's not exactly correct. 'qsort()' is called if 'getlines()'
returns a number greater than *or equal* to zero. ('getlines()' returns
-1 if an error occurs -- that is, more than MAXLINES are read, or memory
allocation fails.)

Sorry about that.

-Mike
 
M

Mike Wahler

mdh said:
Sorry if I mislead you with the if statement. My question related
specifically to the lines:







and the use of *lineptr vs lineptr.

Sorry, I didn't read closely enough.

The expression 'lineptr' refers to the whole array.
The expression '*lineptr' refers to only the first
element of the array. We want to sort the whole
array, not just a single element.

-Mike
 
B

Bertrand Mollinier Toublet

mdh said:
Could I ask a question to help me better understand this issue.
(P 108, K&R II)


If one declares: char *lineptr[someNmber]; ( which is explained as an
array, each of whose elements is a pointer to a char, which I see)

and a function like qsort is as: void qsort(char *lineptr[],
int ,int);

I am not sure I understand why the calling function uses the
terminology:

if ( etc etc etc)

qsort(lineptr, someNumber, someNumber);

as opposed to

qsort(*lineptr, someNumber, someNumber);
Well, the lineptr variable has type "array of pointer to char". The
lineptr argument to qsort has the same type. Therefore, lineptr is
passed as such to qsort.

*lineptr is simply a pointer to char and is incompatible with the array
of pointer to char argument to qsort.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top