我正在寻找学习 C 并且很难掌握字符串指针的概念(通常只是指针)。
我有以下程序:
#include<stdio.h>
#include<string.h>
int main()
{
// char test[6] = "hello";
char *test = "hello"; // test is a pointer to 'h'
int size_of_test_pt = sizeof(test); // this is the size of a pointer? which is 8 bytes.
int size_of_test = sizeof(*test); // this is the size of h, which is 1.
printf("\nSize of pointer to test: %d\n", size_of_test_pt); // prints 8
printf("\nSize of test: %d\n", size_of_test); // prints 1
printf("\nPrint %s\n", test); // why does this print 'hello', I thought test was a pointer?
printf("\nPrint %c\n", *test); // this is printing the first character of hello, I thought this would print hello.
printf("\nPrint %i\n", *test); // this prints 104...is this ASCII for h
return 0;
}
在最后 3 个打印语句之前,一切都是有意义的。如果 test 是一个指针变量。为什么 printf 打印出单词“hello”而不是地址?
对于printf("\nPrint %c\n", *test)
调用是否正确理解我正在取消引用测试,这是一个地址并访问第一个元素,然后将其打印到屏幕上?