I have a function(whose code I have stolen from one of you
in comp.lang.c):
void myStringClean(c har *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(m yName);
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(c har *String);
In advance thanks for helpful replies.
--
No matches found 13 1925
First, if you would like to receive replys to your question, I
recommend removing 'hannibalkannib alATyahooDOTno' 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
Eirik WS <hx************ *************** ***@xyxaxhxoxox .no> spoke thus: void myStringClean(c har *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(m yName); 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)cybers pace.org | don't, I need to know. Flames welcome.
Christopher Benson-Manica <at***@nospam.c yberspace.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)cybers pace.org | don't, I need to know. Flames welcome.
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(m ystring); /* note no '&' anywhere */
He could also write
char mystring[] = "something else";
myStringClean(m ystring); /* 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
On Mon, 5 Jan 2004, Christopher Benson-Manica wrote: Christopher Benson-Manica <at***@nospam.c yberspace.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
Eirik WS wrote:
What is Follow-up to: hannibalkanniba lATyahooDOTno?
Are you playing games? I have a function(whose code I have stolen from one of you in comp.lang.c):
void myStringClean(c har *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(m yName); 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(c har *String);
Here is an example of the use of this function.
#include <stdio.h>
#include <string.h>
/* prototype */
void myStringClean(c har *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",strin g);
myStringClean(s tring);
puts("\nAfter calling function myStringClean") ;
printf("The string = \"%s\"\n",strin g);
return 0;
}
void myStringClean(c har *String)
{
char *pointer;
if((pointer = strchr(String, '\n')) != NULL)
*pointer = '\0';
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email) http://www.geocities.com/abowers822/
Arthur J. O'Dwyer <aj*@nospam.and rew.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)cybers pace.org | don't, I need to know. Flames welcome.
Eirik WS wrote: I have a function(whose code I have stolen from one of you in comp.lang.c):
void myStringClean(c har *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(m yName); 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(c har *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
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following: Arthur J. O'Dwyer <aj*@nospam.and rew.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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 substitute the set<Data*> with a hash_set<Data*>:
typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet;
// typedef set<Data*> DataPointerHashSet; // (see complete code below)
But it doesn't work! Everything is fine...
|
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
|
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 in the following
way
void (*f)(int);
|
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);
}
|
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 values sounds
ideal.
I would like to know if the usage of this function pointer is valid
before I refactor some of the other functions that use the same data
structure to complete functions.
class Object
| |
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
vector_static_function.c:21: error: expected constructor, destructor,
or type conversion before '.' token
|
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;
|
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 100% sure
on their syntax.
This is a program I wrote to give myself an idea of how to use them.
But I'm not quite getting it. And I'm not sure what the syntax is for
a function pointer with parameters.
#include <stdio.h>
|
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 these.
|
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 10 member functions. Can
switch be replaced to member function pointer array?
Please provide me an example of source code to show smart pointer
inside class. Thanks....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |