Given that scanf has (const char *) in the documentation from Microsoft and the answer to this question what the heck is going when I do the same ... |
looking at the declaration for XDrawString from X11, it is
int XDrawString(Display *display, Drawable d, GC gc,
...
|
|
the following code can be compiled correctly on both VC or gcc:
char *str = "I am a const!";
str[2] = 'n';
however, obviously there is a run-time-error. Since "I am a const!" is ... |
|
Hi Banfa, Thanks to the reply. You are absolutely correct about integer. So that only already I have commented the second line of my program. But, like that I should not able to change the character pointer value also. But I am able to change it. (Refer line number 3 of my program). My moto is I should not able to ... |
foo and bar are addresses of characters. You are trying to add addresses and this makes no sense. You need to work with contents of these strings and not their addresses. What you need to so is: 1) count the number of characters in the string foo. 2) count the number iof characters in the string bar. 3) allocate memory for ... |
|
|
Hi, I have a problem with the preprocessor. I have written my own little assert macro. This is supposed to log a message (with log4cxx): #define LogAssert(Expression) \ if (Expression) { \ LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \ #Expression " in file " __FILE__ ", line " #__LINE__ "."); \ ::exit(1); \ } Unfortunately, the preprocessor is only quoting marcro arguments. In my ... |
Sean Farrow wrote: Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? No const_cast is rather technical and best not used (unless you *really* understand what its purpose is). In a nutshell, if you have an object that you know is not const but it appears as const via e.g ... |
Hi, I have a question regarding the conversion (in c or c++) from char** to const char**. The fallowing code refuses :) to compile with g++ (and others): char **p; const char **q = static_cast(p); or, exact the same situation: void f(const char **p) {} int main() { f(p); return 0; } And I don't understand why. It should work ... |
Hi Guys, When a function has following arg type, update_data(const char *const * update_list,...) exactly what object type update_list can take? For example, I can call update_aaa() using following variale as an arg: const char *updatelist_default[]={ "aaa", "bbb", NULL}; Then, why the 2nd 'const' is necessary ? I mean, update_data(const char * * update_list,...) would work. But actually not! |
Hi! In converting applications from ansi to unicode, it is a problem that the std::wcout accepts a const char * without complaining compile time. This compiles and runs: ------------------------------ const char *lookup() { return "Hello world"; } std::wcout << lookup(); ------------------------------ I would like it to fail compile time, so I can properly convert cases like this to unicode in a ... |
|
Hello, I have two functions: void function1(char* const names[]); void function2(const char** names) { //do other stuff function1(names); //do more stuff } My compiler (VS2005 C++) gives me "cannot convert parameter 1 from const char** to char * const[]", which I'd expect. My question is how do I convert from const char** to char* const[]? It's been a while since I've ... |
Hi, how can I literator a const char*? I have this code, but it results in an infinite loop. It never exits the while (p1 != '\0') loop void teststr( const char *str1) { char* p1 = (char*)str1; while (p1 != '\0') { //.... p1 ++; } } and i call teststr like this: teststr("http://www.cnn.com"); Can you please tell me why ... |
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) { char* pS1 = s1; char* pS2 = s2; ... ... } As you can see, I ... |
How do I convert the type const char* to type char*? I have an input string ("input_string") of class string. I need to convert this to type char* in order to use it in a call to the strtok() function. However, the .c_str() string class function returns type const char*. The relevant code may be found below. My compiler returns the ... |
C stops the conversion from (char **) to (const char **). c-faq.com sec 11.10 has explanation on this point. But, for example, even the conversion from (char *) to (const char *) brings the same dangerous as in the previous conversion. Why the latter simple but dangerous one is allowed in C? $ cat f1.c int main(void) { const char c ... |
On Jan 28, 11:21 pm, "" ("abcdg"); sizeof(reinterpret_cast(p)); ----------------------------------------------------------------------- ---------------------------------------- > the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *" casts away constness " At this point you stop wrestling with the C++ bullshit and just use a C style ... |
|
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str(); Is this ok? Is there any other better way to do the same? I'm not sure if I'm doing the rigth thing, so I hope you can help me with this. Thanks in advanced! |
lada77@yahoo.com All, Just wondering if one of you very helpful guru's out there could comment on some odd behaviour I am seeing. I'm betting it is something obvious but I am not experienced enough to tell right away. Here is my code snippet and the results that I am seeing: #include |
Greetings, Could anyone explain to me how the parameter type works here? I am also interested in the technical details of what is actually happening on the stack. I have never seen a "char * const &szFoo". I usually just use "char *pszFoo" whenever I want to pass a string, or maybe "const char *pszFoo" if I know that I won't ... |
|
26. const char * cboard.cprogramming.com |
Code: #include #include char *cprog_strrev(char *s, size_t n) { size_t i, j; for (i = 0, j = n - 1; i < j; i++, j--) { char save = s[i]; s[i] = s[j]; s[j] = save; } return s; } char *cprog_itos(int value, char *buf, size_t size, int radix) { const char *digits = "0123456789ABCDEF"; int sign = ... |
No, essentially what's going on is this Code: main.c int main() { //call function in external file extern.c extern(__FILE__,__LINE__,__func__) } extern.c int extern(const char*file, int line, const char *func) { //print file, line, func //print this file's file, line, func } The goal is to macro the extern() function call so it only has ONE parameter, not known to the caller ... |
This is incorrect. The items in your code are constant strings, which you store in non-const char. As soon as it is written to, your program will likely crash. Furthermore, the last "\0" is an empty string, identical to "", except that the former contains two 0-bytes. I expect it should read NULL, although granted, it depends on the called function ... |
|
const char *pointer; { pointer = "This is a text"; } // Would it still be legal and safe to assume pointer still reffer to // "This is a text"?(outside of brackets) // I would guess so, but that would mean text-constants are // guaranteed not to get overwritten even when out of scope. |
|
|
static const unsigned char *const Msgs[1][16] = { { "Please", "go to the", "user menu", "- tanks -", "", "", "", "", "", "", "", "", "", "", "", "" } }; . . . void fun(void) { unsigned char i; for (i = 0; Msgs[0][i]; i++) { printf("%s", (unsigned char *)Msgs[0][i]); } } |
In other words: Code: { const char *p = "initial"; p = "reassignment"; strcpy(p, "copying"); /* invalid */ } { char *const p = "initial"; p = "reassignment"; /* invalid */ strcpy(p, "copying"); } { const char *const p = "initial"; p = "reassignment"; /* invalid */ strcpy(p, "copying"); /* invalid */ } Of course, the second strcpy() is actually invalid ... |
Hello :-) I'm an engineer (mechanic) so Cprogramming is not my job, and my question can sound silly to most of you. Please understand and thank you in advance. My problem is: I get a FileList.txt on argv[1], for example: C:\F00\Pippo_Folder\Pippo_Data\PippoData001.dat C:\F00\Robin_Folder\Robin_Data\RobinData005.dat C:\F00\Batman_Folder\Batman_Data\BatmanData100.dat etc.. I want to fopen(_argv[1],"r") then fopen(first_file,"rb"); and that's OK, I can do that. My problem is that ... |
|
#include #include #define GET_NUM_ELEMENTS(x) (sizeof(x) / sizeof(x[0])) int main(void) { const char *cStr = "Stuff"; char *str = "Stuff"; const char *cStrArray[] = { "Um", "Some", "Words" }; char *strArray[] = { "Um", "Some", "Words" }; for(register unsigned int i = 0; i < GET_NUM_ELEMENTS(StrArray); i++) printf("%s ", strArray[i]); getch(); return 0; } |
#include int main(void) { int n, int i = 0; char part_code[10]; /* input part code */ char part_qty[10]; /* input part quantity */ char part_desc[10][81]; /* input part description */ printf("enter part code or Z to quit:"); scanf("%c", &part_code[i]); while (part_code[i] != 'Z') { printf("\nEnter part %c quantity: ", part_qty[i]); scanf("%d", &part_qty[i]); printf("\nEnter part %c 's Description: ", part_desc[i]); ... |
|
'hello' is the name of the pointer, not the variable to which it points. Its value IS the address to which the pointer points. Changing the value of 'hello' changes WHERE the pointer points. 'Hello' is a mnemonic for the address of the pointer, the contents of the pointer are the address of another thing. |
I have a constructor that takes a const char*, if I have a function that takes const myclass&. and I pass a const char* to it, shouldn't the ctor for const char* implicitly be called to create a temporary object of myclass and pass it to the function? I get an undefined reference error. I have myclass operator= (const myclass&) defined, ... |
Are you sure that putenv() makes no changes to its argument? If there is any chance you should copy the string into another buffer and use that buffer as the argument instead. const may be relatively new, but it has been around for nearly 20 years, so most programmers should know about it. |
Hi to all! First to say, I have no idea at all about C++, but i'm on the way to create a FileMaker plugin that enables the possibility to show help about some topic. Now I'm getting everytime the error : error C2664: 'atoi' : cannot convert parameter 1 from 'unsigned char' to 'const char *' Conversion from integral type to ... |
Let me start by saying I am using Emin Martinian's my_hash_map for my hash_map. I seem to be having a problem with calling the find function and passing a value to the char* const & parameter. find(char* const &); I use the std::string for most of my lookups, but with this guy, I seem to be having a problem. // Pseudo ... |
Declaring a parameter const char*prevents the function being called from moving the pointer. The data pointed to however may be changed. In C++ a reference parameter char& does a similar job and may be clearer. A char* will work, but the protection is not there, and does not indicate intent to the user of the function. Clifford |
I have to write a function that gets a pointer from a user. kinda like scanf() but understands spaces(I guess). So the type of function is: int *getInteger(int **ptr, const char *prompt); Question 1: I know that char is ** ptr going to = &somevar I'm not sure what int *prompt is going to be. (maybe %d?) Question 2: I have ... |
|
|
Well that really depends on whether myFunc was declared with char* because - the programmer was too lazy to say const - because the function does actually modify the string. The first is fixed by fixing the code. The second is fixed by making a modifiable copy of the string constant, then passing that to myFunc. Neither involves a cast. > ... |
|
|
|
|
|
Hi!I have made a const char as follows:char const Ccode [] = "#include \n""#include \n""#include \n""include \n""#include \n""\n""#define TCLGO TCL_GLOBAL_ONLY\n""main(int argc, char *argv[])\n""{\n"" Tcl_Interp *interp;\n" " Tk_Window mainWindow;\n"" Tk_Window tkmain;\n" " int error;\n"" char *trace;\n"" \n""/* first we create a ... |