Pointer arrays

D

Denis Palmeiro

Hi, I've been reading K&R, and I'm a bit confused. In the book,
they have the following function:

void writelines(char *lineptr[], int nlines )
{
while( nlines-- > 0 )
printf("%s\n", *lineptr++);
}

Now when I initialize:

char *lines[] = {"Hello", "Hey"};

and pass it as the first arg in that function, it works exactly
like expected.

However, if I try to do the same thing in main:

int main(void) {

char *lineptr[] = {"Hello", "Hey"};
int i = 2;

while( i-- > 0 )
printf("%s\n", *lineptr++);

return 0;
}

I get the error:
test.c:9: error: wrong type argument to increment

Now, how come I can increment lineptr in writelines, but not in
main. If it's declared as an argument, is it another type?

Also, if this is an array, how come it's possible to increment it
in the writelines function. I thought you can't change what an
array points to?

Thanks in advance,
Denis Palmeiro
 
M

Martin Ambuhl

Denis said:
Hi, I've been reading K&R, and I'm a bit confused. In the book,
they have the following function:

void writelines(char *lineptr[], int nlines )
{
while( nlines-- > 0 )
printf("%s\n", *lineptr++);
}

Now when I initialize:
char *lines[] = {"Hello", "Hey"};
and pass it as the first arg in that function, it works exactly
like expected.
However, if I try to do the same thing in main:
int main(void) {

char *lineptr[] = {"Hello", "Hey"};
int i = 2;
while( i-- > 0 )
printf("%s\n", *lineptr++);

return 0;
}

I get the error:
test.c:9: error: wrong type argument to increment

Now, how come I can increment lineptr in writelines, but not in
main. If it's declared as an argument, is it another type?

Also, if this is an array, how come it's possible to increment it
in the writelines function. I thought you can't change what an
array points to?

Please check the FAQ on the difference between a pointer and array.
Also check the FAQ for what happens when an array is used as an argument
in a function call.
It is considered polite to check the FAQ before posting. More
pointedly, it is considered rude not to have done so.

Compare your code to that below, which repeats your, in my view
ill-chosen, games with autodecrements and autoincrements:

#include <stdio.h>

void writelines(char *lineptr[], int nlines)
{
while (nlines-- > 0)
printf("%s\n", *lineptr++);
}

int main(void)
{

char *lines[] = { "Hello", "Hey" }, **decentargument;
int i = 2;

writelines(lines, 2);
decentargument = lines;
while (i-- > 0)
printf("%s\n", *decentargument++);

return 0;
}
 
J

Joe Wright

Denis said:
Hi, I've been reading K&R, and I'm a bit confused. In the book,
they have the following function:

void writelines(char *lineptr[], int nlines )
{
while( nlines-- > 0 )
printf("%s\n", *lineptr++);
}

Now when I initialize:

char *lines[] = {"Hello", "Hey"};

and pass it as the first arg in that function, it works exactly
like expected.

However, if I try to do the same thing in main:

int main(void) {

char *lineptr[] = {"Hello", "Hey"};
int i = 2;

while( i-- > 0 )
printf("%s\n", *lineptr++);

return 0;
}

I get the error:
test.c:9: error: wrong type argument to increment

Now, how come I can increment lineptr in writelines, but not in
main. If it's declared as an argument, is it another type?

Also, if this is an array, how come it's possible to increment it
in the writelines function. I thought you can't change what an
array points to?

Thanks in advance,
Denis Palmeiro

You can't pass an array to a function. Given..

void writelines(char *lineptr[], int nlines );

...the first argument is really 'char **lineptr', a pointer. In the
body of main()..

char *lineptr[] = {"Hello", "Hey"};

...lineptr is an array, not a pointer. You can't increment an array.
 
J

Jack Klein

On Wed, 28 Jul 2004 20:32:02 -0400, "Denis Palmeiro"

As Martin already said, read the FAQ for this newsgroup, link in my
signature, which has a whole chapter on pointers and arrays.
Hi, I've been reading K&R, and I'm a bit confused. In the book,
they have the following function:

void writelines(char *lineptr[], int nlines )
{
while( nlines-- > 0 )
printf("%s\n", *lineptr++);
}

Now when I initialize:

char *lines[] = {"Hello", "Hey"};

and pass it as the first arg in that function, it works exactly
like expected.

However, if I try to do the same thing in main:

int main(void) {

char *lineptr[] = {"Hello", "Hey"};
int i = 2;

while( i-- > 0 )
printf("%s\n", *lineptr++);

return 0;
}

I get the error:
test.c:9: error: wrong type argument to increment

Now, how come I can increment lineptr in writelines, but not in
main. If it's declared as an argument, is it another type?

Again, the FAQ has an entire section on pointers and arrays, and the
relationship between them. Read it.
Also, if this is an array, how come it's possible to increment it
in the writelines function. I thought you can't change what an
array points to?

The last sentence above certainly indicates a serious misunderstanding
of arrays in C, and their relationship with pointers.

An array is an object, not a pointer. It never points to anything.
Not at all, not ever. In many circumstances the name of an array is
automatically converted into a pointer to its first element, but the
array itself is not a pointer.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top