Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of 'char (*p)[5];'?

I'm trying to grasp the differences between these three declarations:

char p[5];
char *p[5];
char (*p)[5];

I'm trying to find this out by doing some tests, because every guide of reading declarations and stuff like that has not helped me so far. I wrote this little program and it's not working (I've tried other kinds of use of the third declaration and I've ran out of options):

#include <stdio.h>                                                              
#include <string.h>                                                             
#include <stdlib.h>                                                             
                                                                                
int main(void) {                                                                
        char p1[5];                                                             
        char *p2[5];                                                            
        char (*p3)[5];                                                          
                                                                                
        strcpy(p1, "dead");                                                     
                                                                                
        p2[0] = (char *) malloc(5 * sizeof(char));                              
        strcpy(p2[0], "beef");                                                  
                                                                                
        p3[0] = (char *) malloc(5 * sizeof(char));                              
        strcpy(p3[0], "char");                                                  
                                                                                
        printf("p1 = %s\np2[0] = %s\np3[0] = %s\n", p1, p2[0], p3[0]);          
                                                                                
        return 0;                                                               
}

The first and second works alright, and I've understood what they do. What is the meaning of the third declaration and the correct way to use it?

Thank you!

like image 543
jpmelos Avatar asked Jun 24 '11 12:06

jpmelos


People also ask

What does char * p mean in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What is the meaning of char * p?

char (*p)[] this is a pointer to an array. this will point to an array of characters.

What is the meaning of int (* p 5?

int (*p)[5] is called a Pointer to an Array(Array Pointer). We can declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays. In this example p is pointer that can point to an array of 5 integers.

How does a char * work?

The char data type is an integral type, meaning the underlying value is stored as an integer. Similar to how a Boolean value 0 is interpreted as false and non-zero is interpreted as true , the integer stored by a char variable are intepreted as an ASCII character .


2 Answers

The third is a pointer to an array of 5 chars, whereas the second is an array of 5 pointers to char.

Imagine it like this:

________          _____________________
|0x7777| -------> | H | e | l | l | o |
|______|          |_0_|_1_|_2_|_3_|_4_|
    p             ^
                  |
                  0x7777  

Whereas the second one looks like that:

   "abc"  "def"  "ghi"  "jkl"  "mno"
    ^      ^      ^      ^      ^
    |      |      |      |      |
____________________________________
|0x9999|0x7777|0x3333|0x2222|0x6666|
|___0__|___1__|___2__|___3__|___4__|
                  p

This is one of the cases where understanding the difference between pointers and arrays is crucial. An array is an object whose size is the size of each of its elements times the count, whereas a pointer is merely an address.

In the second case, sizeof(p) will yield 5 * the_size_of_a_pointer.

In the third case, sizeof(p) will yield the_size_of_a_pointer, which is usually 4 or 8, depending on the target machine.

like image 107
Blagovest Buyukliev Avatar answered Nov 15 '22 15:11

Blagovest Buyukliev


It's a pointer to an array of chars. It is explained in the C FAQ. In the future, when you don't understand a declaration, use cdecl or cdecl(1).

like image 39
cnicutar Avatar answered Nov 15 '22 15:11

cnicutar