Home c How to glue two lines in si?

How to glue two lines in si?




when the code is executed

# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
  char * first = "first";
  Char * Second = "Second";
  Char * Third = Strcat (First, Second);

Error segmentation occurs.

Answer 1, Authority 100%

If the contents of the strings are known at the compilation stage, then the gluing can be organized at the compilation stage

# define first "first"
#Define Second "Second"
  Const char * first = first;
  Const Char * Second = Second;
  Const Char * Third = First Second;

But it depends on what you need.

For gluing during execution, use the STRCAT function – not the best idea due to the fact that with each gluing function again and again performs the scanning part of the line. (For this reason, Strcat is actually a useless function.) It is better to use for these purposes by the usual snprintf

# include & lt; stdio.h & gt;
  Const char * first = "first";
  Const char * second = "Second";
  Char Third [512];
  SNPrintf (Third, Sizeof Third, "% S% s", First, Second);

And how much memory redundancy method for the recipient string is more suitable – depends on your specific circumstances and requirements.

Answer 2, Authority 56%

To glue 2 lines in SI, you need to perform the following steps:

  1. using the malloc function to highlight the memory unit (Result ), sufficient to save both lines to it (and not forget about the place for the final zero)

  2. using the Memcpy function Copy the first line S1 to the beginning of the dedicated block

  3. Using the same Memcpy function, copy the second line S2 , along with its zero, in the dedicated block, with a displacement of the first line size

  4. After you committed some manipulations with the resulting line, it needs to be released using the free

  5. function


char * concat (char * s1, char * s2) {
    Size_t Len1 = strlen (S1);
    Size_t Len2 = Strlen (S2);
    char * result = malloc (Len1 + Len2 + 1);
    if (! result) {
    Memcpy (Result, S1, Len1);
    Memcpy (Result + Len1, S2, Len2 + 1);

How to use:

char * s = concat ("first", "second");
  PrintF ("% s \ n", s);
  free (s);

Answer 3, Authority 33%

For Strcat The first argument should indicate the buffer of sufficient length so that the final (glued) string can fit into it. Variables First and Second are pointers for string literals, i.e. Data on such indexes cannot be changed. This leads to uncertain behavior (UB).

The initial example can be rewritten as follows:

# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; String.h & gt;
  Const char * first = "first"; // const explicitly indicates that such data cannot be changed
  Const char * second = "Second";
  char * buff = Calloc (Strlen (FIRST) + STRLEN (SECOND) + 1, 1);
  Strcat (buff, first);
  Strcat (Buff, Second);
  PrintF ("% s \ n", buff);

execution result

Answer 4, Authority 22%

The easiest option is to clearly reserve under the first string (to which the "second line) of memory with a margin will be" sticking:

# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
  Char First [256] = "First";
  Char Second [] = "Second";
  Strcat (FIRST, SECOND);
  PUTS (First);
  Return 0;

Performance result: http://ideone.com/ooxUCV

Answer 5, Authority 11%

# include & lt; stdlib.h & gt;
#Include & lt; String.h & gt;
Char * Concat (Char * S1, Char * S2)
  Char * Result = Malloc (Strlen (S1) + Strlen (S2) +1); // + 1 is needed for a string zerodator.
  1. highlight the desired number of memory
  2. glue and return the pointer to the beginning of the memory block.

Answer 6, Authority 11%

Since such a massive correspondence of neoproposis has gone, I will also bring my contribution.

GNU has a very convenient analogue Sprintf - asprintf , which places the result of "output" of its arguments in dynamic memory (uses Malloc).

Accordingly, the program may be like this:

# ifndef _gnu_source
#Define _GNU_Source.
#Include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; String.h & gt;
#Include & lt; errno.h & gt;
Main (Int AC, char * AV [])
 Char First [] = "first",
  Second [] = "Second";
 char * third;
 If (Asprintf (& amp; Third, "% S% s", First, Second) & lt; 0)
  Third = Strdup (STRERROR (ERRNO));
 Return Puts (Third) == EOF;

And if you have it (asprintf) no, it is easy and to write it yourself

static int
Asprintf (char ** ps, Const char * fmt, ...)
 // Puts ("My Asprintf");
 VA_List AP;
 INT RC = VSNPrintf (* PS, 0, FMT, AP);
 IF (RC & GT; = 0) {
  if ((* PS = (char *) malloc (rc + 2))) {
   rc = vsnprintf (* PS, RC + 1, FMT, AP);
   VA_END (AP);
  } ELSE.
   rc = -1;

(or scold) and use without any #define _gnu_source Even in Windows (at least Mingw successfully broadcasts).

Answer 7, Authority 11%

Because the strcat () function adds

  • line to which second parameter (SECOND ")
  • to a string to which first parameter ("first" )

  • and then rotates first parameter - pointer to the first line, but to which the second line is now added ,

You need to the first parameter to highlight sufficient memory for the connected line ("firstsecond" ) - or during compilation, or dynamically.

Answer 8

The problem is that the STRCAT function does not allocate memory, and copies from one line to another. Since the length of the first string is less than the length of the second, then the memory is overwhelmed. You need to change the code, for example, so.

# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
  Char First [7] = "first";
  Char * Third = Strcat (First, Second);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions