If array is the same as pointer, why does one need to be copied to a variable and another need not?

Asked

Viewed 393 times

5

In that reply Maniero said that if the member of the structure was a pointer it would not need to copy the string into it. But arrays are not pointers? Why is it different?

2 answers

5


First, arrays are not pointers. They serve different purposes.

The main difference between them is that the array reserve memory space for its value, the pointer only reserves space for the pointer itself. The value will be in another place pointed by that pointer. Running this code we can observe this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    char *nome;
} Tipo1;

typedef struct {
    char nome[30];
} Tipo2;

int main(void) {
    Tipo1 x;
    Tipo2 y;
    printf("%d\n", sizeof(x));
    printf("%d\n", sizeof(y));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The first has size 4 because the pointer is this size. A string will have to be allocated elsewhere. There you put the pointer. Pointer is a scalar type, it’s simple, it’s something the computer can handle directly. You can copy a value there into the struct in a simple operation.

This pointer can point to any valid memory location. It could point to an area of the stack, but it is rare. In some situations you can point to the static area of the running application. This is what happened in that question. But that’s not so common because this string cannot be changed, it is in read-only area. The most common is to reference the memory in the heap allocated by a malloc().

You can exchange the value of this member of struct Just changing the pointer. If the pointer originally pointed to a static area of memory and needs to change its value, you must create a new one string and exchange the pointer for another that points to this new string. This is necessary because we cannot change static area of memory.

Using the pointer has some advantages, the main ones:

  • to string can be of variable size
  • to string be independent
  • avoid copying the string to be able to use it more easily
  • to struct gets smaller because of this and may make it easier to use it as value
  • it is possible to change its value without changing other references to the old value

Of course there are disadvantages, the main one is that you usually have to manage the lifespan of that string associated.

The pointer creates a indirect.

The two forms can be used depending on what you want each can be the most suitable.

In the case of array which is a composite type, i.e., it has several values together, it is a string of characters. The computer does not know how to manipulate composite data, its code needs to do this. This is the reason you need to use a strcpy() to copy the string from the static area into the struct.

To struct with a array in the code above has size 30, so you have to put the string inside. And it’s your problem not to let this limit be exceeded. Nothing prevents you from putting more than 30 characters (including the terminator of string), if you exceed this limit you will probably corrupt the memory.

This is a practical example as these two types are very different, although the array always be accessed through a pointer and the pointer can always be accessed with the syntax of array. But understand that in the background the language only has the pointer. The array is a syntactic facility and a means of allocating memory.

  • The pointer of char points to several addresses in memory already the vector points to a single address and several values, I’m sure and that’s right?

  • No, the pointer (char *) points to one address. This address may be the beginning of a data sequence (in this case a string of characters), but a pointer points to a memory position. We know that there will be a sequence, the computer does not know. It is our problem to control it. The array It doesn’t stop being a pointer and all that goes for it too, there is no difference. Only that the type array has a difference to pointer type, which is how the content is allocated, the 1st. is allocated inline, where it is declared and generates a pointer to that location.

  • The pointer your code needs to tell you where it should point, maybe making an allocation first. Then you can think and if I create a pointer and access like this: var[3] then he turned array? No, you used the syntax of array to access a pointer. var[0] is one pointer, var[1] is another pointer. Don’t confuse pointer, array with the data in memory. Read the link initial answer? There it helps to understand this difference and similar between array and pointer.

  • Yeah, I read it, and it’s kind of hard not to make a fuss between them, but I’m getting it better. If you had an image that illustrates the difference between array and pointer it would be much better to visualize pq and very abstract :)

  • It took me years to fully understand and I don’t even know if I understood it 100%:) I’ll see if I do something, but actually it’s not easy, because they are the same thing concretely :) The type is that is treated differently.

  • I’ve been programming in C for almost two years and I still don’t understand tbm :) This is because the array has its types, and spaces in memory are used to store addresses or values, which causes certain confusion?

  • That’s basically it. Think that there is only pointer and syntax of array is only to help do pointer arithmetic. If it is pointer itself you have the address where the value is, if it is array already reference to the value. The only most important difference is the memory allocation. The array even allocates memory where it is declared. And obviously if the computer allocates, it knows the size. Other than that, there is only pointer, the rest is just a syntax that seems to be array, then I think it lessens the confusion.

Show 2 more comments

1

Another way of seeing is so the pointer has variable value, ie it can point to anywhere in memory. Already the vector is not static it never changes and if you try to change will have problems.

int main (int argc, char * argv[]) {

    char vetor[30] = "Jhonatan";
    char * ponteiro_vetor = vetor;

    printf("%d\n", sizeof(vetor));
    printf("%d\n", sizeof(ponteiro_vetor));
    printf("%p\n", vetor);
    printf("%p\n", ponteiro_vetor);
    ponteiro_vetor = ponteiro_vetor + 1;
    printf("%p\n", ponteiro_vetor);
}

But if you try to access an invalid place you will have a segmentation failure.

Browser other questions tagged

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