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

Function Using Pointers

P: n/a
I have a function(whose code I have stolen from one of you
in comp.lang.c):

void myStringClean(char *String) {
char *pointer;
if((pointer = strchr(String, '\n')) != NULL) {
*pointer = '\0';
}
}

How should I call this function? I've called like this in main.c:

myStringClean(myName);
I get no errors or warnings when I call
the function like this with
gcc -ansi -pedantic -Wall -W,
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.

The prototype in mSC.h looks like this:
#include <string.h>

void myStringClean(char *String);

In advance thanks for helpful replies.

--
No matches found
Nov 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
First, if you would like to receive replys to your question, I
recommend removing 'hannibalkannibalATyahooDOTno' from your news reader
configuration for the 'Followup-To' field.

On Mon, 05 Jan 2004 15:43:09 -0500, Eirik WS wrote:
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.


The declaration of myName is not 'char *myName;'. Perhaps you're using
'char myName[N];'?

Mike
Nov 14 '05 #2

P: n/a
Eirik WS <hx******************************@xyxaxhxoxox.no > spoke thus:
void myStringClean(char *String) {
char *pointer;
if((pointer = strchr(String, '\n')) != NULL) {
*pointer = '\0';
}
} How should I call this function? I've called like this in main.c: myStringClean(myName);
I get no errors or warnings when I call
the function like this with
gcc -ansi -pedantic -Wall -W,
I take this to indicate that myName is declared as

char myName[512]; /* or something */
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.


If my comment above is correct, then &myName is, indeed, incompatible
with your declaration of myStringClean. myStringClean takes a pointer
to a character as a parameter. myName is a pointer to a character, so
your first try works fine. &myName, though, is a pointer to a pointer
to a character, which is not the same thing. Say thank you to gcc for
kindly pointing this out to you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
char myName[512]; /* or something */ If my comment above is correct, then &myName is, indeed, incompatible
with your declaration of myStringClean. myStringClean takes a pointer
to a character as a parameter. myName is a pointer to a character, so
your first try works fine. &myName, though, is a pointer to a pointer
to a character, which is not the same thing. Say thank you to gcc for
kindly pointing this out to you.


Um... let me just say that I expect and welcome comments about the
fact that I injudiciously called myName a pointer to a character...
*doh*

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

P: n/a

On Mon, 5 Jan 2004, Michael B Allen wrote:

On Mon, 05 Jan 2004 15:43:09 -0500, Eirik WS wrote:

when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.


The declaration of myName is not 'char *myName;'. Perhaps you're using
'char myName[N];'?


No, his function was prototyped to take a 'char *', that is to say
a string. So he should be writing

char *mystring = foo();
myStringClean(mystring); /* note no '&' anywhere */

He could also write

char mystring[] = "something else";
myStringClean(mystring); /* note no '&' anywhere */

of course, but the problem in the code you left quoted isn't with
the variable declarations or the function implementation -- it's
with the way he was trying to call the function.
Given that the OP noticed that the first way worked and the
second way didn't, I don't think there's much more to add.

-Arthur

Nov 14 '05 #5

P: n/a

On Mon, 5 Jan 2004, Christopher Benson-Manica wrote:

Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
char myName[512]; /* or something */

If my comment above is correct, then &myName is, indeed, incompatible
with your declaration of myStringClean. myStringClean takes a pointer
to a character as a parameter. myName is a pointer to a character, so
your first try works fine. &myName, though, is a pointer to a pointer
to a character, which is not the same thing. Say thank you to gcc for
kindly pointing this out to you.


Um... let me just say that I expect and welcome comments about the
fact that I injudiciously called myName a pointer to a character...
*doh*


No, the expression 'myName' *does* yield a pointer to character in
this context. You're going to get comments about the fact that you
called '&myName' a "pointer to a pointer to a character," when it's
really a pointer to an array[512] of character. :)

-Arthur

Nov 14 '05 #6

P: n/a


Eirik WS wrote:

What is Follow-up to: hannibalkannibalATyahooDOTno?
Are you playing games?
I have a function(whose code I have stolen from one of you
in comp.lang.c):

void myStringClean(char *String) {
char *pointer;
if((pointer = strchr(String, '\n')) != NULL) {
*pointer = '\0';
}
}
This is not a robost function. It simply searches the
string pointed to by the argument, from first char to last,
looking for the first occurance of '\n', the newline character.
If it finds a newline character, it will overwrite it with '\0'
which truncates the string and then the function will exit. It a
newline character is not found, the string remains unchanged.
IMO it is a poorly written function, especially because it does
not return a value indicating whether or not the string was
modified.
How should I call this function? I've called like this in main.c:

myStringClean(myName);
I get no errors or warnings when I call
the function like this with
gcc -ansi -pedantic -Wall -W,
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.

You did not provide the declaration of myName. myName and &myName
are not the same. You would think that one or the other is wrong.

The prototype in mSC.h looks like this:
#include <string.h>

void myStringClean(char *String);


Here is an example of the use of this function.

#include <stdio.h>
#include <string.h>

/* prototype */
void myStringClean(char *String);

int main(void)
{
char string[64] = "This is line one\nThis is line two";

puts("Before calling function myStringClean");
printf("The string = \"%s\"\n",string);

myStringClean(string);

puts("\nAfter calling function myStringClean");
printf("The string = \"%s\"\n",string);
return 0;
}

