copy a const char* into array of char (facing a bug)


Mexico
24/4/2016 9:44 am
HOME ยป Web Development

I have following method

static void setName(const char* str, char buf[16])
{
    int sz = MIN(strlen(str), 16);
    for (int i = 0; i < sz; i++) buf[i] = str[i];
    buf[sz] = 0;
}


int main()
{
   const char* string1 = "I am getting bug for this long string
greater than 16 lenght);
   char mbuf[16];
   setName(string,mybuf)
   // if I use buf in my code it is leading to spurious characters
since length is greater than 16 . 

Please let me know what is the correct way to code above if the restriction for buf length is 16 in method static void setName(const char* str, char buf[16])


Related to : copy a const char* into array of char (facing a bug)
copy a const char* into array of char (facing a bug)
24/4/2016 9:44 am Web Development

I have following method

static void setName(const char* str, char buf[16])
{
    int sz = MIN(strlen(str), 16);
    for (int i = 0; i < sz; i++) buf[i] = str[i];
    buf[sz] = 0;
}


int main()
{
   const char* string1 = "I am getting bug for this long string
greater than 16 lenght);
   char mbuf[16];
   setName(string,mybuf)
   // if I use buf in my code it is leading to spurious characters
since length is greater than 16 . 

Please let me know what is the correct way to code above if the restriction for buf length is 16 in method static void setName(const char* str, char buf[16])


On casting from const char array to unsigned char * and Compile .c in Visual C++
Web Development

Dear All,
Can anyone give me some tips on casting from const char array to unsigned char * ? When using as functions parameters, this kind of casting is often used.
By the way, when saved as .c file, directly using const char array when unsigned char * is needed seem allowed. Maybe this is ralted with the downcast issue. Can any expert give me some instructions on it? And how to setup and Compile .c project in Visual C++?
Thank you in advance!
Sincerely,
HWB
Initialize a const char array with data from a non const char array
Web Development

I am working on a JNI project, the env->NewStringUTF function will only accept a const char as a parameter.
The trouble I am having is that within the native method I need to modify the string that is passed in and then return it. If i can initialize and put the data I need into a non-constant char [ ] then is there a way to create a const char[ ] that contians the data in the non-const one then pass the const array to the JNI function.
Pointer to const char vs char array vs std::string
Web Development

Here I've two lines of code


const char * s1 = "test";
char s2 [] = "test";

Both lines of code have the same behavior, so I cannot see any difference whether I should prefer s1 over s2 or vice-versa. In addition to s1 and s2, there is also the way of using std::string. I think the way of using std::string is the most elegant. While looking at other code, I often see that people either use const char * or char s []. Thus, my question is now, when should I use const char * s1 or char s [] or std::string? What are the differences a



HD Wallpapers
3D
3D Abstract
City
Celebrities
Indian Celebrities
Cars
Travel
Girls
Animals Birds
Movies
Sports
Black White
Nature
Planes
Anime
Food Drink
Dreamy Fantasy
Architecture
Games
Space
Holidays
Flowers
Love
Artistic
Baby
Beach
Bikes Motorcycles
Macro
Computers
Vector
Funny
Army
Textures
Brands
Misc
Music
Other
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .