为什么带有星号和不带星号的指针变量在printf中表现不同? [英] why pointer variable with asterisk and without asterisk behave differently in printf?

查看:281
本文介绍了为什么带有星号和不带星号的指针变量在printf中表现不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,在学习指针时,有些地方使我感到困惑。

I m new to programming, there is some part that made me confuse when learning pointer.

code1:

int main()
{
    char string[]="hello";

    char *my_pointer=string;

    printf("first character is %c", *my_pointer);
    return 0;
}

输出:第一个字符为h

output : first character is h

代码2:

int main()
{    
    char array_of_words[]="one\0two\0three";

    char *my_pointer=array_of_words;

    printf("%s \n",my_pointer);

    return 0;
}

输出:一个

问题:

我在这里感到困惑,第一个代码中的printf函数部分使用星号表示要指向内部指针(my_pointer)中的内容变量字符串,它指向数组的指针字符串,指向数组 hello中第一个单词的内存地址。我的理解是真的吗?

I'm confuse here, the printf function part in the first code using asterisk symbol means to point what is inside pointer (my_pointer) which is the address of variable string that refer to the pointer string for an array that point the memory address of the first word in array "hello". is my understanding true?

以及当我在printf中更改%c时(第一个字符为%c,* my_pointer);
到%s,程序崩溃。我想知道为什么我不能使用%s
,如果我在printf中的my_pointer中不使用星号,会有什么不同(第一个字符为%c,my_pointer);

and also when I change %c in printf("first character is %c", *my_pointer); to %s, the program crash. i want to know why i cant use %s and what is the different if i dont use asterisk in my_pointer in printf("first character is %c", my_pointer);

在第二个代码中,my_pointer变量在{printf(%s \n,my_pointer);}中未使用星号(*)。在此代码中,my_pointer是引用array_of_words变量的地址还是my_pointer的内部?以及为什么输出为一个而不是 o?

While in second code my_pointer variable didnt use asterisk(*) in {printf("%s \n",my_pointer);}. in this code is my_pointer refer to the address of array_of_words variable or what is inside of my_pointer? and also why the output is 'one' not 'o'?

推荐答案

当您声明 char字符串时[] = Hello 您声明一个字符数组。

When you declare char string[]="Hello" you declare an array of characters.

string 点到数组的第一个元素。 * 被称为解引用运算符。

string points to the first element of the array. * is known as the dereferencing operator.

假设 ptr 是整数指针。 * ptr 将为您提供指针 ptr 所指向的内存位置的内容。在声明时,必须将其声明为 int * ptr ,这样编译器才能知道ptr是一个指针变量(因此在此处带有星号)。

Suppose ptr is an integer pointer. *ptr will give you the content of the memory location pointed by the pointer ptr. At the time of declaration you have to declare it as int *ptr so that the compiler knows that ptr is a pointer variable(hence the asterix there).

当您执行 printf(第一个字符为%c,* my_pointer); 该函数需要一个与%c 相对应的字符。声明 char * string char string [] 大致(不完全)等效。 my_pointer 是字符类型指针。 * my_pointer 取消引用 my_pointer 。换句话说, * my_pointer 为您提供 my_pointer 的内容,即字符串中的第一个字符。

when you execute printf("first character is %c", *my_pointer); the function expects a character corresponding to %c. The declaration char *string and char string[] are roughly(not completely) equivalent. my_pointer is a character type pointer. *my_pointer dereferences my_pointer. In other words *my_pointer gives you the content of my_pointerie the first character in your string.

但是%s 需要一个字符串(字符数组) ,基本上是一个char指针)作为 printf 中的相应参数。 * my_pointer 是一个字符。 my_pointer 可以视为字符串。同样, print 会将参数视为字符串,并输出who; e直到 \0

However a %s expects a string (a character array, which is basically a char pointer) as the corresponding argument in printf. *my_pointer is a character. my_pointer can be treated as a string. Also print will treat the argument as a string and print the who;e thing till \0.

my_pointer = array_of_words 分配 array_of_words 到 my_pointer

array_of_words [] 是一个数组,因此 array_of_words 基本上是数组第一个元素的内存地址(注意没有[])。实际上, my_pointer 现在指向数组的第一个元素。

array_of_words[] is an array, so the value of array_of_words is basically the memory address of the first element of the array(notice the absence of []). In effect my_pointer now points to the first element of the array.

希望这可以解释。

这篇关于为什么带有星号和不带星号的指针变量在printf中表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