Why do some functions that work with C strings start with *?

3

In one of our classes we were taught that when a function receives as a parameter a vector, it is actually receiving the memory position in which it is allocated, so it is not necessary to return any value since what will be executed within the procedure will automatically be changing in the vector / string itself. But some functions are starting with a * and returning a vector. Ex:

int strlen(char *s){
  int i = 0;
  while(s[i]!='
char *strcpy(char *dest, char *orig){
    int i;
    for(i=0; orig[i]!='
int strlen(char *s){
  int i = 0;
  while(s[i]!='
char *strcpy(char *dest, char *orig){
    int i;
    for(i=0; orig[i]!='%pre%'; i++){
        dest[i] = orig[i];
    }  
    dest[i] = '%pre%';
    return dest;
}
'){ i++; } return i; }
'; i++){ dest[i] = orig[i]; } dest[i] = '%pre%'; return dest; }
'){ i++; } return i; }

Example of a default function that returns the number of elements in string .

%pre%

Example of a function that is returning a string / vector.

What is the reason some functions start with * and consequently return a vector?

    
asked by anonymous 28.04.2017 / 16:45

2 answers

6

Because this particular function should do this. Look at its name: string copy . It should copy a string . What you've learned is correct, if you get a pointer, anything that fiddles with the content pointed to by that variable will change the contents of the original memory, since it's the same location. But in this case you do not want to mess with what already exists, you want to create a copy of that content somewhere else. So you need to create a new object, not the same object. After this function finishes creating this new object it should return where this object is, so it returns a pointer to this new object .

Another way to ensure this is to pass this address as an argument. And in fact this occurs in this function. Maybe your question is why doing both.

The philosophy of C is to always let the programmer take care of memory management because it will probably do the best for every situation. So if you have to allocate memory for the string copy you should pass that address as an argument, and in fact this is being done.

Then according to what you learned the copy will be placed at this last address, changing that new object. Great, it's settled, you do not have to return anything, right?

Yes, right. But it is common to use this function to allocate in another variable and for convenience if given the option to return the same address that you passed to the function to use as a new object.

Better to do

char* nova = strcpy(malloc(10), "texto");

than

char* nova = malloc(10);
strcpy(nova, "texto");
    
28.04.2017 / 17:07
0
  

Being pedantic about reading function declaration

This mistake happens due to the Code Style School of the person who wrote this function. I'll write equivalent headers, each in a different style:

char *strcpy(char *dest, char *orig);
char* strcpy(char *dest, char *orig);
char *strcpy(char* dest, char* orig);
char * strcpy(char * dest, char * orig);
char* strcpy(char* dest, char* orig);

What's the difference between the statements above? For the compiler, none. There are for those who write and for those who read, but this is only at the level of code style.

  

Why do these different style schools exist?

Well, each one has a proposal, a value defended. It's kind of like questioning "if you have cubism, why did you use dadaism?" ...

If someone wrote with the pointer pointer always on the left side thought the pointer pointer should be isolated from the words around it, the left pointer would have succumbed long ago.

I'm from school that, in variable declaration, I use the pointer pointer always pasted into the variable name. Because? Beauty counts? Also, because the following two statements are identical:

int *p, i;
int* p, i;

In both cases, a p pointer and an integer i are being created. The idea of the school I follow is to always show in the variable that it has this pointer pointer, preventing anyone from inadvertently interpreting that i is another pointer to integer.

Thus, you create the habit of always separating the type from * , which may not be salutary. In functions, for example, I do not see motive of disambiguation of reading, only use because yes. Note that because each argument is preceded by its type, the reason for reading improvement becomes irrelevant as well.

Summary

You would not say char* strcpy(char* dest, char* orig); starts with * , would you say? So it does not return a vector because it starts with * , but returns a pointer because the declared return type is the% po_de% character pointer.

    
08.06.2017 / 06:59