Change of char variable content

Asked

Viewed 767 times

7

I’m having trouble working with files and handling char.

CODE

char *fileTipoDespesaDefault;
char *fileTipoPagamentoDefault;

FILE *fileTipoDespesa;
FILE *fileTipoPagamento;

/**
 * tipos : 1 => Despesa, 2 => Pagamento
 */
void load(char *arquivo, char modo[2], int tipo){

    FILE *fileOpen = fopen(arquivo, modo);

    switch (tipo){
        case 1:
            fileTipoDespesa = fileOpen;
            break;
        case 2:
            fileTipoPagamento = fileOpen;
            break;
        default:
            break;
    }
}

int main(){

    strcpy(fileTipoDespesaDefault, "tipo_despesa.txt");
    strcpy(fileTipoPagamentoDefault, "tipo_pagamento.txt");

    menuInicial();

    system("PAUSE");
    return 0;
}

SITUATION

This code does not compile by error on the snippet:

strcpy(fileTipoDespesaDefault, "tipo_despesa.txt");
strcpy(fileTipoPagamentoDefault, "tipo_pagamento.txt");

If I replace the variables for:

char fileTipoDespesaDefault[50];
char fileTipoPagamentoDefault[50];

Everything is okay.

DOUBT

You could do something similar to PHP?

$fileTipoDespesaDefault = "tipo_despesa.txt";
$fileTipoPagamentoDefault= "tipo_pagamento.txt";

My intention is to be able to change the content of the variable to anything the user passes.

1 answer

6


The error that happens when using pointer notation is that you are not allocating memory, either in the heap with malloc() or, less commonly, in stack with alloca(). The strcpy() is just the way to assign a value to a "string" variable. It’s, C, it’s fast, it’s powerful, but it takes work. See about stack and heap.

When you use vector notation the allocation is done by the compiler on stack. This has advantages and disadvantages as in everything in computing.

But if you have a variable (your code does not show this) it is a string (actually a pointer to char or a vector of char) and want to play this value to another string, It is possible very simply (it may not work well in more complex cases, but it doesn’t matter now that you are learning). Since you have two pointers (even in vector notation, you still have a pointer), you can only copy the pointer, so the two variables will point to the same content. In this way: varA = varB;. But if you want to copy the content yourself, only c/ strcpy().

It is possible to initialize like this char fileTipoDespesaDefault[50] = "tipo_despesa.txt"; taking care not to try to put a larger text than the reserved memory (remembering that the string still have a hidden character at the end \0 or NULL). But this only works in the variable declaration. It doesn’t work for a simple assignment.

If it is necessary to use larger spaces you have to reserve enough space for whatever it takes and not let "the user" (actually your program is responsible for this) pass this limit.

Or you can make the allocation on heap as it did originally and go relocating (realloc()) every time you need a bigger space. That is, you have to manage the memory in hand. Of course there are libraries that help in this but I do not know if it is your intention to leave for this, loses the fun of learning. In case of using the pointer you have to declare the variable, allocate the memory and copy the content to it, there is no way.

Do char *fileTipoDespesaDefault = "tipo_despesa.txt"; It may look like it works but it’s a danger because you’re picking up content that’s in a static area of memory, which is read-only and you can’t swap this content. Of course you can still change the variable pointer to another memory area (which can be written).

It may sound confusing but it’s the concrete way the computer works. It doesn’t have the abstractions that PHP provides. All this I mentioned PHP should do too, but it does for you.

Browser other questions tagged

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