Code 1: no warning , no error . work perfect.
#include <stdio.h>
void printP(const char *p)
{
printf("const char *p is : %p\n",p);
if( p )
printf("%s\n",p);
}
void printP2P(const char **p)
{
printf("const char **p pointer to : %p\n",*p);
if( p &&(*p) )
printf("%s\n",*p);
}
int main()
{
char a[] = "Hello World";
const char *p = a;
const char **p2p = &p;
printP(p);
printP2P(p2p);
return 0;
}
Code 2: can work.
warning: initialization from incompatible pointer type [enabled by default]
const char *p = a;
==>
char *p = a;
Code 3 : Segementation fault.
warning: initialization from incompatible pointer type [enabled by default]
const char **p2p = &p;
==>
const char **p2p = &a;
Problem:
- Why code 2 could work but code 3 got a segement fault ?
2.When a pass chat *
to const char *
, no warning or error happened ,but when I assignment char **
to const char **
, I got a warning, why?