Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have two snippets below . Need to understand why char* is successful while int* gives an error. Trying to understand the difference in behavior between a char* and an int*
Thanks in advance for your help .

What I have tried:

snippet 1 :

int *p;
p=19;


cout<<"p val"<<*p<<endl;
cout<<"p addr"<<&p<<endl;

o/p:
-----
main.cpp: In function ‘int main()’:
main.cpp:14:3: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
p=19;

^~
===============================================================

snippet 2:

char *a;
a="fubar";


cout <<"a val = "<<*a<<endl;
cout <<"a = "<<a<<endl;
cout <<"a addr = "<<&a<<endl;

o/p:
-----

$g++ -o main *.cpp
main.cpp: In function ‘int main()’:
main.cpp:14:8: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
a="fubar";
^~~~~~~
$main
a val = f
a whole = fubar
a addr = 0x7ffed3404898
===============================================================
Posted
Updated 17-Sep-18 10:58am
v2

The message says it all - you can't convert a string constant to a 'char *'. The reason being that it would then possible to change the constant and that is not allowed. To make this compile and run, change the declaration of a to 'const char *a' and then it will work.

-edit-

Regarding snippet 1 : it is declared as a pointer and the next line assigns the value 19 to that pointer. That is not likely to be a valid address and that is what the message is referring to. Also, you probably don't really want the address of the pointer but its value which would be the address it holds. This means that line should be changed :
cout << "p addr" << p << endl;
and that will display the address the pointer holds. Here is a better use of a pointer to an integer :
C++
int ivalue = 0;
int* iptr = &ivalue;   // set iptr to the address of ivalue
*iptr = 42;            // set the value of the integer pointed to
now ivalue will have a value of 42. The key thing here is the value of a pointer is the address it holds. To get the data at that address you have to dereference the pointer and that is what the asterisk does.
 
Share this answer
 
v3
Comments
out0FBox 17-Sep-18 16:27pm    
Hi Rick,
Thanks for the reply.
However, the warning for char* is not the issue . I wanted to know why it failed for snippet 1 .

Regards
C++
int *p;
p=19;
p is a "pointer to an integer", and 19 is an integer, not an address, so the compiler is complaining that you can't assign an integer directly to a pointer.
C++
char *a;
a="fubar";
a is a "pointer to a character", but "fubar" is a string, which a a null-terminated array of characters. And the name of an array is a pointer to the first element of the array - that is in the language definition, and is fundamental to how the language works.
Since "fubar" is made of characters, it is effectively (and really) a "pointer to the first character in the string" so you can assign it without problems.
 
Share this answer
 
v2
Comments
out0FBox 17-Sep-18 16:37pm    
Thanks for the excellent explanation .

Therefore ,
char *a;
a='f'; should also give an invalid conversion error.
I just tried it and it does give the invalid conversion error .

Appreciate the help on this .
Regards
OriginalGriff 17-Sep-18 16:51pm    
You're welcome!
A constant character string in C++ is of type 'const char*', which most compilers will allow you to assign to a 'char*' (possibly with a warning). OTOH, an 'int' (e.g. 19) is never compatible with 'int*'. You must dereference the pointer thus:

*p = 19;

(There is a minor exception to this rule - a pointer may be assigned the value 0, which in this context is equivalent to nullptr. However new programs should use nullptr rather than 0.)
 
Share this answer
 
v3
Comments
out0FBox 17-Sep-18 16:40pm    
Thanks for the immediate response . I was looking for an answer close to what OriginalGriff has posted above. But this helps adds too.

Regards
The comparison is not fair: code snippets are not equivalent.
You have to compare either
C++
// snippet a.1
int *p;
p = 19;

cout << "p val"<< *p << endl;
cout << "p addr" << p << endl;


// snippet a.2
char *s;
s = 'a';

cout << "s val"<< *s << endl;
cout << "s addr" << s << endl;
Compiler output:
[...]: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
  p = 19;
      ^~
[...]. error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
  s = 'a';
      ^~~




or


C++
// snippet b.1
const int ia[] = {1,2,3};
int *p;
p = ia;

cout << "p val"<< *p << endl;
cout << "p addr" << p << endl;

// shippet b.2
char *s;
s = "alpha";

cout << "s val"<< *s << endl;
cout << "s addr" << s << endl;
Compiler output
[...]: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
  p = ia;
      ^~
[...]. warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
  s = "alpha";
      ^~~~~~~

In the above case (b snippets), the compiler actually consider char somehow special (I guess it is a C legacy).




[update]
Quote:
But what i do not know is why a regular integer pointer cannot point to a const integer.
A pointer can point to an integer constant, for instance:
C
const int myconst = 10;
const int * p = &myconst; // p is a pointer to const (NOT a const pointer)
printf("Address = %p, value = %d\n", p, *p);

However you cannot arbitrarly assign an address to it (as in your example):
const int * p = 19; // ERROR

is plainly wrong.


Quote:
My first assumption is that , allowing so would allow the value of the const int to be changed through the pointer
This is fairly correct: you can assign the address of a integer const only to a pointer to integer const (NOT a const pointer).


Quote:
econd assumption is that a const pointer is to be used to point to a const variable
Nope.
  • A pointer to integer const is to be used to point a integer const.
  • A const pointer is a pointer whoose value (that is the address it points) cannot be changed.
E.g.
C
const int myconst = 10;
  const int * p = &myconst; // p is pointer to const int
  int i = 0;
  int j = 0;
  int * const q = &i; // q is a const pointer to int
  *q = 50; // i.e. i=50, it is OK, since q points to a variable 
   q = &j; // ERROR, cannot change the address q points to 

[/update]
 
Share this answer
 
v2
Comments
out0FBox 19-Sep-18 18:51pm    
Yes. The comparison is off in my example question.
As per snippet b.1, if the array is not a cont integer, there should be no error.
But what i do not know is why a regular integer pointer cannot point to a const integer.
My first assumption is that , allowing so would allow the value of the const int to be changed through the pointer. Second assumption is that a const pointer is to be used to point to a const variable.
Do let me know if my assumptions are correct.
CPallini 20-Sep-18 4:18am    
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900