简体   繁体   English

c编程中的数组指针

[英]Array pointers in c programming

I am not sure why the out put giving me the first letter for each word, isn't it supposed to give me the whole word?我不知道为什么输出给我每个单词的第一个字母,不应该给我整个单词吗?

#include <stdio.h>

int main()
{   
    char *fruit[] = {
        "watermelon", "banana", "pear", "apple"};
    
    int x;
    
    for(x = 0;x<4; x++){
        putchar((** fruit) +x );
        putchar('\n');                                         
    }                                                          
    return 0;
}   

putchar prints a single character to the standard output - it cannot be using to print an entire string (unless you iterate that string manually, and print each character individually). putchar将单个字符打印到标准 output - 它不能用于打印整个字符串(除非您手动迭代该字符串,并单独打印每个字符)。

This expression这个表达

(** fruit)

yields the first character of your first string.产生第一个字符串的第一个字符。 Each iteration you then add x to the value of this character.然后每次迭代都将x添加到此字符的 (ie, 'w' + 0 == 'w' ... 'w' + 3 == 'z' ). (即'w' + 0 == 'w' ... 'w' + 3 == 'z' )。

Instead of this, use puts to print a complete string (followed by a newline), and usearray subscription to make the code more readable ( fruit[x] is equivalent to *((fruit) + (x)) ) .取而代之的是,使用puts打印一个完整的字符串(后跟换行符),并使用数组订阅使代码更具可读性fruit[x]相当于*((fruit) + (x))

#include <stdio.h>

int main(void)
{   
    char *fruit[] = {
        "watermelon", "banana", "pear", "apple"  
    };
    
    int x;
    
    for(x = 0; x < 4; x++) {
        puts(fruit[x]);                                      
    }                                                          
    
    return 0;
}

There are some problems with the code.代码有一些问题。

An array is group of variables of same data type in contagious memory locations.数组是传染性 memory 位置中相同数据类型的一组变量。

int a[4] = {1, 4, 2, 5};

that means elements 1, 2, 4, 5 are stored in continuous memory locations and the group is represented by name 'a'.这意味着元素 1、2、4、5 存储在连续的 memory 位置,并且组由名称“a”表示。

When you refer array name, you are referring to memory location of first element in the array.当您引用数组名称时,您指的是数组中第一个元素的 memory 位置。 Now if you refer a in your code, you are referring &a[0].现在,如果您在代码中引用a ,您指的是 &a[0]。

a -> address of first element.
a + 1 -> address of second element.

if you dereference them.如果你取消引用它们。

*a -> first element in the array
*(a + 1) -> second element in the array

In your case,在你的情况下,

char *fruit[] = {
        "watermelon", "banana", "pear", "apple"};

fruit is array of character pointers. fruit 是字符指针数组。

*fruit -> "watermelon"
*(fruit + 1) -> "banana"

In the same way,同样的方式,

** fruit means you are dereferencing the first element two times. ** fruit 表示您将第一个元素取消引用两次。 First dereference will give "watermelon".首先取消引用将给出“西瓜”。 Second dereference should give 'w'.第二次取消引用应该给出'w'。

In the loop you are always using (** fruit) which will always give you the character 'w'.在循环中,你总是使用 (**fruit) ,它总是会给你字符 'w'。

(** fruit) + 0 -> 'w' (you are adding 0 to 'w')
(** fruit) + 1 -> 'x' (you are adding 1 to 'w')
(** fruit) + 2 -> 'y' (you are adding 2 to 'w')
(** fruit) + 3 -> 'z' (you are adding 3 to 'w')

Second issue is putchar() prints one character to the output.第二个问题是 putchar() 将一个字符打印到 output。 You need to use puts() or print().您需要使用 puts() 或 print()。

Modified code as below.修改后的代码如下。

#include <stdio.h>

int main()
{   
    char *fruit[] = {
        "watermelon", "banana", "pear", "apple"};
    
    int x;
    
    for(x = 0; x < 4; x++){
        printf("%s", *(fruit + x));
        printf("\n");

        puts(*(fruit + x));
        putchar('\n');                                         
    }                                                          
    return 0;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM