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

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

问题描述

我是编程新手,有些部分让我在学习指针时感到困惑。





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

char * my_pointer = string;

printf( 第一个字符是%c,* my_pointer);
return 0 ;
}



输出:第一个字符是h



代码2:



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

char * my_pointer = array_of_words;

printf( %s \ n,my_pointer);

return 0 ;
}



输出:一个



我尝试过:



问题:



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



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



在第二个代码中* my_pointer = array_of_words复制array_of_the_words的第一个内容的地址是'o'?而char * my_pointer = array_of_the_words意味着我们希望* my_pointer与array_of_the_words具有相同的地址,如果printf中的my_pointer(%s \ n,my_pointer)指的是* my_pointer的内存地址,则引用'o'(?)那么为什么不呢?它产生'一个'而不产生'o'??



实际上它复制第一个字符串地址但包含所有单词?

但是为什么第一个代码在printf中使用* my_pointer而在printf中使用第二个代码my_pointer?

我知道* my_pointer引用value而my_pointer引用地址但是为什么输出不同

解决方案

查看星号的另一种方法是指针取消引用运算符。它获取指针所在的目标值,数组是数组的第一项。



在printf中,%s引用一个字符串,一个数组字符,因此需要传递数组的地址。 %c格式标志用于单个字符,因此该说明符的printf需要该数组的一个成员。输出是不同的,因为格式说明符告诉printf函数如何解释它传递的数据。数据以指针或值的形式在堆栈上传递给函数。如果堆栈上的数据格式错误,那么该函数将提供奇怪的输出,否则会使程序崩溃。


因为它们是真正不同的野兽。我建议您阅读有关该主题的教程,例如:您需要了解的关于C中指针的所有内容 [ ^ ]。


案例1:

 printf( 第一个字符是%c, * my_pointer); 



您告诉 printf 输出单个字符(%c )所以格式字符串后的参数必须是单个字符。由于该字符位于由 my_pointer 指向的数组内,因此需要从数组中提取,并且这样做的方法是使用解除引用操作符( * ),它将从指向的位置返回单个字符。



案例2:

 printf( %s \ n,my_pointer); 



在这种情况下,你告诉 printf 输出一系列字符(%s )所以你必须传递那个序列的地址。由于my_pointer指向字符数组的开头,这就是它将要做的事情。内部printf将执行以下操作:

  int  printf( char  *格式, char  * string)
{
// 处理格式并查找%s ...
while (* string!= < span class =code-string>' \ 0' // < span class =code-comment>重复直到字符串结尾
{
char c = * string; // 从指针地址获取下一个字符
// 将此字符打印到终端
++ string; // 将指针前进到下一个字符
}
}


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


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

    char *my_pointer=string;

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


output : first character is h

code 2:

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

    char *my_pointer=array_of_words;

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

    return 0;
}


output : one

What I have tried:

questions:

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?

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);

While in second code *my_pointer=array_of_words copy the address of the first content of array_of_the_words which is 'o'? And char *my_pointer=array_of_the_words means we want *my_pointer has same address as array_of_the_words which refer to'o'(?) if my_pointer in printf("%s\n",my_pointer) refers to memory address of *my_pointer Then why not it produce 'one' and not produce 'o' ??

Actually it copy the first string address but contain all the words?
But why the first code use *my_pointer in printf and second code my_pointer in printf?
I know that *my_pointer refer to value and my_pointer refer to address but why the output is different

解决方案

Another way to look at the asterisk is as the pointer dereference operator. It obtains the value of where pointer is aimed which for an array is the first item of the array.

In printf, %s refers to a string, an array of characters so the address of the array needs to be passed. The %c format flag is for a single character so one member of the array is expected by printf for that specifier. The output is different because the format specifiers tell the printf function how to interpret the data it is passed. Data is passed on the stack to the function in the form of either a pointer or a value. If the format is wrong for the data on the stack then the function will either give strange output or it will crash the program.


Because they are really different beasts. I would suggest you reading a tutorial on the topic, like, for instance: Everything you need to know about pointers in C[^].


Case 1:

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


You are telling printf to output a single character (%c) so the parameter after the format string must be a single character. Since the character is inside an array pointed to by my_pointer it needs to be extracted from the array, and the way to do that is to use the dereferencing operator (*), which will return the single character from the location pointed to.

Case 2:

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


In this case you are telling printf to output a sequence of characters (%s) so you must pass the address of that sequence. And since my_pointer points to the start of the array of characters, that is what it will do. Internally printf will do something like:

int printf(char* format, char* string)
{
    // process the format and find %s ...
    while (*string != '\0') // repeat until end of string
    {
        char c = *string;  // get the next character from the pointer address
        // print this character to the terminal
        ++string; // advance the pointer to the next character
    }
}


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

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