incompatible pointer type warning ?

M

Michael

Hi all,

why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);

and later on I have:

void collectInput(char *input){
scanf("%s", input);
}

I know this is simple, but I can't see what's wrong.....

Regards
Michael
 
I

Ian Collins

Michael said:
Hi all,

why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);
should be
collectInput(inputString);

or
collectInput(&inputString[0]);

&inputString is the address of the array, which in this case is a char**.
 
R

Ratan

void collectInput(char *input){
scanf("%s", input);
Your function accepts a pointer to chanracter...Not a pointer to
pointer to a character.

When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
to character ...So while calling above function you need to pass the
array name only..
So
collectInput(&inputString); line need to be modified to " collectInput(inputString);"
Hi all,

why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);

and later on I have:

void collectInput(char *input){
scanf("%s", input);
}

I know this is simple, but I can't see what's wrong.....

Regards
Michael
 
P

pete

Michael said:
Hi all,

why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);

Try:

collectInput(inputString);

without the &
instead.
and later on I have:

void collectInput(char *input){
scanf("%s", input);
}

I know this is simple, but I can't see what's wrong.....

The name of an array is automatically converted
to a pointer to it's first element in most cases,
such as the case above.

And make sure that inputString contains a string,
before passing it as an argument to a function
that takes a pointer to a string.
 
W

whyglinux

Ian said:
&inputString is the address of the array, which in this case is a char**.

No, the type of &inputString is not char**, which is a pointer to a
pointer, but char (*)[MAX_INPUT_LENGTH], which is a pointer to an array.
 
S

SuperKoko

Ratan said:
void collectInput(char *input){
scanf("%s", input);
Your function accepts a pointer to chanracter...Not a pointer to
pointer to a character.

When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
to character ...So while calling above function you need to pass the
array name only..
No.
inputString doesn't become a pointer to a character (it is the C
language, not the B language).
inputString is an *array*.
&inputString is a pointer to an array (char (*)[MAX_INPUT_LENGTH])
which is not compatible with a pointer to char (char*).
array-to-pointer conversions occur very easily, but it arrays are not
pointers.

My post is not informative, since whyglinux already pointed that in
this discussion, but I say that in order to ensure that everybody in
this discussion will understand that.
 
C

CBFalconer

Ian said:
Michael said:
why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);

should be
collectInput(inputString);

or
collectInput(&inputString[0]);

&inputString is the address of the array, which in this case is a char**.

No, it isn't a char**. While an array reference is often converted
to a pointer to its first element, that does not make the address
of an array a pointer to a pointer. Thinking of it as a char**
leads to gross errors.

Your actual code corrections are fine.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
K

Keith Thompson

SuperKoko said:
Ratan said:
void collectInput(char *input){
scanf("%s", input);
Your function accepts a pointer to chanracter...Not a pointer to
pointer to a character.

When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
to character ...So while calling above function you need to pass the
array name only..
No.
inputString doesn't become a pointer to a character (it is the C
language, not the B language).
inputString is an *array*.

The object called inputString is an array, and of course it doesn't
become anything.

The expression intputString, which is the name of an array, is
implicitly converted to a pointer to its first element in most
contexts, including this one. So in that sense, it's perfectly
correct to say that "inputString itself becomes a pointer to
character".
 
I

Ian Collins

CBFalconer said:
Ian said:
Michael said:
why do I get a message:

warning: passing arg 1 of `collectInput' from incompatible pointer type

In 'main' have:

char inputString[MAX_INPUT_LENGTH];
collectInput(&inputString);

should be
collectInput(inputString);

or
collectInput(&inputString[0]);

&inputString is the address of the array, which in this case is a char**.


No, it isn't a char**. While an array reference is often converted
to a pointer to its first element, that does not make the address
of an array a pointer to a pointer. Thinking of it as a char**
leads to gross errors.
Oops, my bad.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top