Difference in char* and const char* at the beginning of a function in C

Asked

Viewed 665 times

1

const char* lerArquivo(FILE *file, int tam, char *path){
file = fopen(path, "r");
int cont;
char *palavra;
for(cont = 0; cont < tam; cont++){
    fscanf(file, "%c", &palavra[cont]);
    palavra[cont + 1] = '\0';
}
return palavra;

For example in this code and others, what would be the difference if the function declaration were used only char* and not const char*?

The same goes for other types of declaration, such as int, int*, const int*, etc..?

What changes in function when using a different declaration?

  • Do you know that the name const, comes from constant ? If you say const in the code you are basically saying that the contents of the variable will not change. Besides that const helps the compiler in some cases.

  • That in case the variable you will return from the function is of type const char

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

2

I’m going to start with C because that’s what you used in the code, you didn’t use C++.

That’s how he agreed to consider what a string in C. The language does not have the type string, it has only the concrete mechanism that allows us to have the same result.

One string in other languages is not simply a collection of characters, so a array where each element is a character, that’s all. It usually has the size of the text somewhere (which may be the size of the array - but some cases this is a little more complicated and needs additional information). Another way to set the text size is to say that a special character indicates when it is over (a null is the most common), is the case of C.

Like C has not array as in other languages, then the strings are represented only as pointers to a char, then is the char *.

But the most common is that strings are immutable, that is, you cannot change the content within it, it is better to ensure this by telling the compiler that you want nobody to change. So we declare that the content pointed by the pointer is constant, therefore const char *. Nothing prevents to circumvent it, but within the norm is respected.

So this function returns a string immutable, and it is expected that whoever calls the function keeps this in a variable that guarantees immutability as well, that is, the variable is of the type const char *. But whoever calls the function is not required to do this, only recommended, it can use only char * and can change the content of the text.

The other types have other values that are not characters, so they do not form what we call string, but the principle is the same, it is, for example, a array of integers that can be changed or not (if const).

In C++11 on all string need to have const. Actually in C++ the recommended is to neither do this and use the type string.

Note that you can change the pointer and point to another object, constancy refers only to the object in question and not to the pointer which is another value. It is possible to make the pointer constant, but is used in fewer situations:

const int * const

Browser other questions tagged

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