473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Function Using Pointers

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
13 1905
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
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
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

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

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


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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
15
by: bwaichu | last post by:
I'm struggling with the concept of function pointers. I understand that a function pointer contains the memory address of the function. But I'm not sure how they are effectively used, and I'm not...
4
by: prashant.khade1623 | last post by:
Hi all, Are function pointer and pointer to a function same ? How do we declare a function pointer ? How to declare an array of function pointers ? Can you please give some examples for...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.