Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using char* for strings

Status
Not open for further replies.

GJSmith

Programmer
Aug 23, 2004
2
GB
Hi all,

I hope that someone here can tell me the correct way to use char* for strings.

My code is similar to the following:

char* filename = "\0";
char* fileno = "\0";

for (int i=1;i<20;i++)
{
capGrabFrame(hWnd);
strcpy(filename, "C:\\pic");
itoa(i,fileno,10);

filename = strcat(filename,fileno);
filename = strcat(filename,".bmp");
capFileSaveDIB(hWnd, filename);
}

It seems perfectly logical to me, but no matter what I try, I always get a seg fault or some bizarre string as output! Because of the API, I think I have to use char*. Believe me, I'd use a string class if I could!

TIA
Gregor
 
Your problem is that you are making filename and fileno pointers to buffers without enough space to hold what you are trying to put into them. Make them both a char[20] or something and it will work. Also, saying filename = strcat(filename,fileno) is redundant because the second string is automatically appended to the first.
 
Or more concisely
Code:
for (int i=1;i<20;i++)
{
  char filename[20];
  capGrabFrame(hWnd);
  sprintf( filename, "C:\\pic%d.bmp", i );
  capFileSaveDIB(hWnd, filename);
}

--
 
Thank you, Timmay and Salem. Appreciate your help. I managed to get it working. I do, however, have two further questions: I'd previously tried using char[x] instead of char * and VC++ claimed I couldn't convert between the two. In which circumstances can and can I not do this? Also, why is the second string automatically appended to the first when you don't explicitly ask for it to be?

Many thanks again
Gregor
 
I'd previously tried using char[x] instead of char * and VC++ claimed I couldn't convert between the two

It shouldn't. Example?

why is the second string automatically appended to the first when you don't explicitly ask for it to be?

Where, for example?
 
>>I'd previously tried using char[x] instead of char * and VC++ claimed I couldn't convert between the two

You must have been using incorrect syntax then. In C++, an array is just a pointer in the first place. If you have a char array named szBuf, then the actual value of szBuf is just a pointer to the first element in the array.

>> why is the second string automatically appended to the first when you don't explicitly ask for it to be?

That's just how strcat works. strcat concatinates the second string to the first, and returns the first (this is useful if you want to send it as an argument to another function, ie, you can just do strcpy(str1, strcat(str2, str3))). If you wanted it to leave the first string unchanged and return a new, concatinated string, strcat would be a serious pain. Think about it - the only way this would work is if strcat allocates memory, in which case you would have to remember to delete the concatinated string once you are done with it. Another option would be to send three buffers: one as the first string, one as the second string, and a third as the destination string. This implementation would make sense, but I can't see that it would be better than the current implementation, as they would both be easy enough to use.
 
Ah, this is a C++ forum, so why not use CString and totally forget about allocations, char indexes, etc?

TR
 
>>I'd previously tried using char[x] instead of char * and VC++ claimed I couldn't convert between the two

Did you try to do:

char buffer[10] = "hello";

That would generate a type mismatch error: from a char[6] to a char[10], which are different sized arrays. (But char * pbuff = "hello"; works because pbuff is a pointer to the first character in "hello", then.)

I hope that helps,

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top