问题描述
我有两个问题
问题编号1
I have two questions
Question Number 1
Part1.c
int *p;
int a[]={1};
p=a;
printf("%d",p);
part2.c
char *q;
char b[]="welcome";
q=b;
printf("%s",b);
$ 1 $ b在part1.c中它打印变量的地址在part2中.c它打印的字符串值是多少?只有返回类型有何不同?
问题2
in part1.c it prints the address of variable where in part2.c it prints the values that is string? Only the return type is different how?
Question No 2
int a=10;
int const *p=&a;
a=20;
p=&a;
printf("%d",*p);
以上程序指向常量。在这里我改变了a的值,但它没有显示错误原因?指向常量的指针是否应该将变量声明为const(即)
Above program is pointer to constant . Here i have changed the value of a but it is not showing errors why? Does pointer to constant mean that variable should be declared as const (i.e)
const int a =10;
?
推荐答案
int i1 = 0;
const int i2 = 0;
char* c1 = "...";
char* const c2 = "...";
char const* c3 = "...";
const char* c4 = "...";
const char* const c5 = "...";
我想你有i1和i2想通了。指针使它复杂一点。
c1是一个指向char的指针,可以随意修改。 c2是一个指向char的const指针(不能改变c2指向的位置,但你可以改变它所指向的位置)。 c3和c4是一回事。 c3 / c4是指向const char的指针(你可以改变c3 / c4点的位置,但不能改变它们指向的位置)。 c5是一个指向const char的const指针(不能改变它指向的位置或它指向的位置)。
将''typedef'引入混音邀请一个全新的维度。
I think you have i1 and i2 figured out. Pointers complicate it a little.
c1 is a pointer to char that can be modified at will. c2 is a const pointer to char (can''t change where c2 points but you can change what it points to). c3 and c4 are the same thing. c3/c4 are pointer to const char (you can change where c3/c4 points but can''t change what they point to). c5 is a const pointer to const char (can''t change where it points or what it points to).
Introduction of ''typedef'' into the mix invites a whole new dimension.
这篇关于char数组,整数数组和常量指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!