Click to See Complete Forum and Search --> : pointers


Notsosuperhero
January 30th, 2005, 02:53 PM
Okay this is really making me mad. I have a pointer to a char(char *filename) and I want to assign that to a buffer(char buffer). I've tried looking around and tried what they put( buffer = &filename, etc..) but it still pops up an error
error C2440: 'initializing' : cannot convert from 'char **__w64 ' to 'char []' Please help.

Hobson
January 30th, 2005, 03:04 PM
Show us some code, someone will help then.

Notsosuperhero
January 30th, 2005, 04:56 PM
Something like this:
char *string;
char buffer = &string

gstercken
January 30th, 2005, 05:07 PM
Something like this:
char *string;
char buffer = &stringWell, there are two things wrong here: You are taking the address of the pointer (instead of just the pointer itself) and you are trying to assign it to a single character (not a buffer). In order to copy text characters pointed to by a char* to a char array, you should use strncpy().

NigelQ
January 30th, 2005, 05:08 PM
Your terminology is a little difficult to understand.

A character pointer is a pointer to a series of characters in memory.

A character array is storage space for such a series of characters.

You said you have a pointer (possibly a char *) which you would like to assign to buffer (whatever that is).

If you are trying to copy the characters from one place to another, you may need to use something like strcpy to perform a simple string copy. Before doing this, the buffer must be large enough to hold these characters being copied.

Something like this should work...

char *MyPointer = "Hello World";
char MyBuffer[100];

strcpy(MyBuffer, MyPointer);

Hope this helps,

- Nigel

Kheun
January 30th, 2005, 07:50 PM
... or doing it in the C++ way.


include <string>
using namespace std;

int main()
{
const char *myString = "Hello world";
string anotherString(myString);

return 0;
}

Notsosuperhero
January 30th, 2005, 08:56 PM
Thank you all. I was a little tired(OK really tired) when I was posting this.