String concatenation strcat; differences between arrays and pointers

Source: Internet
Author: User

Problem: String concatenation strcat

Method 1:

Open up a new space and store the results:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>char* _strcat(char* str1, char* str2){  assert(str1 != NULL && str2 != NULL);  char* ret = (char*)malloc((strlen(str1)+strlen(str2)+1)*sizeof(char));  char* tmp = ret;  while(*str1 != ‘\0‘) *tmp++ = *str1++;  while(*str2 != ‘\0‘) *tmp++ = *str2++;  return ret;}int main(){  char str1[] = "abc";  char str2[] = "def";  char tmp = _strcat(str1,str2);  printf("result = %s\n",tmp);  free(ret);  return 0;}

Result:

[[email protected] Desktop]# ./a.outresult = abcdef[[email protected] Desktop]# 

 

Method 2:

Without opening up space, add str2 directly after str1;

At first, I wrote the following code:

void _strcat2(char* str1,char* str2){  while(*str1++ != ‘\0‘);  while((*str1++ = *str2++) != ‘\0‘);}int main(){  char* str1 = "abc";  char* str2 = "def";  _strcat2(str1,str2);  printf("result = %s\n",str1);  return 0;}

A segment error occurs, and the problem is solved after char * str1 = "ABC" is changed to Char str1 [] = "ABC;

Then the problem occurs: the output result is "ABC" instead of "abcdef". In while (* str1 ++! = '\ 0'); then add str1.

 

The following two questions are discussed:

1. Differences between char Str * and char STR.

The differences between the two are described below. The following code:

Char ss [] = "C ++"; SS [0] = 'C'; // valid char * P = "C ++ "; P [0] = 'C'; // valid but incorrect

The problem lies in the statement P [0] = 'C. This statement attempts to modify the first character of the string pointed to by P. An error occurs.

The reason is that the two methods have different mechanisms for operating character Arrays: After the char * P = "C ++" statement is used, the compiler allocates a piece of memory in the constant area of the memory, save the literal value of the "C ++" string, and allocate the memory on the stack to save P. The content of P is the address of "C ++. P [0] = 'C' the program crashes by trying to modify the constant "C ++. The Char ss [] = "C ++" statement defines an array, and the compiler allocates memory space for it on the stack, so it can be modified.

Therefore, it can be summarized as follows:
Char ss [] defines an array. SS can be considered as a regular pointer and SS cannot be changed, but the content pointed to by SS can be changed.
Char * P defines a variable pointer. P can point to other objects. But for char * P = "ABC", P points to a constant, so the content cannot be changed.

 

Let's take a look at this:

char* strA()  {      char str[]="Hello";      return str;  }

If you call this function, you may not be able to obtain the correct result. Because STR defines a local variable, which exists in the stack of function stra. After the function is called, the temporary space is reset, And the stack space allocated to the function is reclaimed, so the address indicated by STR does not exist.

Modify the above Code:

char *strA()  {      char *str="Hello";      return str;  }  

No problem.

 

Question 2: How to Use ++ Operators

In the while (* str1 ++! = '\ 0'); let's look at the execution process: 1. First Judge * str1! = '\ 0' then str1 ++; 2. when * str1 = '\ 0' is executed to the last one, str1 ++ is executed. 3. therefore, str1 points to the position after '\ 0'. Therefore, the error is that the' \ 0' at the end of str1 has been skipped, after splicing, the result is "a B C \ 0 d e f \ 0". When printf reaches C, '\ 0' is returned to stop the output.

 

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.