What is the difference between char (*ptr)[20] and char *ptr[20]?

Asked

Viewed 125 times

1

I’m studying pointers and came across these statements, but I couldn’t understand it very well. Could you help me? Thank you.

  • 3

    char (*ptr)[20] is a pointer to an array of 20 char and char *ptr[20] is an array of 20 pointers for char. If with this explanation you were able to understand the difference between one thing and another, then perhaps your difficulty is only reading the instructions in C, I recommend researching the "right-left Rule". On the other hand, if you couldn’t visualize the difference between one thing and another, it would require further study on pointers.

  • @v. Santos, put your comment as a response

  • @zentrunix, I thought about it, but my problem is that my comment seems incomplete for an answer. And to answer, I didn’t know if I explained the "right-left Rule" or if I was talking about the difference between an array pointer and a pointer array. Now, as the person who asked the question did not express itself and as I liked the answer given by another user, I imagine that there is no longer this need for me to turn the comment into an answer. Anyway, thank you very much.

1 answer

0

Good afternoon,

char (*ptr)[20] is a pointer to a 20-character array...a single pointer that leads to a "string" (char[20]), for simplicity. Ex:

*ptr -> "exemplo"

Char *ptr[20] is an array of 20 pointers... You have 20 pointers that point to characters. Ex:

*ptr[0] -> e
*ptr[1] -> x
*ptr[2] -> e
*ptr[3] -> m
*ptr[4] -> p
*ptr[5] -> l
*ptr[6] -> o

Browser other questions tagged

You are not signed in. Login or sign up in order to post.