0
int num = 78;
int *p;

int array[SIZE] = {0,1,2,3,4};
char c[SIZE] = {'A', 'B', 'C', 'D', 'E'};

p = array[3];
*p = (int) *c;
p++;
array[4] = num;
p++;
p = c;
p++;

I'm trying to figure out the memory behind this above code. I understand that the pointer p initially points to the 3rd element of the array(which is 3). I have no idea what the next line *p = (int) *c; means. Can anyone please explain that line of code??

Edit: After the p is incremented as such can anyone explain what it would be pointing to?

3 Answers3

0

*p = (int) *c;

*c means that you take the value at the address of c

(int) casts it to an int

*p= writes to the address p points to

So if you fix what droppy said, there will be the numerical value of c[0] in the 3rd part of array

so it would be 1,2,65,4,5

Kami Kaze
  • 2,069
  • 15
  • 27
  • "the address of `p`" is `&p`. You mean "to the address `p` points to". That is basic stuff one finds in any tutorial. The cast is problematic and a properly augmented compiler should actually warn about that assignment. Note: use markdown consistently. – too honest for this site Oct 12 '16 at 15:17
  • @Olaf I'm sry, thats what I meant... Just formulated it wrong – Kami Kaze Oct 13 '16 at 05:21
0
  1. You should use 'p = &array[3];'. The pointer will then point to the third element of the array i.e. 'C'

  2. *p = (int) *c;

c[size] is an array. c is the base pointer of the array. so *c is the value at the base pointer which is 'A'. This statement will put 'A' in the third element of the array. So the array now contains A, B, A, D, E

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
0
p = array[3]; // int * = int

is an error; the types don't match, and the compiler will yell at you for it. The type of p is int *, and the type of array[3] is int.

There are two ways to fix this, depending on what you want to do. If you want to set p to point to array[3] (which is what you want to do in this case), you would write

p = &array[3]; // int * = int *

If you want to write the value of array[3] to the object that p points to (which is not what you want to do in this case, since p isn't pointing anywhere valid yet), you would write

*p = array[3]; // int = int

In this case, we want to set p to point to array[3], so we use the first statement. After doing that, the following are true:

 p == &array[3]          // int *
*p ==  array[3] == 2     // int

Now we have the statement

*p = (int) *c;

is saying "take the value of the char object that c points to, convert it to an int value1, and assign the result to the object that p points to."

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize an array of char 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 be the address of the first element of the array.

The expression c has type "5-element array of char". Since it is not the operand of the sizeof or unary & operators, it is converted to an expression of type char *, and the value of the expression is the address of the first element in the array, c[0]. Thus:

 c == &c[0]                         // char *
*c ==  c[0] == 'A' == 65 (ASCII)    // char

Taking all that together, that means

*p = (int) *c;

is another way of writing

*p = (int) c[0];

which is another way of writing

array[3] = (int) c[0];

which is another way of writing

array[3] = (int) 'A';

which is another way of writing

array[3] = 65;


  1. (int) is a cast expression; it means that the value following it should be treated as type int.

John Bode
  • 119,563
  • 19
  • 122
  • 198