C :: Distinguish Between Character Constant And String Constant
Feb 20, 2015Can distinguish between character constant and string constant giving examples
View 1 RepliesCan distinguish between character constant and string constant giving examples
View 1 RepliesI keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?
#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {
[code]....
Here's the line of code with the warning
Code: if((addressbook[3][num][4]=='-')&&(strlen(addressbook[3][num])=='10'))
Or what might be the possible causes of such a warning?
How to do this?
View 10 Replies View RelatedHere's my code. The error's at the last line.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define max_con 30
void add_contact();
void edit_contact();
void del_contact();
void list_contact();
[Code] .....
In this code, i declared a string constant and trying to print the length of string. I know that if i write char a1[7] or char a1[] than it runs and give aggregate output, but in this case it is giving double length of string.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}
[code]....
I have this old c function that takes as an argument a char*. but my app is written in cpp so i used std::string to store my strings. to pass a char * to the function i tried :
Code:
char *input = new char[args.i.length() + 1];
strcpy(input, args.i.c_str()); and then Code: function (input);
and
function ( (char *)input);
But I still get this warning message which i would like to fix:
Warning: deprecated conversion from string constant to "char*" [-Wwrite-strings]
I wrote the following code but i got this error: Deprecated conversion from string constant to 'char*':
#include <iostream>
using namespace std;
#include <stdio.h>
#include <windows.h>
#include <winuser.h>
int Save (int key_stroke, char *file);
void Stealth();
[Code] ....
I have 1 struture:
Code:
struct SetText{
int PosX;
int PosY;
char *Text;
};
And heres how i add the values:
Code:
SetText *x=new SetText;x->PosX=5;
x->PosX=6;
x->Text ="hello mother";
why i receive that warning in: x->Text ="hello mother"; ?
"deprecated conversion from string constant to 'char*' [-Wwrite-strings]"
#include <iostream>
class Hello {
public:
void Test() {
[Code].....
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
line 27 and line 88 Im having a hard time figuring it out what the error is.
#include<iostream>
#include <cmath>
#include<algorithm>
[Code]....
why all strings are always constant?
View 2 Replies View RelatedWhy pointer cannot be initialized with a constant like.
Code: int *p = 3000;
I have never seen anyone pass by const copy and there probably is a reason. I know that the compiler ignores top level const-ness of function arguments. There are functions which take arguments without manipulating those arguments return the result, for example the C Standard Library funcion double sqrt (double x). The function shouldn't modify it's argument, but it can since the argument isn't const.Take these two functions for example:
double square_root_1(double arg)
{
arg = 7; // we won't get the desired results
return arg * arg;
[code]....
So isn't it better to pass by const copy to make sure that you (or someone else) don't by accident modify the argument? The only disadvantage I see is that it makes the code too verbose.
//VALID:
const int CONSTANT=100;
int integerArray[CONSTANT]={ 0 };
//but after getting input let's say:
cin>>randomInteger;
int integerArray[randomInteger]; // This is invalid.
// VISUAL STUDIO 13 Says : randomInteger must be a constant; If so?
const int CONSTANT=randomInteger; //This is also invalid.
How to get user defined
//Input in a constant variable?
How to resolve this? I know dynamically allocation other than this.
I am using VISUAL STUDIO 13 ....
What is the difference between:
const int testFunction() &
int testFunction() const
#include<iostream>
using namespace std;
class Student{
public:
int age;
int rollNo,marks;
string name;
void AddEntry();
[Code] .....
error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|
why and how can I solve it?
I heard that const shall be preferred over #define . So I start to change my program accordingly.
But then below error message occurs during compilation:
#include "common.h"
#include "definition.h"
#include "particle.h"
int main() {
Particle *p = new Particle();
[Code] .....
I guess the error occurs because, when the line 9 of particle.h (File 4) is compiled, value of const int dimension is not seen by the compiler.
I have a class that I'm going to use to store a category. Right now there are seven options, but there is the potential for a whole lot more in the future. So I started off by storing an integer as the private member. I then used static constants to define the numeric values to represent each category, then a set of static constant strings that corresponds to those numbers in case I need their actual names. Finally I set up some static functions to convert between the integer value and the string, and vice versa.
I'm not sure if this is the best way to go about this. For one it makes the categories names and designations unchangeable. I thought that storing them in a file would be a better option, but then i needed a container that is the equivalent of a constant.
I thought of defining a class to contain an int and the associated string. It would be designed so that it can only be initialized with both items. Then provide no functionality to change the contents. So I've basically created my own constant.
Why do I get the error 'rec' cannot appear in a constant-expression ?
I have the following definitions:
... string rec[6];
list<rec> musicList;
...
double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
Where exactly we can use this concept in real time programming ?
Instead of using:
Code:
x=x+k
y=y+k
z=z+k
Is there a more elegant method of adding the same constant to many variables?
Something like: Code: (x, y, z) = (x, y, z) + k ??
I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:
bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)
Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().
If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.
Following is my code. MyCompany::company is a QString.
const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);
I have defined a pointer ptr2 as follows:
const void * ptr2 ( new A(20) );
In this case I expect the data pointed by ptr2 must be constant. But when running the program, the data could still be changed successfully. Why ?
Below is the program:
#include <iostream>
using namespace std;
[URL] ....
class A {
public:
A( int ID ) {
cout << "--Constructor with ID=" << ID << endl;
this->ID = ID;
[Code] .....
The output:
--Constructor with ID=20
ID of ptr2 is 20
ID of ptr2 is 21 after changed!
-~Destructor with ID=21
End of main()
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
Question: What is the significance of "const" in const void * ?
I am having a problem concerning a static const member variable I want to use to set a certain property of my class during development time. The question actually concerns proper implementation as I do have a solution that "works" at least. The variable should denote the size of a member array which I don't want to allocate on the heap due to serious performance issues. So here is my code:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = 256;
private:
int MyArray[MyArraySize];
};
This works but it's not nice for two reasons:
1) It doesn't separate interface from implementation. I would prefer to define the variable in the corresponding .cpp file but it doesn't work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
[Code] .....
If I delete the line int MyArray[MyArraySize]; the above code works but when I use it to define the size of the array I get a "constant expression expected" error for the line int MyArray[MyArraySize]; which makes sense as the compiler does not know the value of MyArraySize when he reaches int MyArray[MyArraySize]; and therefore can not allocate the memory. Of course I can move MyArray to the heap like that:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
static const int MyValue;
[Code] .....
But as I mentioned before this causes a remarkable loss of performance.
Something like the following does not work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = (int) pow(2, 8);
private:
int MyArray[MyArraySize];
};
This gives a "constant expression expected" error for the line static const int MyArraySize = (int) pow(2, 8);
Interestingly the following code works:
//MyClass.h
class MyClass{
public:
static const int MyValue;
};
//MyClass.cpp
#include "MyClass.h"
const int MyClass::MyValue = (int) pow(2, 8);
So if I use pow outside of the class definition I get no errors. Is there any solution to those problems? So what I want is:
1) Don't allocate the array on the heap
2) Separate interface from implementation
3) Being able to use functions like pow to define MyArraySize
4) Not use global variables
I've go this code that I need to use a Named Constant for the price of a t-shirt.
I've already done the code from a previous lab for college and I'm not sure how to proceed.
Here is the code:
int main()
{
int tno, price=12,cost;
float discount;
[Code].....
not asking to have this done for me just a hint at where to put the Named Constant