Strcat of C library function learning notes

Source: Internet
Author: User

The Code is as follows:

# Include <stdio. h>/* for and while loops */char * strcat1 (char * to, char * from) {char * save = to; for (; * save; save ++); while (* save ++ = * from ++ )! = '\ 0'); // return save; // return to before modification;}/* for Loop implementation */char * strcat (char * to, char * from) {char * save = to; for (; * save; save ++); for (; (* save = * from )! = '\ 0'; save ++, from ++); // return save; // return ;} /* while loop implementation */char * strcat2 (char * to, char * from) {char * save = to; while (* save) save ++; while (* save ++ = * from ++ )! = '\ 0'); // return save; // return to before modification;} int main () {char c [20] = {0 }; // char c [20]; char * p = "Hello, world! "; Strcat2 (c, p); printf (" % s \ n ", c); return 0 ;}

The test code contains char c [20] = {0}; and 2, char c [20];

If it is written as char c [20] = {0}, you can completely output Hello, world !, If char c [20] is used, garbled characters are output,

Why?


2. while (* save) save ++ in the while LOOP implementation; can it be changed to while (* save ++ )?;



Follow-up:


Problem 1: char c [20]; defines a char-type array c with a length of 20, but it is not cleared, that is to say, the value of c [0] is not necessarily '\ 0'. This can be verified by printing its value. We can see that when defining data structure variables such as arrays and struct, we need to clear them before using them. Otherwise, an inexplicable exception may occur. Array erasing methods: 1. For example, char c [20] = {0}; 2. Use the system clearing function: memset (c, '\ 0', 20 ); question 2: while (* save) save ++ in the while LOOP implementation; can it be changed to while (* save ++ )?; This is obviously not acceptable. no matter whether the value of * save is '\ 0', the pointer will be offset and point to the next address.

Question 3: Compare the standard strcat function,

char * strcat(char *s, const char *append){    char *save = s;    for (; *s; ++s);    while ((*s++ = *append++) != '\0');    return(save);}

What are the problems in the previously written functions?

1. What is the function return value?

I didn't think so much about it before, leading to such a cute error. For a function, what should we pay attention to: function functions, parameters, and return values? Isn't that the three points? It's simple to say, so it's not much to worry about when writing code by yourself. Shame!

This function concatenates strings. Why do I need to return a pointer to the first address? After thinking about the result, I had to look for google. It was originally intended to implement chained operations, such as strcpy (s, strcat (s1, s2 ));


2. Question about writing *, that is, what is the problem between char * strcat and char * strcat?

According to hua jie, there are three writing formats for defining pointer variables:

1) char *;

2) char *;

3) char *;

The first is his own method, the second is often used in many open-source projects, and the third is his most contemptuous usage, and I use the third method. Decisively despised.

Char * a; three-step explanation of this statement:

1. First, apply for four bytes in the memory to store variable;

2. Define a pointer variable,

3. The data type pointed to by the pointer is char, which determines the number of digits at each offset of the pointer.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.