HomeLinuxStrdup() Perform within the C Language

Strdup() Perform within the C Language


Memorizing and mastering the capabilities that take care of strings is essential as a result of we at all times use them in some a part of this system that we develop.

Strings are an vital a part of programming as a result of they’re used to ship messages to the consumer, show the outcomes, learn the information or specify their path, amongst different issues. They’re additionally the enter and output technique that facilitates the interplay between the consumer and this system.

On this Linux Trace article, you’ll learn to use the strdup() operate to duplicate the strings. We are going to see the syntax and the theoretical description of calling the tactic, its enter and output arguments, and the accepted information sort in every case.

Then, we apply what we discovered within the sensible examples utilizing code snippets and pictures to indicate you find out how to use the strdup() operate to duplicate the strings.

Syntax of the Strdup() Perform in C language

char *strdup(const char *str);

Description of the Strdup() Perform in C language

The strdup() operate duplicates a string. This operate duplicates the string that’s pointed to by the str pointer which is handed within the enter argument, and returns a pointer to the brand new string because the consequence.

Strdup() belongs to the strXXdup() household of capabilities, all of that are meant to duplicate the strings, however every of which has a distinct calling mode and performance to fulfill the actual wants of the programmer.

Like all string processing capabilities, strdup() is outlined within the “string.h” header. To make use of it, you should embody it in your code as follows:

Duplicate a String Utilizing the Strdup() Perform within the C Language

On this instance, we use the strdup() operate to duplicate a string. Then, we use the printf() operate to show the “unique” and “duplicate” string pointers within the command console.

To do that, we embody the “stdio.h” and “string.h” headers in our code and outline the 2 string pointers inside the principle() operate. The primary one is named “unique” which incorporates the “Instance for the strdup() operate” expression. The second pointer is named “duplicate” which incorporates the situation the place the strdup() operate shops the copy of the unique string.

As soon as the strings that this system makes use of are outlined, we name the strdup() operate and go the pointer to the unique string because the enter argument and the pointer to the duplicated string because the output argument. Then, we use the print() operate to show the “unique” and the “duplicate” string tips on the command console. Right here is the code for this instance:

#embody <stdio.h>
#embody <string.h>
void most important ()
{
char* unique =“instance of strdup() operate”;
char* duplicate;
duplicate = strdup (unique);
printf (“Authentic :%sn, unique);
printf (“Duplicate :%sn, duplicate);
}

As we see within the following determine, strdup() duplicates the unique string by actually duplicating its contents.

Variations Between the Strdup() and Strndup() Capabilities

The strXXdupX() household consists of the next capabilities:

char *strdup(const char *s);
char *strndup(const char *s, size_t n);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);

The capabilities on this household are all designed to duplicate the strings, however every of them works differently.

As we have now seen, the strdup() operate fully duplicates the string that’s despatched in its enter argument and returns an actual copy in its output argument.

The strndup() operate duplicates the string of its enter argument. It might accomplish that fully or partially.

Not like strdup(), the strndup() operate takes two enter arguments. The primary argument is “str” which is the pointer to the string to be duplicated. The second argument is “n” which is a variable of sort size_t that specifies the variety of characters that it copies.

Within the following instance, we see the usage of the strndup() operate the place solely 8 of the characters of the unique string are duplicated.

#embody <stdio.h>
#embody <string.h>

void most important ()
{
char* unique =“instance of strdup() operate”;
char* duplicate;
size_t n =8;

duplicate = strndup (unique, n);

printf (“Authentic :%sn, unique);
printf (“Duplicate :%sn, duplicate);
}

As seen within the following determine, the strndup() operate duplicates a quantity “n” characters from the unique string.

Errors That Will be Generated by the Strdup() Perform

The error that this operate can generate is that “the system doesn’t have sufficient reminiscence to run the method” which is unlikely on present methods.

This error is outlined within the “errno.h” header and its code will be queried through the errno world variable. Its definition is ENOMEM.

Conclusion

On this Linux Trace article, we defined find out how to use the strdup() operate which belongs to the household of string processing capabilities that’s outlined within the string.h header.

We appeared on the syntax of this operate and supplied a theoretical clarification about its utilization, its enter and output arguments.

That will help you higher perceive the way it works, we created a sensible instance with code and pictures the place you may see how the pointers and strings which might be utilized by this operate are created and the way the operate is named.

We additionally confirmed you find out how to use the strndup() operate, one other operate from the strXXdupX() household, in order that you understand the totally different prospects that the C language presents to duplicate the strings.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments