| | |
Pointer and function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
Can somebody please tell what does this function prototype indicate--
Is it some thing like that the return type would always be constant char ? And what is the difference in the two declarations-- char const* and const char*?
Thanks
C++ Syntax (Toggle Plain Text)
const char* fun( char const *,const char*);
Is it some thing like that the return type would always be constant char ? And what is the difference in the two declarations-- char const* and const char*?
Thanks
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
•
•
•
•
Originally Posted by aminura
Can somebody please tell what does this function prototype indicate--Is it some thing like that the return type would always be constant char ?C++ Syntax (Toggle Plain Text)
const char* fun( char const *,const char*);
const as "read-only". You the programmer are not supposed to be writing to it. So the function returns a pointer to read-only data (likely a C-style string).•
•
•
•
Originally Posted by aminura
And what is the difference in the two declarations-- char const* and const char*?
char const* - what the pointer is pointing to is read-only.const char* - what the pointer is pointing to is read-only.char *const - the pointer is read-only (may not be incremented, etc.).const char *const - the pointer is read-only (may not be incremented, etc.); what the pointer is pointing to is read-only.char const *const - the pointer is read-only (may not be incremented, etc.); what the pointer is pointing to is read-only. Last edited by Dave Sinkula; Jan 3rd, 2006 at 4:53 pm. Reason: Yay, Dave. Let's try to get it right the first time.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
It may be more helpful to read const as "read-only". You the programmer are not supposed to be writing to it. So the function returns a pointer to read-only data (likely a C-style string). I guess I haven't got this thing fully into my lazy brain! So does it actually mean that whatever the arguments maybe , the output can never be manipulated or I'll be always having the same output.
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
It generally means you shouldn't change what is pointed to. Here the a C++ compiler will prevent you from assigning the return value of foo to oops. But a C compiler may just give you a warning, allowing you to do bad things. Namely, a string literal may be put in program code (or somewhere else that is read-only), and should not be considered to be writable. But the languages do afford you ways to do things you shouldn't.
By const-qualifying the return value, the programmer who gave you the protoype is providing a damn good hint to prevent you from doing things you ought not do. Warnings and errors should then hopefully guide you into writing correct code.
But there is generally no foolproof way of stopping a programmer from doing dumb things and writing code that crashes or otherwise does bad things.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> const char *foo(const char *text) { puts(text); return text; } int main(void) { const char literal[] = "non-modifiable string literal"; char *oops = foo(literal); oops[0] = 'N'; /* BAD! */ return 0; }
By const-qualifying the return value, the programmer who gave you the protoype is providing a damn good hint to prevent you from doing things you ought not do. Warnings and errors should then hopefully guide you into writing correct code.
But there is generally no foolproof way of stopping a programmer from doing dumb things and writing code that crashes or otherwise does bad things.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Deleting this pointer (C++)
- Dynamically Add Event Handler without using this pointer (C++)
- Passing array of structures into a function (C++)
- How to pass a function pointer as an argument? (C++)
- Callback function (C++)
- Passing a Function, function pointer (C++)
- pointer averaging function (C)
- pointer (C)
- Assign content to a function pointer (C)
Other Threads in the C++ Forum
- Previous Thread: Inheritance in C???
- Next Thread: Half Life 2 SDK; Coding help wanted
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






