0

Is the following the correct approach to assign a pointer to a character array?

char s[6] = "hello";
printf("%s\n", s);

char *ptrx = &s[0];
printf("The first char is: %c\n", *ptrx);

At first I thought the correct way to do it would be:

char *ptrx = &s;

But that gave me some compiler errors. Why would the first approach be correct but not the second?

mrflash818
  • 930
  • 13
  • 24
  • 2
    `char *ptrx` is not a "pointer to an array". It is a pointer to a `char`, to an array element in your case. Which is why you initialize (not "assign") it with an address of an array element. A pointer to an array of `char [6]` would be declared as `char (*ptrx)[6]`, which is very different from what you have now. – AnT stands with Russia Sep 02 '19 at 03:51

1 Answers1

1

Examples:

Example 1: C program to understand difference between pointer to an integer and pointer to an array of integers.

#include<stdio.h> 

int main() 
{ 
    // Pointer to an integer 
    int *p; 

    // Pointer to an array of 5 integers 
    int (*ptr)[5]; 
    int arr[5]; 

    // Points to 0th element of the arr. 
    p = arr; 

    // Points to the whole array arr. 
    ptr = &arr; 

    printf("p = %p, ptr = %p\n", p, ptr); 

    p++; 
    ptr++; 

    printf("p = %p, ptr = %p\n", p, ptr); 

    return 0; 
} 

Example 2: C program to illustrate sizes of pointer of array

 #include<stdio.h> 

    int main() 
    { 
        int arr[] = { 3, 5, 6, 7, 9 }; 
        int *p = arr; 
        int (*ptr)[5] = &arr; 

        printf("p = %p, ptr = %p\n", p, ptr); 
        printf("*p = %d, *ptr = %p\n", *p, *ptr); 

        printf("sizeof(p) = %zu, sizeof(*p) = %zu\n", 
                            sizeof(p), sizeof(*p)); 
        printf("sizeof(ptr) = %zu, sizeof(*ptr) = %zu\n", 
                            sizeof(ptr), sizeof(*ptr)); 
        return 0; 
    } 

Example 3: C program to print the elements of 3-D array using pointer notation

#include<stdio.h> 
int main() 
{ 
int arr[2][3][2] = { 
                    { 
                        {5, 10}, 
                        {6, 11}, 
                        {7, 12}, 
                    }, 
                    { 
                        {20, 30}, 
                        {21, 31}, 
                        {22, 32}, 
                    } 
                    }; 
int i, j, k; 
for (i = 0; i < 2; i++) 
{ 
    for (j = 0; j < 3; j++) 
    { 
    for (k = 0; k < 2; k++) 
        printf("%d\t", *(*(*(arr + i) + j) +k)); 
    printf("\n"); 
    } 
} 

return 0; 
} 

Hope this can help you to understand.

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • 1
    Why use `"%lu"` with `sizeof()` instead of `"zu"`? – chux - Reinstate Monica Sep 02 '19 at 04:05
  • A printf format specifier follows the form `%[flags][width][.precision][length]specifier.` `u` is a specifier meaning `"unsigned decimal integer"` `l ` is a length modifier meaning `"long".` The length modifier should go before the conversion specifier, which means `%lu`, It's not a rule, you can modify as you wish, to your own way – Otávio Barreto Sep 02 '19 at 04:13
  • 1
    @OtávioBarreto `%lu` would be the wrong specifier for `size_t`, if `size_t` is not a typedef for `unsigned long`. For example some systems have `size_t` as a typedef for `unsigned int`, or `unsigned long long`. – M.M Sep 02 '19 at 04:23
  • 1
    C specifies `sizeof` returns a type of `size_t`. This may differ from `unsigned long`. C also specs `"%zu"` matches a `size_t` in `*printf()`. Using `"%lu"` with `sizeof` is not what C specifies as matching. It's not a rule, you can modify as you wish, to your own way – chux - Reinstate Monica Sep 02 '19 at 04:34
  • @AnttiHaapala if that's the case it's fixed. – Otávio Barreto Sep 02 '19 at 12:50