0

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

6
  • *M = '1' and M++ should suffice.You dont need *(char*)&M = '1' and &M++. Commented Oct 18, 2012 at 2:24
  • hi abhinole, M++ still throwing an error Commented Oct 18, 2012 at 2:39
  • @user1275129...Please post the errors and some more code. Commented Oct 18, 2012 at 2:42
  • 1
    Because you cannot modify the array - if you want a pointer use a pointer, ie unsigned char *X=M - please just post a full code example that to stop people trying to guess what random things your are trying to do Commented Oct 18, 2012 at 2:46
  • hi abhinole, i have updated my question with detailed code Commented Oct 18, 2012 at 2:52

3 Answers 3

2

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.

char myArray[20];    // an array of 20 characters

myArray[2] = 'C';    // assigning a value of the letter C to the third element of the array
*(myArray + 2) = 'K';  // assigning a value of the letter K to third element of the array


char *pmyArray;      // a pointer to one or more characters, not yet initialized though.
pmyArray = myArray;  // assigning the address of the first element of the array to my pointer
*(pmyArray + 2) = 'J';     // assigning a value of the letter J to third character after the character pointed to by pmyArray

The value of a pointer can be incremented. For example:

pmyArray++;    // increment by one position
pmyArray = pmyArray + 1;    // increment by one position
pmyArray += 1;       // increment by one position

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:

char myArray[48];
char *pmyChar = myArray;
short *pmyShort = (short *)myArray;  // use cast to assign address of array to pointer
long  *pmyLong = (long *)myArray;

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:

pmyChar++;      // increment by one to next character
pmyShort++;     // increment by one to next short
pmyLong++;      // increment by one to next long

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:

char myArray[48];
char *pmyArray;
char **pmyPointerToMyArray;

pmyArray = myArray;                 // pointer to myArray
pmyPointerToMyArray = &pmyArray;    // address of pointer to myArray

Then you can do something like this:

pmyArray++;   // increment to second element of myArray
*(pmyArray) = 'J';  // set the second element of myArray to letter J
*(*pmyPointerToMyArray) = 'K';  // set the second element of myArray to letter K

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.

Sign up to request clarification or add additional context in comments.

Comments

0

you need to declare a new pointer and assign its value to &M then increase this new pointer instead of increase &M directly.

In C array name is pointer but its value can not be changed.

Comments

0

You will need to declare a separate pointer variable, rather than trying to manipulate M. C will not allow you to assign to or modify M directly.

char M[1000];
char *p = M; // or &M[0]
...
*p++ = 'a';  // M[0] = 'a'
*p++ = 'b';  // M[1] = 'b'

Except when it is the operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will the address of the first element of the array.

However...

While you can dereference the resulting pointer expression with * (for example, the expressions *M and M[0] have the same type and yield the same value), it may not be an operand of the ++ or -- operators, and it may not be the target of an assignment operator. So you cannot do things like M++ or M += ....

Also,

from my understanding, when i do &M++ i am increasing the address to 1 byte,

No. Whenever you advance a pointer with ++, you increase the address by the size of the pointed-to type. If p points to a char type, then yes, p++ will advance to the address of the next char value, which is the next byte. If p points to an int type, p++ will advance to the address of the next int value, which may be 2, 4, or even 8 bytes from the current address, depending on the platform.

Note that &M++ won't work for several reasons. One, I've already pointed out that M may not be an operand of ++, since it's an array type. Two, postfix ++ has higher precedence than unary &, so the expression is parsed as &(M++), and the result of ++ is not an lvalue, meaning it doesn't have an address as such, so you can't apply the & operator to it. Three, the expression &M would yield a pointer to an array of char (char (*)[1000]), not an individual char. Assume the following declarations:

char *p = M;
char (*ap)[1000] = &M;

p++;  // points to M[1];
ap++; // points to the char following M[999];

p++ advances p to the next char. ap++ advances ap to the next 1000-element array of char.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.