473,786 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring functions moving input pointer

There is already one function processing pointer values this way. And
there may easily appear more, including wrappers for this function.

For purposes of further discussion, consider simplified function,
`consume'. It has one arg, INBUF, that points to a variable that
points to the first character in the input buffer. The function never
modifies characters pointed to by `*INBUF'. It, however, updates
the `*INBUF' value to point to the byte following the last byte
successfully consumed.

We can declare this function

void consume (const char **);

or

void consume (char **);

Which one is appropriate, wrt semantics of `const' as designed in
current C language standards? In any case, applications may have both
`char *' and `const char *' variables, which must be passed and
updated as described above. In any case, the conversions are be
needed to do such a passing, like this.

void consume (const char **);
/* ... */
char *buf;
consume ((const char **) &buf);

Or

void consume (char **);
/* ... */
const char *buf;
consume ((char **) &buf);

Currently, if T is type, any `T*' value may be assigned to `const T*'
one. This is not the case for assigning `T**' values to `const T**'
ones. Why would allowing this be incorrect / dangerous?

Or should we completely avoid declaring functions with `**' args like
these, and specify them like `write' instead? That is, to take `const
char *' arg and return or otherwise pass to caller a `ssize_t' value
saying how much should be added to said `const char *' value before
passing it to the next call.
The function needs to update `size_t' value in addition to `char *'
one anyway, and this should be also the case for its wrappers. So one
might declare `consume' like this.

typedef struct {
const char *p;
size_t s;
} ciov_t;
void consume (ciov_t *IR);

So that is updates both `IR->p' and `IR->s'.

Never seen such a declarations, however. `writev', in particular,
does not use input structures with `const void *' for data to write.
This will even cause trouble for application writers needing to pass
such a data pointers to `writev'. What would be dangerous / incorrect
in declaring functions this way?

Aug 24 '06 #1
1 2000
Ilya N. Golubev schrieb:
There is already one function processing pointer values this way. And
there may easily appear more, including wrappers for this function.

For purposes of further discussion, consider simplified function,
`consume'. It has one arg, INBUF, that points to a variable that
points to the first character in the input buffer. The function never
modifies characters pointed to by `*INBUF'. It, however, updates
the `*INBUF' value to point to the byte following the last byte
successfully consumed.

We can declare this function

void consume (const char **);

or

void consume (char **);

Which one is appropriate, wrt semantics of `const' as designed in
current C language standards? In any case, applications may have both
`char *' and `const char *' variables, which must be passed and
updated as described above. In any case, the conversions are be
needed to do such a passing, like this.

void consume (const char **);
/* ... */
char *buf;
consume ((const char **) &buf);

Or

void consume (char **);
/* ... */
const char *buf;
consume ((char **) &buf);

Currently, if T is type, any `T*' value may be assigned to `const T*'
one. This is not the case for assigning `T**' values to `const T**'
ones. Why would allowing this be incorrect / dangerous?
Have a look at the comp.lang.c FAQ, especially
http://c-faq.com/ansi/constmismatch.html

BTW: I'd rather use the "secure" form of "const char **" and have
no cast, i.e.
char *buf;
const char *cbuf;
....
cbuf = buf;
consume(&cbuf);
Avoiding the cast can help you find errors like
consume(cbuf);
which is not possible for
consume((const char **)buf);
Or should we completely avoid declaring functions with `**' args like
these, and specify them like `write' instead? That is, to take `const
char *' arg and return or otherwise pass to caller a `ssize_t' value
saying how much should be added to said `const char *' value before
passing it to the next call.
Note that in the context of standard C, there is no "ssize_t" but
there is in POSIX. From the following, I guess that you really mean
size_t.
The function needs to update `size_t' value in addition to `char *'
one anyway, and this should be also the case for its wrappers. So one
might declare `consume' like this.

typedef struct {
const char *p;
size_t s;
} ciov_t;
void consume (ciov_t *IR);

So that is updates both `IR->p' and `IR->s'.

Never seen such a declarations, however. `writev', in particular,
does not use input structures with `const void *' for data to write.
This will even cause trouble for application writers needing to pass
such a data pointers to `writev'. What would be dangerous / incorrect
in declaring functions this way?
I am not sure whether I understood you correctly.
Do you want to modify data pointed to by a pointer to const type?
This is most of the time a bad idea; if you can guarantee that
you had a pointer to unqualified type to start with, then
"removing the const" may work. If the data was const qualified
from the start, it may well reside in some sort of non-writable
memory.
Or do you want to read data pointed to by IR->p?

The "updates" throws me off, to be honest.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 24 '06 #2

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

Similar topics

19
4261
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
47
3899
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R say, C programs consist of variables to store the input and functions to manipulate them. This would make C object-oriented - how cool would that be? Are there any problems with adding the ability to have functions
4
1960
by: snow.carriers | last post by:
http://www.rafb.net/paste/results/fTQgRW16.html Here's my program so far. This is what I'm trying to do: http://contest-cemc.uwaterloo.ca/ccc/2005/senior/phone.pdf So far it works fine. The only problem is that I'm not sure how to input data, s1, into the program. The second thing that I'm not sure how to input the variable into the function. The code: strcpy(line, "88-SNOW-5555");
4
1424
by: mantrid | last post by:
Hello Hope you can help. I have been using the code below to move elenents around the page. This has worked fine until I put some arguments into the functions so I could use them in many places with out having to create lots of slightly different functions. The code that has now stoped working is below. My javascript is not great and I have been struggling with this all day. Can anyone see where Ive gone wrong Thanks Ian
11
13482
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
3
2018
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite loop (easy) waiting for the mouse to be clicked (I dont need help with this) and depending on user input a variable might define what function I want (be it line, circle....(again these are my issues)
7
2909
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its method called and then the derived class have its method called. What am I not understanding? Int the following code, my Event Tester class is getting called twice for keyboard events when I step through the debugger:...
5
8840
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT INTEGER,
6
2287
by: ma740988 | last post by:
I'm perusing a question and curiosity got the best of me in part because I cant solve the problem Consider: typedef unsigned int word_type ; class foo { public: void set_mx_digits_1 ( word_type wt ) {}
0
9496
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,...
0
10164
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
9961
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
8989
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...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
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
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
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
3
2894
bsmnconsultancy
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...

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.