I'd be extremely grateful if someone could tell me why I get runtime errors with the following code. I want to pass the array inBuf[6] containing the characters {HELLO} to the function getAbbrv() and return only the first 3 characters in an array. If I pass HELLO, I want the return array abb[] to contain HEL. How do I do this?
Thanks a million!
KH.
--------------------------------------------
#include <cstring>
#include <stdlib.h>
#include <ostream.h>
void getAbbrv( char* inbuf, char abb[])
{
char ab[3];
for (int i = 0; i < 2; i++)
{
ab = inbuf;
}
ab = '\0';
strcpy(abb, ab);
//cout << "ABB IS---"<< abb << endl;
}
int main()
{
char inBuf[] = "HELLO";
char* abbrv = "XX";
/* this for loop is just to test my inBuf array */
for (int i = 0; i<strlen(inBuf)+1; i++)
cout << inBuf;
cout << '\n' ;
/* this is the function call that doesn't work */
getAbbrv( inBuf, abbrv);
return 0;
}
Thanks a million!
KH.
--------------------------------------------
#include <cstring>
#include <stdlib.h>
#include <ostream.h>
void getAbbrv( char* inbuf, char abb[])
{
char ab[3];
for (int i = 0; i < 2; i++)
{
ab = inbuf;
}
ab = '\0';
strcpy(abb, ab);
//cout << "ABB IS---"<< abb << endl;
}
int main()
{
char inBuf[] = "HELLO";
char* abbrv = "XX";
/* this for loop is just to test my inBuf array */
for (int i = 0; i<strlen(inBuf)+1; i++)
cout << inBuf;
cout << '\n' ;
/* this is the function call that doesn't work */
getAbbrv( inBuf, abbrv);
return 0;
}