I know there is a lot c pointer tutotiral on the web and I did read them. However I still confuse about some part of the pointer. For example, I have an array unsigned char M[10000], now I want to store the values in the array using pointer way instead of specifying the index , my code below has an error.
//starting to put value into M[0]
*(char*)&M='1';
//increase the address to M[1]
&M++;
//input value at M[1]
*(char*)&M='a';
from my understanding, when I do &M++ I am increasing the address to 1 byte, where is the place of M[1] stands, but there is an error. I am new to c, thanks for help.
detailed code- updated:
#include <stdio.h>
#include <stdlib.h>
int eax,ebx,ecx,edx,esi,edi,ebp,esp;
unsigned char M[10000];
void exec(void){
*M= 'a';
M++ ;
//append space behind a
*(char*)&M += ' ' ;
*(char*)&M += 'b' ;
}
int main() {
exec();
print(M);
}
I am using gcc and codeblock
An array variable name can be considered to be a constant pointer. While it can be treated as a pointer, the variable name value can not be modified.
For instance we can create an array and then access an element of the array by either using subscripts or by using pointer arithmetic and dereferencing using the asterisk (*) symbol.
The value of a pointer can be incremented. For example:
However the array name is not a true pointer variable it is more like a pointer constant so an array name can not be incremented in the way that a pointer variable can be incremented. The nice thing about a pointer that is incremented is that the actual address is increased by the number of bytes needed to move the pointer to the address of the next memory address for the type.
For example:
With these statements, the three different pointers will all point to the same memory address, the address where the character array myArray begins. However if we then increment each of these pointers like so:
each of these pointers will now contain a different address. The pointer pmyChar will contain the address of the second element of the character array myArray because pmyChar is a char pointer just as the myArray is a char array.
The pointer pmyShort will contain the address of the third element of the character array myArray because pmyShort is a short pointer, a short contains two bytes (each char is a byte), and the incrementing of pmyShort is done by increasing the address contained in the pointer variable by the size of a short and not by the size of a char.
The pointer pmyLong will contain the address of the fifth element of the character array myArray because pmyLong is a long pointer, a long contains four bytes, and the incrementing of pmyLong is done by increasing the address contained in the pointer variable by the size of a long and not by the size of a char.
You can also have a pointer to a pointer like the following:
Then you can do something like this:
What this last statement does is to get the value pointed to by pmyPointerToMyArray, which is the address of the variable pmyArray, and to then use that value as the address of a character to put the letter K.