Function Using Pointers

E

Eirik WS

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.
 
M

Michael B Allen

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.

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
 
C

Christopher Benson-Manica

Eirik WS said:
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.
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
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*
 
A

Arthur J. O'Dwyer

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
 
A

Arthur J. O'Dwyer

Christopher Benson-Manica said:
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
 
E

Eric Sosman

Eirik said:
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.
 
A

Al Bowers

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';
}
 
C

Christopher Benson-Manica

Arthur J. O'Dwyer said:
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.
 
J

Joona I Palaste

Christopher Benson-Manica said:
Arthur J. O'Dwyer said:
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.
 
K

Kevin Goodsell

Christopher said:
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
 
B

Barry Schwarz

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>>
 
C

Christopher Benson-Manica

Joona I Palaste said:
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? :)
 
R

Richard Heathfield

Christopher said:
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".
 

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

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top