void myStringClean(char *String)
{
char *pointer;
if((pointer = strchr(String, '\n')) != NULL)
*pointer = '\0';
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #7

P: n/a
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
No, the expression 'myName' *does* yield a pointer to character in
this context. You're going to get comments about the fact that you
called '&myName' a "pointer to a pointer to a character," when it's
really a pointer to an array[512] of character. :)


Now, you say 'myName' "yields" a pointer to a character - I do know
that it decays to such in this context - but is it *really* a pointer
to a character? Is that too pedantic of a question? The faqt (sic)
is that this is a FAQ and I blew it, although I did retrieve my error
in a timely fashion, kind of.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8

P: n/a
Eirik WS wrote:

I have a function(whose code I have stolen from one of you
in comp.lang.c):

void myStringClean(char *String) {
char *pointer;
if((pointer = strchr(String, '\n')) != NULL) {
*pointer = '\0';
}
}

How should I call this function? I've called like this in main.c:

myStringClean(myName);
I get no errors or warnings when I call
the function like this with
gcc -ansi -pedantic -Wall -W,
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.

The prototype in mSC.h looks like this:
#include <string.h>

void myStringClean(char *String);

In advance thanks for helpful replies.


You have omitted one crucial piece of information: the
declaration of `myName'. However, since gcc accepts the first
form of the call without complaint it appears `myName' is
either a `char' array or a pointer to `char'. Read Section 6
in the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... if you are confused about why I cannot tell whether
`myName' is an array or a pointer from the information you
have provided.

The second form of the call is incorrect because:

- If `myName' is an array of `char', then `&myName'
is a pointer to such an array. myStringClean(),
though, expects to receive a pointer to a `char',
not a pointer to an array -- and that is why gcc
complains. See FAQ Questions 6.12 and 6.13.

- If `myName' is a `char*' pointer variable, then
`&myName' is a pointer to a pointer (... to `char').
myStringClean() expects to receive a pointer to a
`char', not a pointer to a pointer, so gcc complains.

--
Er*********@sun.com
Nov 14 '05 #9

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
No, the expression 'myName' *does* yield a pointer to character in
this context. You're going to get comments about the fact that you
called '&myName' a "pointer to a pointer to a character," when it's
really a pointer to an array[512] of character. :)
Now, you say 'myName' "yields" a pointer to a character - I do know
that it decays to such in this context - but is it *really* a pointer
to a character? Is that too pedantic of a question? The faqt (sic)
is that this is a FAQ and I blew it, although I did retrieve my error
in a timely fashion, kind of.


Depends on what do you mean by "myName". The variable myName or the
expression consisting of it? They're two very different beasts.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #10

P: n/a
Christopher Benson-Manica wrote:
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:

No, the expression 'myName' *does* yield a pointer to character in
this context. You're going to get comments about the fact that you
called '&myName' a "pointer to a pointer to a character," when it's
really a pointer to an array[512] of character. :)

Now, you say 'myName' "yields" a pointer to a character - I do know
that it decays to such in this context - but is it *really* a pointer
to a character? Is that too pedantic of a question? The faqt (sic)
is that this is a FAQ and I blew it, although I did retrieve my error
in a timely fashion, kind of.


myName is just an identifier in the source code, of course. The type of
the object it refers to is array of char, obviously. But in most
contexts where the name is used it is syntactically an expression, and
the type of that expression is char*. So saying that 'myName' is a
pointer in some contexts seems accurate enough to me.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #11

P: n/a
On Mon, 05 Jan 2004 21:43:09 +0100, Eirik WS
<hx******************************@xyxaxhxoxox.no > wrote:
I have a function(whose code I have stolen from one of you
in comp.lang.c):

void myStringClean(char *String) {
char *pointer;
if((pointer = strchr(String, '\n')) != NULL) {
*pointer = '\0';
}
}

How should I call this function? I've called like this in main.c:

myStringClean(myName);
I get no errors or warnings when I call
the function like this with
gcc -ansi -pedantic -Wall -W,
You don't tell us how you defined myName. Based on the success of the
above though, it is probably either a char* or char[N]. In either
case, when used as an argument to a function, it becomes a char* which
matches exactly what the function is expecting.
when I call it using
myStringClean(&myName);
I get the warnings
passing arg 1 of 'myStringClean' from incompatible pointer type.
If myName is a char*, then &myName is a char** (pointer to pointer to
char) and the error message is means exactly what it says.

If myName is a char[N], then &myName is of type char (*)[N] (pointer
to array of N char) and again the error message means exactly what it
says.

Even though the value in all four cases points to the same byte, there
are three different types involved. Only one of the three types is
acceptable to myStringClean so you should not be surprised that the
other two get rejected.

The prototype in mSC.h looks like this:
#include <string.h>
This has nothing to do with how you prototype myStringClean.

void myStringClean(char *String);

<<Remove the del for email>>
Nov 14 '05 #12

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
Depends on what do you mean by "myName". The variable myName or the
expression consisting of it? They're two very different beasts.


Um, care to elaborate on both? Please? :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #13

P: n/a
Christopher Benson-Manica wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
Depends on what do you mean by "myName". The variable myName or the
expression consisting of it? They're two very different beasts.


Um, care to elaborate on both? Please? :)


The object, myName, is an array of 512 characters. Thus, if you do sizeof
myName, you'll get a result of 512.

In a value context such as, say:

char *p = myName;

the expression myName evaluates not to the array itself, but to a pointer to
its first element.

This is what Chris Torek calls "The Rule".

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.