473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer calling trouble

int main(){

/*
I have a function :) and I want to use it but I dunno how to call
it... (I have trouble that pointers)
please help me...
mmm... thanks for all advance!!
*/

return 0;
}
class MyClass {
....
....

public:

....

/**
* Breaks the given file name up to directory, name, and extension.
* @param fname The file name.
* @param dir Variable to receive the newly allocated directory
string.
* @param name Variable to receive the newly allocated name string.
* @param ext Variable to receive the newly allocated extension
string.
*/
virtual void parseFilename(c onst char* fname,
char** dir,
char** name,
char** ext);

....
....
}
Jul 22 '05 #1
4 1387
Hi

OzgurGul wrote:
* @param dir Variable to receive the newly allocated directory
string.
* @param name Variable to receive the newly allocated name string.
* @param ext Variable to receive the newly allocated extension
string.
*/
virtual void parseFilename(c onst char* fname,
char** dir,
char** name,
char** ext);


I reckon this is not your code, but notice however that in my opinion it's
poor design. parseFilename expects you to give a string containing a
filename as the first argument, followed by pointers to char pointers that
will hold the addresses of the corresponding dir/name/ext parts of that
filename upon successful return.
So you are expected to use it like that:

char *dir, *name, *ext;
MyClass mc; // default constructor available/reasonable?
mc.parseFilenam e("/usr/lib/libc.so", &dir, &name, &ext);
/* now dir points to a C-string containing the directory part, name to the
name and ext to a string giving the extension */

Problems with this:
- It's C-strings all over. C-strings aren't exactly bad, but they make code
error-prone and clumsy. Prefer std::string (declared in <string>)
- parseFilename accepts pointers to pointers where it had better take
references to these pointers: parseFilename("/file.ext", 0, 0, 0);
- parseFilename allocates memory, but obviously you have to deallocate it
(does MyClass offer any deallocation function for that purpose?). That's
bad because you don't know whether to use free or delete (or maybe
something completely different)

Markus

Jul 22 '05 #2
Markus Moll <mo**@rbg.infor matik.tu-darmstadt.de> wrote in message news:<41******* *************** *@newsread4.arc or-online.net>...
Hi

OzgurGul wrote:
* @param dir Variable to receive the newly allocated directory
string.
* @param name Variable to receive the newly allocated name string.
* @param ext Variable to receive the newly allocated extension
string.
*/
virtual void parseFilename(c onst char* fname,
char** dir,
char** name,
char** ext);

[snip - use it like this] char *dir, *name, *ext;
MyClass mc; // default constructor available/reasonable?
mc.parseFilenam e("/usr/lib/libc.so", &dir, &name, &ext);
/* now dir points to a C-string containing the directory part, name to the
name and ext to a string giving the extension */

Problems with this:
- It's C-strings all over. C-strings aren't exactly bad, but they make code
error-prone and clumsy. Prefer std::string (declared in <string>)
This assumes that parseFilename allocates memory. It does not have to.
- parseFilename accepts pointers to pointers where it had better take
references to these pointers: parseFilename("/file.ext", 0, 0, 0);
Conventional wisdom (i.e,. C++PL3ed) says not to pass by reference if
the value of the argument is going to be changed.
- parseFilename allocates memory, but obviously you have to deallocate it
(does MyClass offer any deallocation function for that purpose?). That's
bad because you don't know whether to use free or delete (or maybe
something completely different)


Nothing about parseFilename suggests that it allocates memory. In
fact, I would assume that it doesn't since it uses pointer-to-char
parameters. What is obviously missing here is the contract for this
function specifying what its semantics and behavior are.

Additionally, this function returns 'void' which is not helpful
insofar as determining whether the function succeeded, or if and how
the 'fname' argument is incorrectly formatted. 'int' would be a good
choice here.

/david
Jul 22 '05 #3

"David Rubin" <da********@war pmail.net> wrote in message
news:82******** *************** **@posting.goog le.com...
Markus Moll <mo**@rbg.infor matik.tu-darmstadt.de> wrote in message
news:<41******* *************** *@newsread4.arc or-online.net>...
Hi

OzgurGul wrote:
> * @param dir Variable to receive the newly allocated directory
> string.
> * @param name Variable to receive the newly allocated name string.
> * @param ext Variable to receive the newly allocated extension
> string.
> */

This assumes that parseFilename allocates memory. It does not have to.
Nothing about parseFilename suggests that it allocates memory. In
fact, I would assume that it doesn't since it uses pointer-to-char
parameters. What is obviously missing here is the contract for this
function specifying what its semantics and behavior are.
/david


Huh? Read the comments again. It most certainly *does* specify that it
alllocates new memory for those pointers. And that's exactly why it uses
char**, so that it can modify the pointers passed to it.

-Howard

Jul 22 '05 #4
"Howard" <al*****@hotmai l.com> wrote in message news:<%M******* **************@ bgtnsc05-news.ops.worldn et.att.net>...

[snip - comments]
Huh? Read the comments again. It most certainly *does* specify that it
alllocates new memory for those pointers. And that's exactly why it uses
char**, so that it can modify the pointers passed to it.


Somehow I missed that. My bad. Just goes to show how important
comments are since I had a much different idea about what the function
does without (apparantly) reading the comments. /david
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1360
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded my remove function as follows template <class T>
16
1912
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction, with denominator zero. A Fraction is created by the newFraction() function, and which in turns call normalize() function to simplify it, but before, the normalize() function will do a check, if the denominator is zero, it will give a error by...
9
5287
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
10
8648
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling func. as one of the following:
23
7797
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
10
5609
by: Ant | last post by:
Hi, I am having trouble with a member function pointer. It sees to give me the followign error Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. It only seems to give me problems if I use the friend class declaration
8
2396
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
1
1476
by: harter.jim | last post by:
I am currently calling a 3rd party external library using P/Invoke. The library gives me a handful of functions that I need to call. I have been successful at calling many of them, but I am some trouble calling a certain function that requires a pointer to a pointer. Here is the relevant header information: typedef struct _ITEM_INFO { int itemID; int itemValue;
30
2725
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
26
4849
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
0
8253
marktang
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...
0
8692
Oralloy
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...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8497
tracyyun
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...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
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();...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.