Is Array a C pointer?

Asked

Viewed 140 times

6

I am reading a book about C pointers, but as I am new to such a language, still for me the concept of pointers is a bit complex, and when reading the following definition, I came to the question whether an array is a pointer.

Pointer variables

The actual size required is not Too Important so long as we have a way of informing the Compiler that what we want to store is an address. Such a variable is called a Pointer variable.

By definition then, arrays are pointers, right ? In some places, if it says that the array it decays to a pointer, it would be correct to also say this ?

  • 1

    I know little about C, but by the knowledge I have, pointers are not the array itself, but a way to identify it.

2 answers

6


A pointer is a special type of variable that stores addresses.

If a pointer p stores the address of a variable i, we can say p points to i or p is the address of i. (In more abstract terms, it is said that p is a reference to the variable i.) If a pointer p has different value than NULL then *p is the value of the object pointed by p. (Do not confuse this use of * with the multiplication operator! ) For example, if i is a variable and p vale &i then say *p is the same as saying i.

Let’s go to the examples:

  1. char exemplo[20]
  2. char *exemplo[20]

What is the difference between the 2?

In the first example there is a array with 20 chars. In the second there is a array with 20 pointer to char.

Credits: Maniero

  • Very good your answer, and I also particularly liked that you put the proper credits to a response that you took as an example. + 1 and correct answer.

  • 2

    If I had already answered was not the case to vote to close as duplicate?

  • 2

    @Bigown did not know it fit as duplicate. I thought that because it is more specific, could be considered different.

  • 1

    @bigown In the other question the author asks if an array becomes a pointer, and what is the difference between them. In mine I’m asking if an array is already a pointer ,not the difference between them, and I’m not asking how they work internally. If you read my final part of the question, you will see that it is not a duplicate. If so, even the one you answered in the other question, it is a duplicate because it has another similar question. This generates an avalanche of endless duplicates, the issue here is specific to a situation, and not in general.

  • @Monteiro But the answer is this. Without knowing how it works internally we can not answer this properly. Even the two questions use the same phrase to ask. Francis' answer is only a summary of what is there, it does not add anything new. Exactly we are trying to avoid an avalanche of endless duplicates. As there is nothing new here this is a duplicate. The one I answered has nothing like it, has on the same subject, but there in mine has much new information.

  • @bigown On this I agree, the questions in the title are the same, and the answer is based on yours. But the solution is really to close this question ?

  • It’s not just in the title, it’s in the body too, it even uses the same phrase. The solution is to close every question that has a proper answer elsewhere on the site. And note that it wasn’t even me who closed it. Even other people would vote to close it also as it was said in the chat.

Show 2 more comments

3

Variables that are pointers have an asterisk in their declaration. Then the array will only be pointer if it is with that asterisk. Samples:

Pointedly:

int *x;
char *array[];
float *peso;

Hands-off:

int x;
char array[];
float peso;

Browser other questions tagged

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