灵感来自K&R第5.5节中描述的程序:
void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}
Run Code Online (Sandbox Code Playgroud)
C程序
if ('\0') { printf("\'\\0\' -> true \n"); }
else { printf("\'\\0\' -> false\n"); }
if ("\0") { printf("\"\\0\" -> true \n"); }
else { printf("\"\\0\" -> false\n"); }
Run Code Online (Sandbox Code Playgroud)
版画
'\0' -> false
"\0" -> true
Run Code Online (Sandbox Code Playgroud)
为什么'\0'和"\0"评价不同的C 2
clang版本3.8.0
我的理解是数组只是指向一系列值的常量指针,当你在C中声明一个数组时,你就是声明一个指针并为它所指向的序列分配空间.
但这让我感到困惑:以下代码:
char y[20];
char *z = y;
printf("y size is %lu\n", sizeof(y));
printf("y is %p\n", y);
printf("z size is %lu\n", sizeof(z));
printf("z is %p\n", z);
Run Code Online (Sandbox Code Playgroud)
使用Apple GCC编译时会得到以下结果:
y size is 20
y is 0x7fff5fbff930
z size is 8
z is 0x7fff5fbff930
Run Code Online (Sandbox Code Playgroud)
(我的机器是64位,指针长8个字节).
如果'y'是常量指针,为什么它的大小为20,就像它指向的值序列一样?变量名'y'是否在编译时被内存地址替换为适当的?那么数组是C中的某种语法糖,它在编译时只是转换为指针的东西吗?
在以下规则中,当数组衰减到指针时:
左值[见2.5问题]型阵列的-T出现在表达衰变(有三个例外)转换成一个指向它的第一个元素的; 结果指针的类型是指向T的指针.
(例外情况是,当阵列是的sizeof或&运算符的操作数,或为字符数组一个文本字符串初始化.)
如何理解数组是"字符数组的文字字符串初始值设定项"的情况?请举个例子.
谢谢!
#include <stdio.h>
#define N 10
char str2[N]={"Hello"};
int main(){
printf("sizeof(str2): %d bytes\n", sizeof(str2));
printf("sizeof(&str2): %d bytes\n", sizeof(&str2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof(str2): 10 bytes
sizeof(&str2): 4 bytes
Run Code Online (Sandbox Code Playgroud)
我知道str2单独是数组中第一个元素的地址str2.并且当str2它的参数何时sizeof返回整个数组str2的大小.
另外,&str2也是arr中第一个元素的地址,str2但是来自不同的类型(char (*)[N]==指向数组的指针).但是&str2当它是一个论证时,它是如何表现的sizeof?
我正在尝试使用C指针文献.在其中一个插图中,我遇到了以下代码.
# include <stdio.h>
int main()
{
static int a[]={0,1,2,3,4};
static int *p[]={a, a+1, a+2, a+3, a+4};
int **ptr;
ptr =p;
**ptr++;
printf("%d %d %d\n", ptr-p, *ptr-a, **ptr);
*++*ptr;
printf("%d %d %d\n", ptr-p, *ptr-a, **ptr);
++**ptr;
printf("%d %d %d\n", ptr-p, *ptr-a, **ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到输出为.
1 1 1
1 2 2
1 2 3
Run Code Online (Sandbox Code Playgroud)
我在为这个输出辩护时遇到了问题.我在副本上制作了很多盒子,以便轻松掌握问题.我能够证明输出的合理性1 1 1,我的麻烦始于声明,*++*ptr.
因为,一元运算符从右到左执行.因此,*ptr将首先处理,然后ptr将增加值.在这个增量之后,我不确定会发生什么,书中说不知何故p也会增加指向此数组中的下一个元素.输出1 2 2只能通过增量来实现p.
我不确定这种问题是否恰好适合stackoverflow.
我尽我所能,浪费了至少10页,上面画着盒子.
任何澄清将不胜感激.
在我的代码中:
char *str[] = {"forgs", "do", "not", "die"};
printf("%d %d", sizeof(str), sizeof(str[0]));
Run Code Online (Sandbox Code Playgroud)
我得到了输出12 2,所以我怀疑是:
str和str[0]是字符指针,对不对?代码1
#include<stdio.h>
int main(int argc, char *argv[])
{
int j;
printf("%d", argv[1][0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码2
#include<stdio.h>
int main(int argc, char **argv)
{
int j;
printf("%d", argv[1][0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CODE 1和CODE 2都提供相同的输出.但是CODE 1和CODE 2中主要功能的参数2是不同的.在编译时在数据部分上创建指针数组.argv是指针数组.然后我们应该在main函数中声明参数作为指向字符的指针,即**argv.在CODE 1中声明如何正确?
我的代码如下:
main() {
int array[5] = {3,6,9,-8,1};
printf("the size of the array is %d\n", sizeof(array));
printf("the address of array is %p\n", array);
printf("the address of array is %p\n", &array);
int * x = array;
printf("the address of x is %p\n", x);
printf("the size of x is %d\n", sizeof(x));
}
Run Code Online (Sandbox Code Playgroud)
输出是
the size of the array is 20
the address of array is 0x7fff02309560
the address of array is 0x7fff02309560
the address of x is 0x7fff02309560
the size of x is …Run Code Online (Sandbox Code Playgroud) 我不明白以下代码的结果:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[4]={1, 3, 5, 6};
//suppose a is stored at location 2010
printf("%d\n", a + 2);
printf("%d", a++);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么第二个printf函数会产生以下错误?
error: lvalue required as increment operand