Archived

This topic is now archived and is closed to further replies.

C pointers!

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, was wondering on the use of strcpy function. Am trying to use it with a TCHAR and a TCHAR *. It compiles and runs with an error that says it is accessing memory at location 0x000000. If anyone knows how I can copy the contents of the pointer to the array I would appreciate it. Thanx in advance!

Share this post


Link to post
Share on other sites
You need to allocate the space that you are copying into using strcpy. For example, this will not work:

char *ptr;
strcpy(ptr, "Hello World");

because ''ptr'' has not been initialized to point to anything. You could do the following:

char *ptr, buffer[128];
ptr = &buffer[0];
strcpy(ptr, "Hello World");

because ''ptr'' now points to valid storage - the local variable ''buffer''.

The toughest thing to understand about pointers is that, by default, they don''t point to ANYTHING valid. You have to assign them to point to some valid storage. That valid storage might be other variables or memory that you allocate using either malloc or new.

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
using the string class is easier





--{You fight like a dairy farmer!}


True, if you are using C++. The topic however is ''C pointers!'' which probably means the person is programming in C and not C++, thus not having the STL available.

Share this post


Link to post
Share on other sites