How to convert char to char[40]

Usually I do strcpy but here is looking like I can't copy bigger-sized to lower-sized array. I understand that I need to skip an array element for it, I want to skip first [0] element but how can I do it? I really don't want to write something alike a[39]=b[40]; a[38]=b[39]... etc.

Answers


Play safe. Use strncpy instead of strcpy:

std::strncpy(a, b + 1, 40);

It will work even if the last character in b is not '\0', or there is '\0' somewhere at index i.

strncpy is particularly useful if a and b are not to be treated as cstring, rather they're just buffer which might or might not contain '\0'.

In C++, you can also use std::copy as:

#include <algorithm> //include this!

std::copy(b + 1, b + 41, a); //b + 41 = (b + 1) + 40

I usually prefer std::copy, for it is generic and can be used with any iteratable type.


Need Your Help

How to determine by what IP Address my website has been accessed?

java jsp url java-ee servlets

I have a web application and for that I want to capture the IP address of the clients who are accessing my website so that I can know what region is the most accessing the application. I am using J...

D2: empty string in a conditional statement

d d2

In the following code, why does 2 give output but not 3? The removechars statement returns a string with length 0

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.