简体   繁体   中英

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

This expression

(** fruit)

yields the first character of your first string. Each iteration you then add x to the value of this character. (ie, '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)) ) .

#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.

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'.

When you refer array name, you are referring to memory location of first element in the array. Now if you refer a in your code, you are referring &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 -> "watermelon"
*(fruit + 1) -> "banana"

In the same way,

** fruit means you are dereferencing the first element two times. First dereference will give "watermelon". Second dereference should give 'w'.

In the loop you are always using (** fruit) which will always give you the character '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. You need to use puts() or 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;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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