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

3

In this answer bigown said that if the frame member was a pointer you do not need to copy the string into it. But arrays are not pointers? Why is it different?

    
asked by anonymous 02.02.2017 / 12:35

2 answers

4

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

The main difference between them is that the array reserves space in memory for its value, the pointer only reserves space for the pointer itself. The value is elsewhere indicated 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));
}

See running on ideone . And at Coding Ground . Also I put it in GitHub for future reference .

The first one has size 4 because the pointer is this size. The string will need 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 within struct into a simple operation.

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

You can change the value of this member of struct by changing the pointer. If the pointer originally pointed to a static area of memory and needs to change its value, it will have to create a new string and change the pointer to another that points to this new string . This is necessary because we can not change the static area of memory.

Using the pointer has some advantages, the main ones:

  • a string can have variable length
  • string becomes independent
  • avoid copying to string so you can use it more easily
  • struct gets smaller because of this and may make it easier to use it as value
  • You can change its value without changing other references to the old value

Of course there are drawbacks, the main one being that you often have to manage the lifetime of that string associated.

The pointer creates an indirection .

The two forms can be used, depending on what you want each one may be the most appropriate.

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

The struct with an array in the above code has size 30, so you have to put the string in there. And it's your business not to let that limit burst. Nothing prevents you from putting more than 30 characters (including the string terminator), if you exceed this limit will probably corrupt memory.

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

    
02.02.2017 / 12:35
1

Another way of looking at it is that the pointer has a variable value, that is, it can point anywhere in memory. Since the vector is not static it never changes and if you try to change it 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 fault.

    
02.02.2017 / 16:56