Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: C bug involving char arrays

Thread Tools

Re: C bug involving char arrays

Chandra Shekhar Kumar
Posts: n/a

David Baldwin wrote:

> Ok I believe this falls under the category of stupid newbie question ;p
> The function below returns values correctly whenever length is > 3.
> However, anything from 1-3 returns an array of correct values followed by
> garbage.
> Any insight on what I'm doing wrong here?
> Thanks
> char* getBlock(char* array, int length, int start){
> if(length == 0) return "";
> char* c = new char[length];
> for(int z=0;z<length;z++){
> c[z] = array[z+start];
> }
> return c;
> }

let me take the case when length = 5, start = 4;
then this loop
for(int z=0;z<length;z++){
c[z] = array[z+start]; }

then the index in the array is from 4 to 8, but where u r insuring
this...means the array u r passing might not have that much size...

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre C++ 3 03-09-2006 12:14 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Re: C bug involving char arrays Rob Williscroft C++ 0 06-24-2003 01:33 PM