Click here to Skip to main content
15,915,603 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalexperimenting... "to understand pointers more" Pin
SilverShalkin30-May-02 15:20
SilverShalkin30-May-02 15:20 
GeneralRe: experimenting... "to understand pointers more" Pin
Paul M Watt30-May-02 16:02
mentorPaul M Watt30-May-02 16:02 
GeneralRe: experimenting... "to understand pointers more" Pin
SilverShalkin30-May-02 16:18
SilverShalkin30-May-02 16:18 
GeneralRe: experimenting... "to understand pointers more" Pin
Roger Broomfield30-May-02 18:04
Roger Broomfield30-May-02 18:04 
GeneralRe: experimenting... "to understand pointers more" Pin
Nish Nishant30-May-02 17:56
sitebuilderNish Nishant30-May-02 17:56 
GeneralRe: experimenting... "to understand pointers more" Pin
SilverShalkin30-May-02 18:11
SilverShalkin30-May-02 18:11 
GeneralRe: experimenting... "to understand pointers more" Pin
Nish Nishant30-May-02 18:18
sitebuilderNish Nishant30-May-02 18:18 
GeneralRe: experimenting... "to understand pointers more" Pin
Matt Gullett30-May-02 19:55
Matt Gullett30-May-02 19:55 
WARNING: This is a long post.

I think your real problem here is a misunderstanding of how C style pointers work and how to use them. Let me start by saying that a pointer is an intrinisic data type just like float, int, char, etc. The pointer data type (on Win32 systems) is a 32 bit value. This means that when you write a statement like

void* pSomeValue;


you are telling the compiler to allocate 32 bits of memory for your program. It also identifies that you intend to populate that variable with a memory address. In the case of a void type pointer you are saying that the memory pointed to by the variable can be of any type you want. (In other words, no type affinity.)

C and C++ allows pointers to have type affinity also. This means that instead of being a void pointer, you can define a pointer like

char* pszSomeValue;

OR
long* plSomeValue;

OR
CSomeClassName* pSomeValue;

ETC.

When you define a pointer like this you are telling the compiler that you want it to allocate 32 bits of storabe for a variable that will eventually point to a specific data type (char, long, CSomeClassName).

The next subject of confusion (I think) is when you are using C style arrays of a particular data type. In your example, you used char* pchar[6] and char character[6] = "hello".

The char* pchar[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a pointer to a char. This means that the compiler will allocate six 32 bit memory locations whose values it expects to be pointers to a char (char*).

The char character[6] is instructing the compiler to allocate 6 memory locations each having a type affinity for a char (NOT a pointer to a char). This means that the compiler will allocate six 8 bit memory locations (that is the size of a char data type) whose value it expects to be a char.

One of the nuiansces of C style arrays is that when you allocate a C style array the compiler guarantees that the memory it allocates will be continuous. This means that when you do char *pchar[6];, the compiler is going to allocate 32*6 bits in a row. When you do char character[6], the compiler is going to allocate 8*6 bits in a row.

OK. That was a long winded explanation of pointers and arrays. With that understanding, lets look at the line of code where the compiler is giving you a problem.

strcpy(pchar, &character);


The strcpy function is defined as taking 2 paramaters, a char* and a const char*. (Ignore the const for now.) The second parameter is supposed to be a pointer to an array of characters with the last character being NULL (NULL terminated string.) The first parameter is supposed to be a pointer to an array of characters large enough to contain the data pointed to by the second parameter. (BYW strcpy will copy the contents of the second parameter to the first, therefore you will have 2 seperate copies of the data. Changing one will not affect the other.)

For the first parameter, you are passing in a an array of char* type pointers. This is quite different from an array of char type. (Remember char* = 32 bits, char = 8 bits.) As you can see, for the first parameter, the compiler is confused because it is expecting the char array, not the char* array.

You say "what i am trying to do is, have pchar point to the address of character". If this is really what you want, then what you would want to write is

char *pchar;
char character[6] = "hello";

pChar = character;


You may be thinking "how can I assign an array to a pointer?". The answer to this obvious questions goes back to the previous explanation or how C style arrays work. When you specicify the variable name of a C style array without specifying an index into the array (ie. character instead of character[0]), the compiler knows that you want a pointer to the array.

I hope this helps more that it confuses.
GeneralRe: experimenting... "to understand pointers more" Pin
SilverShalkin31-May-02 11:19
SilverShalkin31-May-02 11:19 
GeneralIs it me, or does there seem to be a bubble of questions with long subjects and nothing of any merit in the actual body of the message. Pin
Tim Smith30-May-02 13:38
Tim Smith30-May-02 13:38 
Generalall fixed :) Pin
SilverShalkin30-May-02 15:39
SilverShalkin30-May-02 15:39 
GeneralRe: Is it me, or does there seem to be a bubble of questions with long subjects and nothing of any merit in the actual body of the message. Pin
Nish Nishant30-May-02 17:57
sitebuilderNish Nishant30-May-02 17:57 
Generaldevice context of picture box control Pin
Speedy30-May-02 13:10
Speedy30-May-02 13:10 
QuestionHow do i catch when the user selects an item in a listview control with the RIGHT mouse button? Pin
redeemer30-May-02 13:03
redeemer30-May-02 13:03 
AnswerRe: How do i catch when the user selects an item in a listview control with the RIGHT mouse button? Pin
Tomasz Sowinski30-May-02 13:05
Tomasz Sowinski30-May-02 13:05 
GeneralRe: How do i catch when the user selects an item in a listview control with the RIGHT mouse button? Pin
redeemer30-May-02 13:28
redeemer30-May-02 13:28 
GeneralOveriding mouse events Pin
30-May-02 11:51
suss30-May-02 11:51 
GeneralVariable issues Pin
Stew30-May-02 11:37
Stew30-May-02 11:37 
GeneralRe: Variable issues Pin
Bill Wilson30-May-02 11:43
Bill Wilson30-May-02 11:43 
GeneralRe: Variable issues Pin
Stew30-May-02 12:01
Stew30-May-02 12:01 
GeneralRe: Variable issues Pin
Bill Wilson30-May-02 12:18
Bill Wilson30-May-02 12:18 
GeneralRe: Variable issues Pin
James R. Twine31-May-02 10:48
James R. Twine31-May-02 10:48 
GeneralCleaning up from Visual Studio 6 Pin
dazinith30-May-02 9:47
dazinith30-May-02 9:47 
GeneralRe: Cleaning up from Visual Studio 6 Pin
Ed Gadziemski30-May-02 11:06
professionalEd Gadziemski30-May-02 11:06 
GeneralRe: Cleaning up from Visual Studio 6 Pin
Jonathan Craig30-May-02 11:56
Jonathan Craig30-May-02 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.