Thread: what is wrong here??? dynamic array of chars

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Since you're implementing your own strcat function, I would do it this way:
    Code:
    bool strcat(char* dst, const char* src, uint32_t dest_length)
    {
    	const char* tmp = src;    
    	uint32_t count_src = strlen(src);
    	uint32_t count_dst = strlen(dst);
    	if (count_src + count_dst + 1 > dest_length)
    		return false;
    	dst += count_dst;
    	while (*src) *dst++ = *src++;
    	*dst = 0;
    	return true;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jun 2008
    Posts
    23
    Thanks... Will be used probably in 3-4 months... and I mean many thanks.

    As for my understanding of this function, it does way more than my currnent needs and I need to "downsize" purposes. I know I make it sound pretty vague. This is a mini project for current fixes.
    The most important part is using the "array" way of char, meaning indexes, rather than pointers (that was our supervisor's request). In 5-6 months time, all of the bigger project will become dynamic and in a c++ kind of way (while decremanting the c syntax entirely).

    And again, many thanks for your implementationg of strcat()...

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Well, no, it doesn't do much.
    It copies just like strcat, but it also checks that the buffer (destination) you passed is big enough to hold the data. If it isn't, it returns false, and if it it succeeds, it returns true.
    But it also uses pointers instead of indexes, but that's not difficult to change.
    You're still welcome to try to understand it. A debugger might help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see any problem there. It has to do with something how you are using the function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    23
    That's exactly what I meant...
    I will use it in the bigger project, but not yet (since the current request is using a "void" syntax
    and 2 pointers as parameters).
    I'm guessing that our supervisor already implemented his own project, and probably used the functions just as he requested.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I understand that you shouldn't use it, but you can still dissect it and learn something from it.
    But... as you go deeper into C++ (if I get you correctly), you won't need strcat at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Jun 2008
    Posts
    23
    And as I already said, I took it and am "learning something from it"...

    Nice to know that there are good people out there after all... for:
    "There's the respect that's makes calamity of so long lives,
    for who would bear the weeps and scorns of time?", or so they say...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 09:46 AM
  2. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM