Changing the contents of the char variable

7

I'm having trouble working with files and manipulating 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 compose by error in the excerpt:

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

Would you like to do something similar to PHP?

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

My intention is to be able to change the contents of the variable to whatever the user passes.

    
asked by anonymous 18.11.2014 / 13:14

1 answer

5

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

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

But if you have a variable (your code does not show this) that is a string (actually a pointer to char or a char vector) another string , is very simple (it may not work well in more complex cases, but it does not 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. This way: varA = varB; . But if you want to copy the same content, only c / strcpy() .

You can therefore initialize char fileTipoDespesaDefault[50] = "tipo_despesa.txt"; taking care not to try to place a text larger than the reserved memory (remembering that string still has a hidden character at the end NULL or realloc() ). But this only works on the variable declaration. It does not work for a simple assignment.

If you need to use larger spaces you have to reserve enough space for what you need and do not leave "the user" (in fact your program is responsible for this) to go beyond this limit.

Or you can do the heap allocation as you did originally and go relocating ( char *fileTipoDespesaDefault = "tipo_despesa.txt"; ) every time you need a bigger space. That is, you have to manage the memory at hand. Of course there are libraries that help in this but I do not know if it is your intention to start for this, you lose the grace 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.

Making %code% may seem to work but it is a danger because you will be taking content that is in a static area of memory, which is read-only and can not exchange this content. Of course you can still change the pointer of the variable to another area of memory (which can be written).

It may seem confusing but it is the actual way the computer works. It does not have the abstractions that PHP provides. All this I mentioned PHP should do as well, but it does for you.

    
18.11.2014 / 14:34