#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2

    Copying char arrays the right way


    I'm wondering how to copy char from argv into a char arrray. Now before you call me an idiot and point me to the many string copying functions I'd like to show you some things.
    I have trouble figuring out what signedness the char arrays in argv have.
    I've read much on good programming style and all the information says to use either an unsigned char array or a signed char array but to please use some kind of a sign.
    Personally, I am using memcpy which, at least with gcc, does not produce an error. I'm not so stupid though to think that just because it works on my system that it will work
    on all systems (i.e. don't make ussumptions.) Further, while browsing through my libraries in my package manager I came across a library intended to provide a more secure version of, among other things, strcpy. It was called "s_strncpy secure strncpy."
    It's web adress is

    http://www.suse.de/~thomas/projects/secproglib/index.html

    Again no signedness is specified. In fact all the string copying functions accept a pointer
    to a char array, no sign.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481

    An internet spy might care


    I must be the dummy. In copying bits from here to there why would anyone care about what those bits of bytes might represent there or here?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Originally Posted by b49P23TIvg
    I must be the dummy. In copying bits from here to there why would anyone care about what those bits of bytes might represent there or here?
    If the bytes are singed then it affects the value of them. By the way, I was browsing my compilers docs and it has an option to make all the char either signed or unsigned so I will use that.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by ballsystemlord
    I have trouble figuring out what signedness the char arrays in argv have.
    C does have a "signed" keyword, but in practice you rarely have to use it, since the default is signed. it would get tedious if you had to type "signed char" all over the place.

    Originally Posted by ballsystemlord
    Personally, I am using memcpy.
    memcpy doesn't care about signedness. It only cares about the source and destination addresses and the number of bytes to copy.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0
    For copying memory it really doesn't matter if char is signed or unsigned. Usually char is signed, but it's implementation defined.
    Originally Posted by RedRacer
    C does have a "signed" keyword, but in practice you rarely have to use it, since the default is signed. it would get tedious if you had to type "signed char" all over the place.
    This is incorrect. "signed char" is a distinct type from "char" in C. This only applies to "char".

IMN logo majestic logo threadwatch logo seochat tools logo